-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter/import): add no-dynamic-require rule
Rule Detail: [link](https://github.com/import-js/eslint-plugin-import/blob/v2.29.1/docs/rules/no-dynamic-require.md)
- Loading branch information
Showing
3 changed files
with
208 additions
and
0 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
127 changes: 127 additions & 0 deletions
127
crates/oxc_linter/src/rules/import/no_dynamic_require.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,127 @@ | ||
use oxc_ast::{ast::Expression, AstKind}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::{GetSpan, Span}; | ||
|
||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
fn no_dnyamic_require_diagnostic(span: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("Expected a literal string or immutable template literal") | ||
.with_help("Replace the argument with a literal string or immutable template literal") | ||
.with_label(span) | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoDynamicRequire { | ||
esmodule: bool, | ||
} | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// The require method from CommonJS is used to import modules from different files. Unlike the | ||
/// ES6 import syntax, it can be given expressions that will be resolved at runtime. While this | ||
/// is sometimes necessary and useful, in most cases it isn't. Using expressions (for instance, | ||
/// concatenating a path and variable) as the argument makes it harder for tools to do static | ||
/// code analysis, or to find where in the codebase a module is used. | ||
/// | ||
/// ### Example | ||
/// ```javascript | ||
/// // Bad | ||
/// require(name); | ||
/// require(`../${name}`); | ||
/// // Good | ||
/// require('../name'); | ||
/// require(`../name`); | ||
/// ``` | ||
NoDynamicRequire, | ||
restriction, | ||
); | ||
|
||
impl Rule for NoDynamicRequire { | ||
fn from_configuration(value: serde_json::Value) -> Self { | ||
let esmodule = value | ||
.get(0) | ||
.and_then(|config| config.get("esmodule")) | ||
.and_then(serde_json::Value::as_bool) | ||
.unwrap_or(false); | ||
|
||
Self { esmodule } | ||
} | ||
|
||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
match node.kind() { | ||
AstKind::ImportExpression(import) => { | ||
if self.esmodule && !is_static_value(&import.source) { | ||
ctx.diagnostic(no_dnyamic_require_diagnostic(import.source.span())); | ||
} | ||
} | ||
AstKind::CallExpression(call) => { | ||
if call.arguments.is_empty() { | ||
return; | ||
} | ||
|
||
if !call.callee.is_specific_id("require") { | ||
return; | ||
} | ||
|
||
let Some(expr) = &call.arguments[0].as_expression() else { | ||
return; | ||
}; | ||
|
||
if !is_static_value(expr) { | ||
ctx.diagnostic(no_dnyamic_require_diagnostic(call.callee.span())); | ||
} | ||
} | ||
_ => {} | ||
}; | ||
} | ||
} | ||
|
||
fn is_static_value(expr: &Expression) -> bool { | ||
expr.is_string_literal() && expr.is_immutable_value() | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
use serde_json::json; | ||
|
||
let pass = vec![ | ||
(r#"import _ from "lodash""#, None), | ||
(r#"require("foo")"#, None), | ||
("require(`foo`)", None), | ||
(r#"require("./foo")"#, None), | ||
(r#"require("@scope/foo")"#, None), | ||
("require()", None), | ||
(r#"require("./foo", "bar" + "okay")"#, None), | ||
(r#"var foo = require("foo")"#, None), | ||
("var foo = require(`foo`)", None), | ||
(r#"var foo = require("./foo")"#, None), | ||
(r#"var foo = require("@scope/foo")"#, None), | ||
(r#"import("foo")"#, Some(json!([{ "esmodule": true }]))), | ||
("import(`foo`)", Some(json!([{ "esmodule": true }]))), | ||
(r#"import("./foo")"#, Some(json!([{ "esmodule": true }]))), | ||
(r#"import("@scope/foo")"#, Some(json!([{ "esmodule": true }]))), | ||
(r#"var foo = import("foo")"#, Some(json!([{ "esmodule": true }]))), | ||
("var foo = import(`foo`)", Some(json!([{ "esmodule": true }]))), | ||
(r#"var foo = import("./foo")"#, Some(json!([{ "esmodule": true }]))), | ||
(r#"var foo = import("@scope/foo")"#, Some(json!([{ "esmodule": true }]))), | ||
]; | ||
|
||
let fail = vec![ | ||
(r#"require("../" + name)"#, None), | ||
("require(`../${name}`)", None), | ||
("require(name)", None), | ||
("require(name())", None), | ||
("require(`foo${x}`)", None), | ||
("var foo = require(`foo${x}`)", None), | ||
(r#"require(name + "foo", "bar")"#, Some(json!([{ "esmodule": true }]))), | ||
(r#"import("../" + "name")"#, Some(json!([{ "esmodule": true }]))), | ||
("import(`../${name}`)", Some(json!([{ "esmodule": true }]))), | ||
("import(name)", Some(json!([{ "esmodule": true }]))), | ||
("import(name())", Some(json!([{ "esmodule": true }]))), | ||
]; | ||
|
||
Tester::new(NoDynamicRequire::NAME, pass, fail).test_and_snapshot(); | ||
} |
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,79 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:1] | ||
1 │ require("../" + name) | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:1] | ||
1 │ require(`../${name}`) | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:1] | ||
1 │ require(name) | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:1] | ||
1 │ require(name()) | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:1] | ||
1 │ require(`foo${x}`) | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:11] | ||
1 │ var foo = require(`foo${x}`) | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:1] | ||
1 │ require(name + "foo", "bar") | ||
· ─────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:8] | ||
1 │ import("../" + "name") | ||
· ────────────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:8] | ||
1 │ import(`../${name}`) | ||
· ──────────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:8] | ||
1 │ import(name) | ||
· ──── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal | ||
|
||
⚠ eslint-plugin-import(no-dynamic-require): Expected a literal string or immutable template literal | ||
╭─[no_dynamic_require.tsx:1:8] | ||
1 │ import(name()) | ||
· ────── | ||
╰──── | ||
help: Replace the argument with a literal string or immutable template literal |