mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(linter): no-new false positive when return from arrow expression (#3393)
This commit is contained in:
parent
8a1db6738c
commit
c664c6c264
2 changed files with 27 additions and 7 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
use oxc_ast::AstKind;
|
use oxc_ast::AstKind;
|
||||||
use oxc_diagnostics::OxcDiagnostic;
|
use oxc_diagnostics::OxcDiagnostic;
|
||||||
use oxc_macros::declare_oxc_lint;
|
use oxc_macros::declare_oxc_lint;
|
||||||
use oxc_span::Span;
|
use oxc_span::{GetSpan, Span};
|
||||||
|
|
||||||
use crate::{context::LintContext, rule::Rule, AstNode};
|
use crate::{context::LintContext, rule::Rule, AstNode};
|
||||||
|
|
||||||
|
|
@ -37,11 +37,21 @@ impl Rule for NoNew {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(parent_node) = ctx.nodes().parent_node(node.id()) {
|
let mut ancestors = ctx.nodes().ancestors(node.id()).skip(1);
|
||||||
if matches!(parent_node.kind(), AstKind::ExpressionStatement(_)) {
|
let Some(node_id) = ancestors.next() else { return };
|
||||||
ctx.diagnostic(no_new_diagnostic(expr.span));
|
|
||||||
|
let kind = ctx.nodes().kind(node_id);
|
||||||
|
if matches!(kind, AstKind::ExpressionStatement(_)) {
|
||||||
|
ancestors.next(); // skip `FunctionBody`
|
||||||
|
if let Some(node_id) = ancestors.next() {
|
||||||
|
let kind = ctx.nodes().kind(node_id);
|
||||||
|
if matches!(kind, AstKind::ArrowFunctionExpression(e) if e.expression) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
let span = Span::new(expr.span.start, expr.callee.span().end);
|
||||||
|
ctx.diagnostic(no_new_diagnostic(span));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -49,9 +59,13 @@ impl Rule for NoNew {
|
||||||
fn test() {
|
fn test() {
|
||||||
use crate::tester::Tester;
|
use crate::tester::Tester;
|
||||||
|
|
||||||
let pass = vec!["var a = new Date()", "var a; if (a === new Date()) { a = false; }"];
|
let pass = vec![
|
||||||
|
"var a = new Date()",
|
||||||
|
"var a; if (a === new Date()) { a = false; }",
|
||||||
|
"(() => new Date())",
|
||||||
|
];
|
||||||
|
|
||||||
let fail = vec!["new Date()"];
|
let fail = vec!["new Date()", "(() => { new Date() })"];
|
||||||
|
|
||||||
Tester::new(NoNew::NAME, pass, fail).test_and_snapshot();
|
Tester::new(NoNew::NAME, pass, fail).test_and_snapshot();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,11 @@ expression: no_new
|
||||||
⚠ eslint(no-new): Do not use 'new' for side effects.
|
⚠ eslint(no-new): Do not use 'new' for side effects.
|
||||||
╭─[no_new.tsx:1:1]
|
╭─[no_new.tsx:1:1]
|
||||||
1 │ new Date()
|
1 │ new Date()
|
||||||
· ──────────
|
· ────────
|
||||||
|
╰────
|
||||||
|
|
||||||
|
⚠ eslint(no-new): Do not use 'new' for side effects.
|
||||||
|
╭─[no_new.tsx:1:10]
|
||||||
|
1 │ (() => { new Date() })
|
||||||
|
· ────────
|
||||||
╰────
|
╰────
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue