Skip to content

Commit

Permalink
implement noRequireImports
Browse files Browse the repository at this point in the history
  • Loading branch information
minht11 committed Sep 7, 2024
1 parent e50a3a4 commit 801620d
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 96 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
Contributed by @Conaclos

- Add [nursery/useConsistentMemberAccessibility](https://biomejs.dev/linter/rules/use-consistent-member-accessibility/). Contributed by @seitarof
- Add [nursery/noRequireImports](https://biomejs.dev/linter/rules/no-require-imports/). Contributed by @minht11

#### Enhancements

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

207 changes: 113 additions & 94 deletions crates/biome_configuration/src/analyzer/linter/rules.rs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/biome_diagnostics_categories/src/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,14 @@ define_categories! {
"lint/nursery/noExportedImports": "https://biomejs.dev/linter/rules/no-exported-imports",
"lint/nursery/noImportantInKeyframe": "https://biomejs.dev/linter/rules/no-important-in-keyframe",
"lint/nursery/noInvalidDirectionInLinearGradient": "https://biomejs.dev/linter/rules/no-invalid-direction-in-linear-gradient",
"lint/nursery/noInvalidGridAreas": "https://biomejs.dev/linter/rules/use-consistent-grid-areas",
"lint/nursery/noInvalidPositionAtImportRule": "https://biomejs.dev/linter/rules/no-invalid-position-at-import-rule",
"lint/nursery/noIrregularWhitespace": "https://biomejs.dev/linter/rules/no-irregular-whitespace",
"lint/nursery/noLabelWithoutControl": "https://biomejs.dev/linter/rules/no-label-without-control",
"lint/nursery/noMisplacedAssertion": "https://biomejs.dev/linter/rules/no-misplaced-assertion",
"lint/nursery/noMissingGenericFamilyKeyword": "https://biomejs.dev/linter/rules/no-missing-generic-family-keyword",
"lint/nursery/noReactSpecificProps": "https://biomejs.dev/linter/rules/no-react-specific-props",
"lint/nursery/noRequireImports": "https://biomejs.dev/linter/rules/no-require-imports",
"lint/nursery/noRestrictedImports": "https://biomejs.dev/linter/rules/no-restricted-imports",
"lint/nursery/noRestrictedTypes": "https://biomejs.dev/linter/rules/no-restricted-types",
"lint/nursery/noShorthandPropertyOverrides": "https://biomejs.dev/linter/rules/no-shorthand-property-overrides",
Expand All @@ -159,7 +161,6 @@ define_categories! {
"lint/nursery/useBiomeSuppressionComment": "https://biomejs.dev/linter/rules/use-biome-suppression-comment",
"lint/nursery/useConsistentBuiltinInstantiation": "https://biomejs.dev/linter/rules/use-consistent-new-builtin",
"lint/nursery/useConsistentCurlyBraces": "https://biomejs.dev/linter/rules/use-consistent-curly-braces",
"lint/nursery/noInvalidGridAreas": "https://biomejs.dev/linter/rules/use-consistent-grid-areas",
"lint/nursery/useConsistentMemberAccessibility": "https://biomejs.dev/linter/rules/use-consistent-member-accessibility",
"lint/nursery/useDateNow": "https://biomejs.dev/linter/rules/use-date-now",
"lint/nursery/useDefaultSwitchClause": "https://biomejs.dev/linter/rules/use-default-switch-clause",
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod no_irregular_whitespace;
pub mod no_label_without_control;
pub mod no_misplaced_assertion;
pub mod no_react_specific_props;
pub mod no_require_imports;
pub mod no_restricted_imports;
pub mod no_restricted_types;
pub mod no_static_element_interactions;
Expand Down Expand Up @@ -60,6 +61,7 @@ declare_lint_group! {
self :: no_label_without_control :: NoLabelWithoutControl ,
self :: no_misplaced_assertion :: NoMisplacedAssertion ,
self :: no_react_specific_props :: NoReactSpecificProps ,
self :: no_require_imports :: NoRequireImports ,
self :: no_restricted_imports :: NoRestrictedImports ,
self :: no_restricted_types :: NoRestrictedTypes ,
self :: no_static_element_interactions :: NoStaticElementInteractions ,
Expand Down
74 changes: 74 additions & 0 deletions crates/biome_js_analyze/src/lint/nursery/no_require_imports.rs
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."
}),
)
}
}
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/options.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require('node:fs')

if (true) {
require('node:fs')
}
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.
```
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')
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')
```
7 changes: 6 additions & 1 deletion packages/@biomejs/backend-jsonrpc/src/workspace.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/@biomejs/biome/configuration_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 801620d

Please sign in to comment.