-
-
Notifications
You must be signed in to change notification settings - Fork 860
feat(linter): add unicorn/require-module-attributes rule
#17166
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
baevm:unicorn/require-module-attributes
Dec 20, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
07d44f1
feat(linter): add unicorn/require-module-attributes rule
baevm 62a5827
fix clippy warnings
baevm f79d581
[autofix.ci] apply automated fixes
autofix-ci[bot] ad79a88
update lintgen formating
baevm 9f33910
fix copilot comment
baevm cb85250
u
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
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
161 changes: 161 additions & 0 deletions
161
crates/oxc_linter/src/rules/unicorn/require_module_attributes.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,161 @@ | ||
| use oxc_ast::{ | ||
| AstKind, | ||
| ast::{Expression, PropertyKind, WithClause}, | ||
| }; | ||
| use oxc_diagnostics::OxcDiagnostic; | ||
| use oxc_macros::declare_oxc_lint; | ||
| use oxc_span::{GetSpan, Span}; | ||
|
|
||
| use crate::{AstNode, context::LintContext, rule::Rule, utils::is_empty_object_expression}; | ||
|
|
||
| fn require_module_attributes_diagnostic(span: Span, import_type: &str) -> OxcDiagnostic { | ||
| OxcDiagnostic::warn(format!("{import_type} with empty attribute list is not allowed.")) | ||
| .with_label(span) | ||
| } | ||
|
|
||
| #[derive(Debug, Default, Clone)] | ||
| pub struct RequireModuleAttributes; | ||
|
|
||
| declare_oxc_lint!( | ||
| /// ### What it does | ||
| /// | ||
| /// This rule enforces non-empty attribute list in import/export statements and import() expressions. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// | ||
| /// Import attributes are meant to provide metadata about how a module should be loaded | ||
| /// (e.g., `with { type: "json" }`). An empty attribute object provides no information | ||
| /// and should be removed. | ||
| /// | ||
| /// ### Examples | ||
| /// | ||
| /// Examples of **incorrect** code for this rule: | ||
| /// ```js | ||
| /// import foo from 'foo' with {}; | ||
| /// | ||
| /// export { foo } from 'foo' with {}; | ||
| /// | ||
| /// const foo = await import('foo', {}); | ||
| /// | ||
| /// const foo = await import('foo', { with: {} }); | ||
| /// ``` | ||
| /// | ||
| /// Examples of **correct** code for this rule: | ||
| /// ```js | ||
| /// import foo from 'foo'; | ||
| /// | ||
| /// export { foo } from 'foo'; | ||
| /// | ||
| /// const foo = await import('foo'); | ||
| /// | ||
| /// const foo = await import('foo'); | ||
| /// ``` | ||
| RequireModuleAttributes, | ||
| unicorn, | ||
| style, | ||
| pending, | ||
| ); | ||
|
|
||
| impl Rule for RequireModuleAttributes { | ||
| fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
| match node.kind() { | ||
| AstKind::ImportExpression(import_expr) => { | ||
| let Some(options) = &import_expr.options else { return }; | ||
|
|
||
| let Expression::ObjectExpression(obj_expr) = options.get_inner_expression() else { | ||
| return; | ||
| }; | ||
|
|
||
| if obj_expr.properties.is_empty() { | ||
| ctx.diagnostic(require_module_attributes_diagnostic( | ||
| obj_expr.span, | ||
| "import expression", | ||
| )); | ||
| return; | ||
| } | ||
|
|
||
| let empty_with_prop = obj_expr.properties.iter().find_map(|prop| { | ||
| let obj_prop = prop.as_property()?; | ||
| if !obj_prop.method | ||
| && !obj_prop.shorthand | ||
| && !obj_prop.computed | ||
| && obj_prop.kind == PropertyKind::Init | ||
| && obj_prop.key.is_specific_static_name("with") | ||
| && is_empty_object_expression(obj_prop.value.get_inner_expression()) | ||
| { | ||
| Some(obj_prop) | ||
| } else { | ||
| None | ||
| } | ||
| }); | ||
|
|
||
| if let Some(empty_with_prop) = empty_with_prop { | ||
| let span = empty_with_prop.value.span(); | ||
| ctx.diagnostic(require_module_attributes_diagnostic(span, "import expression")); | ||
| } | ||
| } | ||
| AstKind::ImportDeclaration(decl) => { | ||
| check_with_clause(ctx, decl.with_clause.as_deref(), "import statement"); | ||
| } | ||
| AstKind::ExportNamedDeclaration(decl) => { | ||
| check_with_clause(ctx, decl.with_clause.as_deref(), "export statement"); | ||
| } | ||
| AstKind::ExportAllDeclaration(decl) => { | ||
| check_with_clause(ctx, decl.with_clause.as_deref(), "export statement"); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn check_with_clause(ctx: &LintContext, with_clause: Option<&WithClause>, import_type: &str) { | ||
| if let Some(with_clause) = with_clause | ||
| && with_clause.with_entries.is_empty() | ||
| { | ||
| ctx.diagnostic(require_module_attributes_diagnostic(with_clause.span, import_type)); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test() { | ||
| use crate::tester::Tester; | ||
|
|
||
| let pass = vec![ | ||
| r#"import foo from "foo""#, | ||
| r#"export {foo} from "foo""#, | ||
| r#"export * from "foo""#, | ||
| r#"import foo from "foo" with {type: "json"}"#, | ||
| r#"export {foo} from "foo" with {type: "json"}"#, | ||
| r#"export * from "foo" with {type: "json"}"#, | ||
| "export {}", | ||
| r#"import("foo")"#, | ||
| r#"import("foo", {unknown: "unknown"})"#, | ||
| r#"import("foo", {with: {type: "json"}})"#, | ||
| r#"not_import("foo", {})"#, | ||
| r#"not_import("foo", {with:{}})"#, | ||
| ]; | ||
|
|
||
| let fail = vec![ | ||
| r#"import "foo" with {}"#, | ||
| r#"import foo from "foo" with {}"#, | ||
| r#"export {foo} from "foo" with {}"#, | ||
| r#"export * from "foo" with {}"#, | ||
| r#"export * from "foo"with{}"#, | ||
| r#"export * from "foo"/* comment 1 */with/* comment 2 */{/* comment 3 */}/* comment 4 */"#, | ||
| r#"import("foo", {})"#, | ||
| r#"import("foo", (( {} )))"#, | ||
| r#"import("foo", {},)"#, | ||
| r#"import("foo", {with:{},},)"#, | ||
| r#"import("foo", {with:{}, unknown:"unknown"},)"#, | ||
| r#"import("foo", {"with":{}, unknown:"unknown"},)"#, | ||
| r#"import("foo", {unknown:"unknown", with:{}, },)"#, | ||
| r#"import("foo", {unknown:"unknown", with:{} },)"#, | ||
| r#"import("foo", {unknown:"unknown", with:{}, unknown2:"unknown2", },)"#, | ||
| r#"import("foo"/* comment 1 */, /* comment 2 */{/* comment 3 */}/* comment 4 */,/* comment 5 */)"#, | ||
| r#"import("foo", {/* comment 1 */"with"/* comment 2 */:/* comment 3 */{/* comment 4 */}, }/* comment 5 */,)"#, | ||
| r#"import("foo", {with: (({}))})"#, | ||
| ]; | ||
|
|
||
| Tester::new(RequireModuleAttributes::NAME, RequireModuleAttributes::PLUGIN, pass, fail) | ||
| .test_and_snapshot(); | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
crates/oxc_linter/src/snapshots/unicorn_require_module_attributes.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,110 @@ | ||
| --- | ||
| source: crates/oxc_linter/src/tester.rs | ||
| --- | ||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import statement with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:19] | ||
| 1 │ import "foo" with {} | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import statement with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:28] | ||
| 1 │ import foo from "foo" with {} | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): export statement with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:30] | ||
| 1 │ export {foo} from "foo" with {} | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): export statement with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:26] | ||
| 1 │ export * from "foo" with {} | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): export statement with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:24] | ||
| 1 │ export * from "foo"with{} | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): export statement with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:54] | ||
| 1 │ export * from "foo"/* comment 1 */with/* comment 2 */{/* comment 3 */}/* comment 4 */ | ||
| · ───────────────── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:15] | ||
| 1 │ import("foo", {}) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:18] | ||
| 1 │ import("foo", (( {} ))) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:15] | ||
| 1 │ import("foo", {},) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:21] | ||
| 1 │ import("foo", {with:{},},) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:21] | ||
| 1 │ import("foo", {with:{}, unknown:"unknown"},) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:23] | ||
| 1 │ import("foo", {"with":{}, unknown:"unknown"},) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:40] | ||
| 1 │ import("foo", {unknown:"unknown", with:{}, },) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:40] | ||
| 1 │ import("foo", {unknown:"unknown", with:{} },) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:40] | ||
| 1 │ import("foo", {unknown:"unknown", with:{}, unknown2:"unknown2", },) | ||
| · ── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:45] | ||
| 1 │ import("foo"/* comment 1 */, /* comment 2 */{/* comment 3 */}/* comment 4 */,/* comment 5 */) | ||
| · ───────────────── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:68] | ||
| 1 │ import("foo", {/* comment 1 */"with"/* comment 2 */:/* comment 3 */{/* comment 4 */}, }/* comment 5 */,) | ||
| · ───────────────── | ||
| ╰──── | ||
|
|
||
| ⚠ eslint-plugin-unicorn(require-module-attributes): import expression with empty attribute list is not allowed. | ||
| ╭─[require_module_attributes.tsx:1:22] | ||
| 1 │ import("foo", {with: (({}))}) | ||
| · ────── | ||
| ╰──── |
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.