-
-
Notifications
You must be signed in to change notification settings - Fork 401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(biome_css_analyzer): implement noInvalidPositionAtImportRule #2717
Merged
ematipico
merged 23 commits into
biomejs:main
from
t-shiratori:feature/noInvalidPositionAtImportRule
May 10, 2024
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a33755b
feat: noInvalidPositionAtImportRule
t-shiratori eb0c07d
chore: refactor
t-shiratori 10e882e
fix: conflict crates/biome_configuration/src/linter/rules.rs
t-shiratori 6e3c7e4
Update crates/biome_css_analyze/src/lint/nursery/no_invalid_position_…
t-shiratori f757457
Update crates/biome_css_analyze/src/lint/nursery/no_invalid_position_…
t-shiratori 981c14b
Update crates/biome_css_analyze/src/lint/nursery/no_invalid_position_…
t-shiratori 9f715bd
Update crates/biome_css_analyze/src/lint/nursery/no_invalid_position_…
t-shiratori e97271b
Update crates/biome_css_analyze/src/lint/nursery/no_invalid_position_…
t-shiratori 7495e81
fix: Emphasis @import
t-shiratori c6f45ab
fix: correct the order of notes
t-shiratori 9ca1793
chore: refactor
t-shiratori b3cfcf4
update: snap
t-shiratori f536251
chore: refactor
t-shiratori 6f752ed
fix: multiple diagnostic
t-shiratori 83b81ac
update:
t-shiratori 85420e3
Merge branch 'main' into feature/noInvalidPositionAtImportRule
t-shiratori b3c385d
Merge branch 'biomejs:main' into feature/noInvalidPositionAtImportRule
t-shiratori 370b78c
Merge branch 'main' into feature/noInvalidPositionAtImportRule
t-shiratori eb6210b
fix: test case validCharset
t-shiratori aa879c1
add: test case validCharsetComment
t-shiratori 27b6ab5
fix: test case validCharsetMultipleImport
t-shiratori 26d9f6e
Merge branch 'main' into feature/noInvalidPositionAtImportRule
t-shiratori e690c55
Merge branch 'main' into feature/noInvalidPositionAtImportRule
t-shiratori File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'; | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit Maybe add a test case for @charset "utf-8";
/* some comment */
@import 'foo.css'; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fix: aa879c1 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if you noticed, but the diagnostic isn't correct because it doesn't highlight the
@
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review.
Sorry, I didn't know how to do it 🙇♂️
Is there a sample somewhere?