-
-
Notifications
You must be signed in to change notification settings - Fork 859
feat(linter): add import/no-unassigned-import rule #10970
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
camc314
merged 6 commits into
oxc-project:main
from
huangtiandi1999:feat/linter/import/no-unassigned-import
May 15, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
748a916
implement
huangtiandi1999 7f02cca
use GlobSet
huangtiandi1999 ed86497
solve conflict
huangtiandi1999 574126d
add test case
huangtiandi1999 97b41c0
update doc comment
camc314 11df989
Merge branch 'main' into feat/linter/import/no-unassigned-import
camc314 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
175 changes: 175 additions & 0 deletions
175
crates/oxc_linter/src/rules/import/no_unassigned_import.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,175 @@ | ||
| use globset::{Glob, GlobSet, GlobSetBuilder}; | ||
| use oxc_ast::{ | ||
| AstKind, | ||
| ast::{Argument, Expression}, | ||
| }; | ||
| use oxc_diagnostics::OxcDiagnostic; | ||
| use oxc_macros::declare_oxc_lint; | ||
| use oxc_span::{CompactStr, Span}; | ||
| use serde_json::Value; | ||
|
|
||
| use crate::{AstNode, context::LintContext, rule::Rule}; | ||
|
|
||
| fn no_unassigned_import_diagnostic(span: Span, msg: &str) -> OxcDiagnostic { | ||
| OxcDiagnostic::warn(msg.to_string()) | ||
| .with_help("Consider assigning the import to a variable or removing it if it's unused.") | ||
| .with_label(span) | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct NoUnassignedImport(Box<NoUnassignedImportConfig>); | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct NoUnassignedImportConfig { | ||
| globs: GlobSet, | ||
| } | ||
|
|
||
| impl std::ops::Deref for NoUnassignedImport { | ||
| type Target = NoUnassignedImportConfig; | ||
|
|
||
| fn deref(&self) -> &Self::Target { | ||
| &self.0 | ||
| } | ||
| } | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// This rule aims to remove modules with side-effects by reporting when a module is imported but not assigned. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// With both CommonJS' require and the ES6 modules' import syntax, | ||
| /// it is possible to import a module but not to use its result. | ||
| /// This can be done explicitly by not assigning the module to a variable. | ||
| /// Doing so can mean either of the following things: | ||
| /// * The module is imported but not used | ||
| /// * The module has side-effects. Having side-effects, | ||
| /// makes it hard to know whether the module is actually used or can be removed. | ||
| /// It can also make it harder to test or mock parts of your application. | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```js | ||
| /// import 'should' | ||
| /// require('should') | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```js | ||
| /// import _ from 'foo' | ||
| /// import _, {foo} from 'foo' | ||
| /// import _, {foo as bar} from 'foo' | ||
| /// const _ = require('foo') | ||
| /// const {foo} = require('foo') | ||
| /// const {foo: bar} = require('foo') | ||
| /// bar(require('foo')) | ||
| /// ``` | ||
| NoUnassignedImport, | ||
| import, | ||
| suspicious, | ||
| ); | ||
|
|
||
| fn build_globset(patterns: Vec<CompactStr>) -> Result<GlobSet, globset::Error> { | ||
| if patterns.is_empty() { | ||
| return Ok(GlobSet::empty()); | ||
| } | ||
| let mut builder = GlobSetBuilder::new(); | ||
| for pattern in patterns { | ||
| let pattern_str = pattern.as_str(); | ||
| builder.add(Glob::new(pattern_str)?); | ||
| } | ||
| builder.build() | ||
| } | ||
|
|
||
| impl Rule for NoUnassignedImport { | ||
| fn from_configuration(value: Value) -> Self { | ||
| let obj = value.get(0); | ||
| let allow = obj | ||
| .and_then(|v| v.get("allow")) | ||
| .and_then(Value::as_array) | ||
| .map(|v| v.iter().filter_map(Value::as_str).map(CompactStr::from).collect()) | ||
| .unwrap_or_default(); | ||
| Self(Box::new(NoUnassignedImportConfig { globs: build_globset(allow).unwrap_or_default() })) | ||
| } | ||
| fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
| match node.kind() { | ||
| AstKind::ImportDeclaration(import_decl) => { | ||
| if import_decl.specifiers.is_some() { | ||
| return; | ||
| } | ||
| let source_str = import_decl.source.value.as_str(); | ||
| if !self.globs.is_match(source_str) { | ||
| ctx.diagnostic(no_unassigned_import_diagnostic( | ||
| import_decl.span, | ||
| "Imported module should be assigned", | ||
| )); | ||
| } | ||
| } | ||
| AstKind::ExpressionStatement(statement) => { | ||
| let Expression::CallExpression(call_expr) = &statement.expression else { | ||
| return; | ||
| }; | ||
| if !call_expr.is_require_call() { | ||
| return; | ||
| } | ||
| let first_arg = &call_expr.arguments[0]; | ||
| let Argument::StringLiteral(source_str) = first_arg else { | ||
| return; | ||
| }; | ||
| if !self.globs.is_match(source_str.value.as_str()) { | ||
| ctx.diagnostic(no_unassigned_import_diagnostic( | ||
| call_expr.span, | ||
| "A `require()` style import is forbidden.", | ||
| )); | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test() { | ||
| use crate::tester::Tester; | ||
| use serde_json::json; | ||
|
|
||
| let pass = vec![ | ||
| ("import _ from 'foo'", None), | ||
| ("import foo from 'foo'", None), | ||
| ("import foo, { bar } from 'foo'", None), | ||
| ("import * as _ from 'foo'", None), | ||
| ("require('lodash')()", None), | ||
| ("require('lodash').foo", None), | ||
| ("require('lodash').foo()", None), | ||
| ("const _ = require('./')", None), | ||
| ("bar(require('foo'))", None), | ||
| ("const [a, b] = require('lodash')", None), | ||
| ("import 'app.css'", Some(json!([{ "allow": ["**/*.css"]}]))), | ||
| ("import 'app.css'", Some(json!([{ "allow": ["*.css"]}]))), | ||
| ("import './app.css'", Some(json!([{ "allow": ["**/*.css"]}]))), | ||
| ("import '../dist/app.css'", Some(json!([{ "allow": ["**/*.css"]}]))), | ||
| ("import '../dist/app.js'", Some(json!([{ "allow": ["**/dist/**"]}]))), | ||
| ("import 'foo/bar'", Some(json!([{ "allow": ["foo/**"]}]))), | ||
| ("import 'foo/bar'", Some(json!([{ "allow": ["foo/bar"]}]))), | ||
| ("import 'babel-register'", Some(json!([{ "allow": ["babel-register"]}]))), | ||
| ("require('./app.css')", Some(json!([{ "allow": ["**/*.css"]}]))), | ||
| ("import './styles/app.css'", Some(json!([{ "allow": ["**/styles/*.css"]}]))), | ||
| ]; | ||
|
|
||
| let fail = vec![ | ||
| ("require('should')", None), | ||
| ("import 'foo'", None), | ||
| ("import './styles/app.css'", Some(json!([{ "allow": ["styles/*.css"]}]))), | ||
| ("import './app.css'", Some(json!([{ "allow": ["**/*.js"]}]))), | ||
| ("import './app.css'", Some(json!([{ "allow": ["**/dir/**"]}]))), | ||
| ("import './app.js'", None), | ||
| ("require('./app.css')", Some(json!([{ "allow": ["**/*.js"]}]))), | ||
| ]; | ||
|
|
||
| Tester::new(NoUnassignedImport::NAME, NoUnassignedImport::PLUGIN, pass, fail) | ||
| .change_rule_path("no-unassigned-import.js") | ||
| .with_import_plugin(true) | ||
| .test_and_snapshot(); | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
crates/oxc_linter/src/snapshots/import_no_unassigned_import.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,51 @@ | ||
| --- | ||
| source: crates/oxc_linter/src/tester.rs | ||
| --- | ||
| ⚠ eslint-plugin-import(no-unassigned-import): A `require()` style import is forbidden. | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ require('should') | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. | ||
|
|
||
| ⚠ eslint-plugin-import(no-unassigned-import): Imported module should be assigned | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ import 'foo' | ||
| · ──────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. | ||
|
|
||
| ⚠ eslint-plugin-import(no-unassigned-import): Imported module should be assigned | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ import './styles/app.css' | ||
| · ───────────────────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. | ||
|
|
||
| ⚠ eslint-plugin-import(no-unassigned-import): Imported module should be assigned | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ import './app.css' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. | ||
|
|
||
| ⚠ eslint-plugin-import(no-unassigned-import): Imported module should be assigned | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ import './app.css' | ||
| · ────────────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. | ||
|
|
||
| ⚠ eslint-plugin-import(no-unassigned-import): Imported module should be assigned | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ import './app.js' | ||
| · ───────────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. | ||
|
|
||
| ⚠ eslint-plugin-import(no-unassigned-import): A `require()` style import is forbidden. | ||
| ╭─[no-unassigned-import.js:1:1] | ||
| 1 │ require('./app.css') | ||
| · ──────────────────── | ||
| ╰──── | ||
| help: Consider assigning the import to a variable or removing it if it's unused. |
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.