-
-
Notifications
You must be signed in to change notification settings - Fork 475
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(biome_css_analyzer): implement noInvalidPositionAtImportRule (#2717
) Co-authored-by: Emanuele Stoppa <[email protected]>
- Loading branch information
1 parent
7c577de
commit 4213527
Showing
31 changed files
with
480 additions
and
43 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
crates/biome_css_analyze/src/lint/nursery/no_invalid_position_at_import_rule.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource}; | ||
use biome_console::markup; | ||
use biome_css_syntax::{AnyCssRule, CssRuleList}; | ||
use biome_rowan::{AstNode, TextRange}; | ||
|
||
declare_rule! { | ||
/// Disallow the use of `@import` at-rules in invalid positions. | ||
/// | ||
/// Any `@import` rules must precede all other valid at-rules and style rules in a stylesheet (ignoring `@charset` and `@layer`), or else the `@import` rule is invalid. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```css,expect_diagnostic | ||
/// a {} | ||
/// @import 'foo.css'; | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```css | ||
/// @import 'foo.css'; | ||
/// a {} | ||
/// ``` | ||
/// | ||
pub NoInvalidPositionAtImportRule { | ||
version: "next", | ||
name: "noInvalidPositionAtImportRule", | ||
recommended: true, | ||
sources: &[RuleSource::Stylelint("no-invalid-position-at-import-rule")], | ||
} | ||
} | ||
|
||
impl Rule for NoInvalidPositionAtImportRule { | ||
type Query = Ast<CssRuleList>; | ||
type State = TextRange; | ||
type Signals = Vec<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Vec<Self::State> { | ||
let node = ctx.query(); | ||
let mut is_invalid_position = false; | ||
let mut invalid_import_list = Vec::new(); | ||
|
||
for rule in node { | ||
let any_css_at_rule = match rule { | ||
AnyCssRule::CssAtRule(item) => item.rule().ok(), | ||
_ => None, | ||
}; | ||
|
||
if let Some(any_css_at_rule) = any_css_at_rule { | ||
// Ignore @charset, @layer | ||
if any_css_at_rule.as_css_charset_at_rule().is_some() { | ||
continue; | ||
} | ||
if any_css_at_rule.as_css_layer_at_rule().is_some() { | ||
continue; | ||
} | ||
|
||
let import_rule = any_css_at_rule.as_css_import_at_rule(); | ||
if let Some(import_rule) = import_rule { | ||
if is_invalid_position { | ||
invalid_import_list.push(import_rule.range()); | ||
} | ||
} else { | ||
is_invalid_position = true; | ||
} | ||
} else { | ||
is_invalid_position = true; | ||
} | ||
} | ||
invalid_import_list | ||
} | ||
|
||
fn diagnostic(_: &RuleContext<Self>, state: &Self::State) -> Option<RuleDiagnostic> { | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
state, | ||
markup! { | ||
"This "<Emphasis>"@import"</Emphasis>" is in the wrong position." | ||
}, | ||
) | ||
.note(markup! { | ||
"Any "<Emphasis>"@import"</Emphasis>" rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the "<Emphasis>"@import"</Emphasis>" rule is invalid." | ||
}).note(markup! { | ||
"Consider moving import position." | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
crates/biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalid.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
a {} | ||
@import 'foo.css'; | ||
@import 'bar.css'; |
48 changes: 48 additions & 0 deletions
48
crates/biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalid.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: invalid.css | ||
--- | ||
# Input | ||
```css | ||
a {} | ||
@import 'foo.css'; | ||
@import 'bar.css'; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.css:2:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
1 │ a {} | ||
> 2 │ @import 'foo.css'; | ||
│ ^^^^^^^^^^^^^^^^^ | ||
3 │ @import 'bar.css'; | ||
4 │ | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` | ||
|
||
``` | ||
invalid.css:3:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
1 │ a {} | ||
2 │ @import 'foo.css'; | ||
> 3 │ @import 'bar.css'; | ||
│ ^^^^^^^^^^^^^^^^^ | ||
4 │ | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` |
7 changes: 7 additions & 0 deletions
7
...me_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalidBetweenImport.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@import 'foo.css'; | ||
a {} | ||
@import 'bar1.css'; | ||
@import 'bar2.css'; | ||
a {} | ||
@import 'bar3.css'; | ||
@import 'bar4.css'; |
91 changes: 91 additions & 0 deletions
91
...s_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalidBetweenImport.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: invalidBetweenImport.css | ||
--- | ||
# Input | ||
```css | ||
@import 'foo.css'; | ||
a {} | ||
@import 'bar1.css'; | ||
@import 'bar2.css'; | ||
a {} | ||
@import 'bar3.css'; | ||
@import 'bar4.css'; | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalidBetweenImport.css:3:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
1 │ @import 'foo.css'; | ||
2 │ a {} | ||
> 3 │ @import 'bar1.css'; | ||
│ ^^^^^^^^^^^^^^^^^^ | ||
4 │ @import 'bar2.css'; | ||
5 │ a {} | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` | ||
|
||
``` | ||
invalidBetweenImport.css:4:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
2 │ a {} | ||
3 │ @import 'bar1.css'; | ||
> 4 │ @import 'bar2.css'; | ||
│ ^^^^^^^^^^^^^^^^^^ | ||
5 │ a {} | ||
6 │ @import 'bar3.css'; | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` | ||
|
||
``` | ||
invalidBetweenImport.css:6:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
4 │ @import 'bar2.css'; | ||
5 │ a {} | ||
> 6 │ @import 'bar3.css'; | ||
│ ^^^^^^^^^^^^^^^^^^ | ||
7 │ @import 'bar4.css'; | ||
8 │ | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` | ||
|
||
``` | ||
invalidBetweenImport.css:7:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
5 │ a {} | ||
6 │ @import 'bar3.css'; | ||
> 7 │ @import 'bar4.css'; | ||
│ ^^^^^^^^^^^^^^^^^^ | ||
8 │ | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` |
2 changes: 2 additions & 0 deletions
2
...iome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalidMediaImport.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@media print {} | ||
@import url('foo.css'); |
28 changes: 28 additions & 0 deletions
28
...css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalidMediaImport.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: invalidMediaImport.css | ||
--- | ||
# Input | ||
```css | ||
@media print {} | ||
@import url('foo.css'); | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalidMediaImport.css:2:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
1 │ @media print {} | ||
> 2 │ @import url('foo.css'); | ||
│ ^^^^^^^^^^^^^^^^^^^^^^ | ||
3 │ | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` |
2 changes: 2 additions & 0 deletions
2
...analyze/tests/specs/nursery/noInvalidPositionAtImportRule/invalidMediaImportUpperCase.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@media print {} | ||
@imPort URl('foo.css'); |
28 changes: 28 additions & 0 deletions
28
...ze/tests/specs/nursery/noInvalidPositionAtImportRule/invalidMediaImportUpperCase.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: invalidMediaImportUpperCase.css | ||
--- | ||
# Input | ||
```css | ||
@media print {} | ||
@imPort URl('foo.css'); | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalidMediaImportUpperCase.css:2:2 lint/nursery/noInvalidPositionAtImportRule ━━━━━━━━━━━━━━━━━━━━━ | ||
! This @import is in the wrong position. | ||
1 │ @media print {} | ||
> 2 │ @imPort URl('foo.css'); | ||
│ ^^^^^^^^^^^^^^^^^^^^^^ | ||
3 │ | ||
i Any @import rules must precede all other valid at-rules and style rules in a stylesheet (ignoring @charset and @layer), or else the @import rule is invalid. | ||
i Consider moving import position. | ||
``` |
2 changes: 2 additions & 0 deletions
2
crates/biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/valid.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@import 'foo.css'; | ||
a {} |
10 changes: 10 additions & 0 deletions
10
crates/biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/valid.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: valid.css | ||
--- | ||
# Input | ||
```css | ||
@import 'foo.css'; | ||
a {} | ||
``` |
2 changes: 2 additions & 0 deletions
2
crates/biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/validCharset.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@charset "utf-8"; | ||
@import 'foo.css'; |
10 changes: 10 additions & 0 deletions
10
...biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/validCharset.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: validCharset.css | ||
--- | ||
# Input | ||
```css | ||
@charset "utf-8"; | ||
@import 'foo.css'; | ||
``` |
3 changes: 3 additions & 0 deletions
3
...ome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/validCharsetComment.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@charset "utf-8"; | ||
/* some comment */ | ||
@import 'foo.css'; |
11 changes: 11 additions & 0 deletions
11
...ss_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/validCharsetComment.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: validCharsetComment.css | ||
--- | ||
# Input | ||
```css | ||
@charset "utf-8"; | ||
/* some comment */ | ||
@import 'foo.css'; | ||
``` |
3 changes: 3 additions & 0 deletions
3
..._analyze/tests/specs/nursery/noInvalidPositionAtImportRule/validCharsetMultipleImport.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@charset "utf-8"; | ||
@imPORT 'foo.css'; | ||
@import 'bar.css'; |
11 changes: 11 additions & 0 deletions
11
...yze/tests/specs/nursery/noInvalidPositionAtImportRule/validCharsetMultipleImport.css.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
source: crates/biome_css_analyze/tests/spec_tests.rs | ||
expression: validCharsetMultipleImport.css | ||
--- | ||
# Input | ||
```css | ||
@charset "utf-8"; | ||
@imPORT 'foo.css'; | ||
@import 'bar.css'; | ||
``` |
2 changes: 2 additions & 0 deletions
2
crates/biome_css_analyze/tests/specs/nursery/noInvalidPositionAtImportRule/validComment.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/* some comment */ | ||
@import 'foo.css'; |
Oops, something went wrong.