-
-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(linter/node): implement no-new-require
- Loading branch information
Showing
3 changed files
with
91 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
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,73 @@ | ||
use oxc_ast::AstKind; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_macros::declare_oxc_lint; | ||
use oxc_span::Span; | ||
|
||
use crate::{context::LintContext, rule::Rule, AstNode}; | ||
|
||
fn no_new_require(span: Span) -> OxcDiagnostic { | ||
OxcDiagnostic::warn("Unexpected use of new with require") | ||
.with_label(span) | ||
.with_help("Initialise the constructor separate from the import statement") | ||
} | ||
|
||
#[derive(Debug, Default, Clone)] | ||
pub struct NoNewRequire; | ||
|
||
declare_oxc_lint!( | ||
/// ### What it does | ||
/// | ||
/// Warn about calling `new` on `require`. | ||
/// | ||
/// ### Why is this bad? | ||
/// | ||
/// The `require` function is used to include modules and might return a constructor. As this | ||
/// is not always the case this can be confusing. | ||
/// | ||
/// ### Examples | ||
/// | ||
/// Examples of **incorrect** code for this rule: | ||
/// ```js | ||
/// var appHeader = new require('app-header'); | ||
/// ``` | ||
/// | ||
/// Examples of **correct** code for this rule: | ||
/// ```js | ||
/// var AppHeader = require('app-header'); | ||
/// var appHeader = new AppHeader(); | ||
/// ``` | ||
NoNewRequire, | ||
restriction); | ||
|
||
impl Rule for NoNewRequire { | ||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { | ||
let AstKind::NewExpression(new_expression) = node.kind() else { | ||
return; | ||
}; | ||
|
||
if !new_expression.callee.is_specific_id("require") { | ||
return; | ||
}; | ||
|
||
ctx.diagnostic(no_new_require(new_expression.span)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
use crate::tester::Tester; | ||
|
||
let pass = vec![ | ||
"var appHeader = require('app-header')", | ||
"var AppHeader = new (require('app-header'))", | ||
"var AppHeader = new (require('headers').appHeader)", | ||
"var AppHeader = require('app-header'); var appHeader = new AppHeader();", | ||
]; | ||
|
||
let fail = vec![ | ||
"var appHeader = new require('app-header')", | ||
"var appHeader = new require('headers').appHeader", | ||
]; | ||
|
||
Tester::new(NoNewRequire::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,16 @@ | ||
--- | ||
source: crates/oxc_linter/src/tester.rs | ||
--- | ||
⚠ eslint-plugin-node(no-new-require): Unexpected use of new with require | ||
╭─[no_new_require.tsx:1:17] | ||
1 │ var appHeader = new require('app-header') | ||
· ───────────────────────── | ||
╰──── | ||
help: Initialise the constructor separate from the import statement | ||
|
||
⚠ eslint-plugin-node(no-new-require): Unexpected use of new with require | ||
╭─[no_new_require.tsx:1:17] | ||
1 │ var appHeader = new require('headers').appHeader | ||
· ────────────────────── | ||
╰──── | ||
help: Initialise the constructor separate from the import statement |