-
-
Notifications
You must be signed in to change notification settings - Fork 529
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
278 additions
and
96 deletions.
There are no files selected for viewing
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
8 changes: 8 additions & 0 deletions
8
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.
207 changes: 113 additions & 94 deletions
207
crates/biome_configuration/src/analyzer/linter/rules.rs
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
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
74 changes: 74 additions & 0 deletions
74
crates/biome_js_analyze/src/lint/nursery/no_require_imports.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,74 @@ | ||
use biome_analyze::{ | ||
context::RuleContext, declare_lint_rule, Ast, Rule, RuleDiagnostic, RuleSource, | ||
}; | ||
use biome_console::markup; | ||
use biome_js_syntax::JsCallExpression; | ||
use biome_rowan::AstNode; | ||
|
||
declare_lint_rule! { | ||
/// Use the newer ES6-style imports over require(). | ||
/// | ||
/// ES6-style `import`s are modern alternative to CommonJS `require` imports. Supported by all modern browsers and Node.js versions. | ||
/// Tooling can more easily statically analyze and tree-shake ES6-style `import`s compared in comparison to `require`. | ||
/// | ||
/// ## Examples | ||
/// | ||
/// ### Invalid | ||
/// | ||
/// ```js,expect_diagnostic | ||
/// require('node:fs'); | ||
/// ``` | ||
/// | ||
/// ### Valid | ||
/// | ||
/// ```js | ||
/// import fs from 'node:fs'; | ||
/// ``` | ||
/// ```js | ||
/// import('node:fs') | ||
/// ``` | ||
/// | ||
pub NoRequireImports { | ||
version: "next", | ||
name: "noRequireImports", | ||
language: "js", | ||
sources: &[ | ||
RuleSource::EslintTypeScript("no-require-imports"), | ||
], | ||
recommended: false, | ||
} | ||
} | ||
|
||
impl Rule for NoRequireImports { | ||
type Query = Ast<JsCallExpression>; | ||
type State = (); | ||
type Signals = Option<Self::State>; | ||
type Options = (); | ||
|
||
fn run(ctx: &RuleContext<Self>) -> Self::Signals { | ||
let expression = ctx.query(); | ||
let callee = expression.callee().ok()?; | ||
let name = callee.as_js_reference_identifier()?.value_token().ok()?; | ||
if name.text_trimmed() == "require" { | ||
Some(()) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
fn diagnostic(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<RuleDiagnostic> { | ||
let node = ctx.query(); | ||
Some( | ||
RuleDiagnostic::new( | ||
rule_category!(), | ||
node.range(), | ||
markup! { | ||
"Use ES6-style `import`s instead of `require`." | ||
}, | ||
) | ||
.note(markup! { | ||
"ES6-style `import` statements are more easily statically analyzable and tree-shakable compared to `require` imports." | ||
}), | ||
) | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
crates/biome_js_analyze/tests/specs/nursery/noRequireImports/invalid.js
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,5 @@ | ||
require('node:fs') | ||
|
||
if (true) { | ||
require('node:fs') | ||
} |
43 changes: 43 additions & 0 deletions
43
crates/biome_js_analyze/tests/specs/nursery/noRequireImports/invalid.js.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,43 @@ | ||
--- | ||
source: crates/biome_js_analyze/tests/spec_tests.rs | ||
expression: invalid.js | ||
--- | ||
# Input | ||
```jsx | ||
require('node:fs') | ||
|
||
if (true) { | ||
require('node:fs') | ||
} | ||
``` | ||
|
||
# Diagnostics | ||
``` | ||
invalid.js:1:1 lint/nursery/noRequireImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Use ES6-style `import`s instead of `require`. | ||
> 1 │ require('node:fs') | ||
│ ^^^^^^^^^^^^^^^^^^ | ||
2 │ | ||
3 │ if (true) { | ||
i ES6-style `import` statements are more easily statically analyzable and tree-shakable compared to `require` imports. | ||
``` | ||
|
||
``` | ||
invalid.js:4:5 lint/nursery/noRequireImports ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ | ||
! Use ES6-style `import`s instead of `require`. | ||
3 │ if (true) { | ||
> 4 │ require('node:fs') | ||
│ ^^^^^^^^^^^^^^^^^^ | ||
5 │ } | ||
i ES6-style `import` statements are more easily statically analyzable and tree-shakable compared to `require` imports. | ||
``` |
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/nursery/noRequireImports/valid.js
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,4 @@ | ||
import 'node:fs' | ||
import { join } from 'node:path' | ||
|
||
import('node:fs/promises') |
11 changes: 11 additions & 0 deletions
11
crates/biome_js_analyze/tests/specs/nursery/noRequireImports/valid.js.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_js_analyze/tests/spec_tests.rs | ||
expression: valid.js | ||
--- | ||
# Input | ||
```jsx | ||
import 'node:fs' | ||
import { join } from 'node:path' | ||
|
||
import('node:fs/promises') | ||
``` |
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.