-
-
Notifications
You must be signed in to change notification settings - Fork 961
feat(lint): add nursery rule useImportsFirst
#9272
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c6d9702
feat(lint): add nursery rule `useImportsFirst`
terror e5f5245
Add changeset
terror e4594c0
Merge branch 'main' into use-imports-first
terror bb4718f
fix(lint): add "why" note to useImportsFirst diagnostic
terror 2a6ad5c
test(lint): add useImportsFirst test with all imports after statements
terror 70dec32
[autofix.ci] apply automated fixes
autofix-ci[bot] 0246a34
docs(lint): fix changeset wording for useImportsFirst
terror 241311f
refactor(lint): use UseImportsFirstOptions instead of unit type
terror 2e78c65
test(lint): add useImportsFirst test with directive before imports
terror 10f3274
Merge branch 'main' into use-imports-first
terror a784b75
fix(lint): reword useImportsFirst diagnostic to describe the error
terror 3acc388
Merge branch 'main' into use-imports-first
terror c445b79
Merge branch 'main' into use-imports-first
terror 804dc36
docs(changeset): add code snippet to useImportsFirst changeset
terror 1c42091
docs(useImportsFirst): remove 'Note that' from directive sentence
terror 7ddf5e8
docs(useImportsFirst): note that rule does not apply to require() calls
terror 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
This file contains hidden or 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,17 @@ | ||
| --- | ||
| "@biomejs/biome": patch | ||
| --- | ||
|
|
||
| Added the nursery rule [`useImportsFirst`](https://biomejs.dev/linter/rules/use-imports-first/) that enforces all import statements appear before any non-import statements in a module. Inspired by the eslint-plugin-import [`import/first`](https://github.com/import-js/eslint-plugin-import/blob/HEAD/docs/rules/first.md) rule. | ||
|
|
||
| ```js | ||
| // Invalid | ||
| import { foo } from "foo"; | ||
| const bar = 1; | ||
| import { baz } from "baz"; // ← flagged | ||
|
|
||
| // Valid | ||
| import { foo } from "foo"; | ||
| import { baz } from "baz"; | ||
| const bar = 1; | ||
| ``` | ||
12 changes: 12 additions & 0 deletions
12
crates/biome_cli/src/execute/migrate/eslint_any_rule_to_biome.rs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
98 changes: 98 additions & 0 deletions
98
crates/biome_js_analyze/src/lint/nursery/use_imports_first.rs
This file contains hidden or 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,98 @@ | ||
| use biome_analyze::{ | ||
| Ast, Rule, RuleDiagnostic, RuleSource, context::RuleContext, declare_lint_rule, | ||
| }; | ||
| use biome_console::markup; | ||
| use biome_js_syntax::{AnyJsModuleItem, JsModuleItemList}; | ||
| use biome_rowan::{AstNode, AstNodeList, TextRange}; | ||
| use biome_rule_options::use_imports_first::UseImportsFirstOptions; | ||
|
|
||
| declare_lint_rule! { | ||
| /// Enforce that all imports appear at the top of the module. | ||
| /// | ||
| /// Import statements that appear after non-import statements are harder to | ||
| /// find and may indicate disorganized code. Keeping all imports together at | ||
| /// the top makes dependencies immediately visible. | ||
| /// | ||
| /// Directives such as `"use strict"` are always allowed before | ||
| /// imports, since they are parsed separately from module items. | ||
terror marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// | ||
| /// This rule only applies to ES module `import` statements. CommonJS | ||
| /// `require()` calls are not covered. | ||
| /// | ||
| /// ## Examples | ||
| /// | ||
| /// ### Invalid | ||
| /// | ||
| /// ```js,expect_diagnostic | ||
| /// import { foo } from "foo"; | ||
| /// const bar = 1; | ||
| /// import { baz } from "baz"; | ||
| /// ``` | ||
| /// | ||
| /// ### Valid | ||
| /// | ||
| /// ```js | ||
| /// import { foo } from "foo"; | ||
| /// import { bar } from "bar"; | ||
| /// const baz = 1; | ||
| /// ``` | ||
| /// | ||
| /// ```js | ||
| /// "use strict"; | ||
| /// import { foo } from "foo"; | ||
| /// ``` | ||
| /// | ||
| pub UseImportsFirst { | ||
| version: "next", | ||
| name: "useImportsFirst", | ||
| language: "js", | ||
| recommended: false, | ||
| sources: &[RuleSource::EslintImport("first").same()], | ||
| } | ||
| } | ||
|
|
||
| impl Rule for UseImportsFirst { | ||
| type Query = Ast<JsModuleItemList>; | ||
| type State = TextRange; | ||
| type Signals = Vec<Self::State>; | ||
| type Options = UseImportsFirstOptions; | ||
|
|
||
| fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
| let items = ctx.query(); | ||
| let mut seen_non_import = false; | ||
| let mut signals = Vec::new(); | ||
|
|
||
| for item in items.iter() { | ||
| match item { | ||
| AnyJsModuleItem::JsImport(_) => { | ||
| if seen_non_import { | ||
| signals.push(item.range()); | ||
| } | ||
| } | ||
| _ => { | ||
| seen_non_import = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| signals | ||
| } | ||
|
|
||
| fn diagnostic(_ctx: &RuleContext<Self>, range: &Self::State) -> Option<RuleDiagnostic> { | ||
| Some( | ||
| RuleDiagnostic::new( | ||
| rule_category!(), | ||
| range, | ||
| markup! { | ||
| "This import appears after a non-import statement." | ||
| }, | ||
| ) | ||
| .note(markup! { | ||
| "Scattering imports makes it harder to see the module's dependencies at a glance." | ||
| }) | ||
| .note(markup! { | ||
| "Move all import statements before any other statements." | ||
| }), | ||
| ) | ||
terror marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/invalid.js
This file contains hidden or 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,5 @@ | ||
| import { foo } from "foo"; | ||
| const bar = 1; | ||
| import { baz } from "baz"; | ||
|
|
||
| import { qux } from "qux"; | ||
terror marked this conversation as resolved.
Show resolved
Hide resolved
|
||
55 changes: 55 additions & 0 deletions
55
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/invalid.js.snap
This file contains hidden or 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,55 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: invalid.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| import { foo } from "foo"; | ||
| const bar = 1; | ||
| import { baz } from "baz"; | ||
| import { qux } from "qux"; | ||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid.js:3:1 lint/nursery/useImportsFirst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| i This import appears after a non-import statement. | ||
| 1 │ import { foo } from "foo"; | ||
| 2 │ const bar = 1; | ||
| > 3 │ import { baz } from "baz"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 4 │ | ||
| 5 │ import { qux } from "qux"; | ||
| i Scattering imports makes it harder to see the module's dependencies at a glance. | ||
| i Move all import statements before any other statements. | ||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
| ``` | ||
|
|
||
| ``` | ||
| invalid.js:5:1 lint/nursery/useImportsFirst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
| i This import appears after a non-import statement. | ||
| 3 │ import { baz } from "baz"; | ||
| 4 │ | ||
| > 5 │ import { qux } from "qux"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 6 │ | ||
| i Scattering imports makes it harder to see the module's dependencies at a glance. | ||
| i Move all import statements before any other statements. | ||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/invalid_all_after.js
This file contains hidden or 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,5 @@ | ||
| const foo = 1; | ||
| let bar = 2; | ||
| function baz() {} | ||
| import { qux } from "qux"; | ||
| import { quux } from "quux"; |
55 changes: 55 additions & 0 deletions
55
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/invalid_all_after.js.snap
This file contains hidden or 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,55 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: invalid_all_after.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| const foo = 1; | ||
| let bar = 2; | ||
| function baz() {} | ||
| import { qux } from "qux"; | ||
| import { quux } from "quux"; | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid_all_after.js:4:1 lint/nursery/useImportsFirst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i This import appears after a non-import statement. | ||
|
|
||
| 2 │ let bar = 2; | ||
| 3 │ function baz() {} | ||
| > 4 │ import { qux } from "qux"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 5 │ import { quux } from "quux"; | ||
| 6 │ | ||
|
|
||
| i Scattering imports makes it harder to see the module's dependencies at a glance. | ||
|
|
||
| i Move all import statements before any other statements. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid_all_after.js:5:1 lint/nursery/useImportsFirst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i This import appears after a non-import statement. | ||
|
|
||
| 3 │ function baz() {} | ||
| 4 │ import { qux } from "qux"; | ||
| > 5 │ import { quux } from "quux"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 6 │ | ||
|
|
||
| i Scattering imports makes it harder to see the module's dependencies at a glance. | ||
|
|
||
| i Move all import statements before any other statements. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/invalid_mixed.js
This file contains hidden or 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,5 @@ | ||
| import foo from "foo"; | ||
| foo.init(); | ||
| import bar from "bar"; | ||
| export { bar }; | ||
| import baz from "baz"; |
55 changes: 55 additions & 0 deletions
55
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/invalid_mixed.js.snap
This file contains hidden or 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,55 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: invalid_mixed.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| import foo from "foo"; | ||
| foo.init(); | ||
| import bar from "bar"; | ||
| export { bar }; | ||
| import baz from "baz"; | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| invalid_mixed.js:3:1 lint/nursery/useImportsFirst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i This import appears after a non-import statement. | ||
|
|
||
| 1 │ import foo from "foo"; | ||
| 2 │ foo.init(); | ||
| > 3 │ import bar from "bar"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^ | ||
| 4 │ export { bar }; | ||
| 5 │ import baz from "baz"; | ||
|
|
||
| i Scattering imports makes it harder to see the module's dependencies at a glance. | ||
|
|
||
| i Move all import statements before any other statements. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` | ||
|
|
||
| ``` | ||
| invalid_mixed.js:5:1 lint/nursery/useImportsFirst ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i This import appears after a non-import statement. | ||
|
|
||
| 3 │ import bar from "bar"; | ||
| 4 │ export { bar }; | ||
| > 5 │ import baz from "baz"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^ | ||
| 6 │ | ||
|
|
||
| i Scattering imports makes it harder to see the module's dependencies at a glance. | ||
|
|
||
| i Move all import statements before any other statements. | ||
|
|
||
| i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information. | ||
|
|
||
|
|
||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/valid.js
This file contains hidden or 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,5 @@ | ||
| /* should not generate diagnostics */ | ||
| import { foo } from "foo"; | ||
| import { bar } from "bar"; | ||
| import { baz } from "baz"; | ||
| const qux = 1; |
13 changes: 13 additions & 0 deletions
13
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/valid.js.snap
This file contains hidden or 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,13 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: valid.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should not generate diagnostics */ | ||
| import { foo } from "foo"; | ||
| import { bar } from "bar"; | ||
| import { baz } from "baz"; | ||
| const qux = 1; | ||
|
|
||
| ``` |
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/useImportsFirst/valid_directive.js
This file contains hidden or 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,5 @@ | ||
| /* should not generate diagnostics */ | ||
| "use strict"; | ||
| import { foo } from "foo"; | ||
| import { bar } from "bar"; | ||
| const baz = 1; |
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.
Uh oh!
There was an error while loading. Please reload this page.