feat(linter): eslint/no-new-native-nonconstructor (#3187)

feat(linter): eslint/no-new-native-nonconstructor

closes #3179

remove no new symbol
This commit is contained in:
Boshen 2024-05-07 17:10:17 +08:00 committed by GitHub
parent 7338364219
commit ca9f13f4f2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 112 additions and 91 deletions

View file

@ -85,7 +85,7 @@ mod eslint {
pub mod no_iterator;
pub mod no_loss_of_precision;
pub mod no_mixed_operators;
pub mod no_new_symbol;
pub mod no_new_native_nonconstructor;
pub mod no_new_wrappers;
pub mod no_nonoctal_decimal_escape;
pub mod no_obj_calls;
@ -444,7 +444,6 @@ oxc_macros::declare_all_lint_rules! {
eslint::no_iterator,
eslint::no_loss_of_precision,
eslint::no_mixed_operators,
eslint::no_new_symbol,
eslint::no_new_wrappers,
eslint::no_nonoctal_decimal_escape,
eslint::no_obj_calls,
@ -475,6 +474,7 @@ oxc_macros::declare_all_lint_rules! {
eslint::use_isnan,
eslint::valid_typeof,
eslint::no_await_in_loop,
eslint::no_new_native_nonconstructor,
typescript::adjacent_overload_signatures,
typescript::array_type,
typescript::ban_ts_comment,

View file

@ -0,0 +1,83 @@
use oxc_ast::{ast::Expression, AstKind};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::{self, Error},
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{CompactStr, Span};
use crate::{context::LintContext, rule::Rule, AstNode};
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-new-native-nonconstructor): `{0}` cannot be called as a constructor.")]
#[diagnostic(severity(warning))]
struct NoNewNativeNonconstructorDiagnostic(CompactStr, #[label] pub Span);
#[derive(Debug, Default, Clone)]
pub struct NoNewNativeNonconstructor;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow new operators with global non-constructor functions (Symbol, BigInt)
///
/// ### Why is this bad?
///
/// Both new Symbol and new BigInt throw a type error because they are functions and not classes.
/// It is easy to make this mistake by assuming the uppercase letters indicate classes.
///
/// ### Example
/// ```javascript
/// // throws a TypeError
/// let foo = new Symbol("foo");
///
/// // throws a TypeError
/// let result = new BigInt(9007199254740991);
/// ```
NoNewNativeNonconstructor,
correctness,
);
impl Rule for NoNewNativeNonconstructor {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::NewExpression(expr) = node.kind() else { return };
let Expression::Identifier(ident) = &expr.callee else { return };
if matches!(ident.name.as_str(), "Symbol" | "BigInt")
&& ctx.semantic().is_reference_to_global_variable(ident)
{
let start = expr.span.start;
let end = start + 3;
ctx.diagnostic(NoNewNativeNonconstructorDiagnostic(
ident.name.to_compact_str(),
Span::new(start, end),
));
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
"var foo = Symbol('foo');",
"function bar(Symbol) { var baz = new Symbol('baz');}",
"function Symbol() {} new Symbol();",
"new foo(Symbol);",
"new foo(bar, Symbol);",
"var foo = BigInt(9007199254740991);",
"function bar(BigInt) { var baz = new BigInt(9007199254740991);}",
"function BigInt() {} new BigInt();",
"new foo(BigInt);",
"new foo(bar, BigInt);",
];
let fail = vec![
"var foo = new Symbol('foo');",
"function bar() { return function Symbol() {}; } var baz = new Symbol('baz');",
"var foo = new BigInt(9007199254740991);",
"function bar() { return function BigInt() {}; } var baz = new BigInt(9007199254740991);",
];
Tester::new(NoNewNativeNonconstructor::NAME, pass, fail).test_and_snapshot();
}

View file

@ -1,72 +0,0 @@
use oxc_ast::{ast::Expression, AstKind};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode};
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-new-symbol): Disallow new operators with the Symbol object")]
#[diagnostic(
severity(warning),
help(
"Symbol is not intended to be used with the new operator, but to be called as a function. Consider removing the new operator."
)
)]
struct NoNewSymbolDiagnostic(#[label] pub Span);
#[derive(Debug, Default, Clone)]
pub struct NoNewSymbol;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow new operators with the Symbol object
///
///
/// ### Why is this bad?
///
/// Symbol is not intended to be used with the new operator, but to be called as a function.
///
/// ### Example
/// ```javascript
/// var foo = new Symbol('foo');
/// ```
NoNewSymbol,
correctness
);
impl Rule for NoNewSymbol {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::NewExpression(expr) = node.kind() else { return };
let Expression::Identifier(ident) = &expr.callee else { return };
if ident.name == "Symbol" && ctx.semantic().is_reference_to_global_variable(ident) {
let start = expr.span.start;
let end = start + 3;
ctx.diagnostic(NoNewSymbolDiagnostic(Span::new(start, end)));
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
("var foo = Symbol('foo');", None),
("function bar(Symbol) { var baz = new Symbol('baz');}", None),
("function Symbol() {} new Symbol();", None),
("new foo(Symbol);", None),
("new foo(bar, Symbol);", None),
];
let fail = vec![
("var foo = new Symbol('foo');", None),
("function bar() { return function Symbol() {}; } var baz = new Symbol('baz');", None),
];
Tester::new(NoNewSymbol::NAME, pass, fail).test_and_snapshot();
}

View file

@ -0,0 +1,27 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_new_native_nonconstructor
---
⚠ eslint(no-new-native-nonconstructor): `Symbol` cannot be called as a constructor.
╭─[no_new_native_nonconstructor.tsx:1:11]
1 │ var foo = new Symbol('foo');
· ───
╰────
⚠ eslint(no-new-native-nonconstructor): `Symbol` cannot be called as a constructor.
╭─[no_new_native_nonconstructor.tsx:1:59]
1 │ function bar() { return function Symbol() {}; } var baz = new Symbol('baz');
· ───
╰────
⚠ eslint(no-new-native-nonconstructor): `BigInt` cannot be called as a constructor.
╭─[no_new_native_nonconstructor.tsx:1:11]
1 │ var foo = new BigInt(9007199254740991);
· ───
╰────
⚠ eslint(no-new-native-nonconstructor): `BigInt` cannot be called as a constructor.
╭─[no_new_native_nonconstructor.tsx:1:59]
1 │ function bar() { return function BigInt() {}; } var baz = new BigInt(9007199254740991);
· ───
╰────

View file

@ -1,17 +0,0 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_new_symbol
---
⚠ eslint(no-new-symbol): Disallow new operators with the Symbol object
╭─[no_new_symbol.tsx:1:11]
1 │ var foo = new Symbol('foo');
· ───
╰────
help: Symbol is not intended to be used with the new operator, but to be called as a function. Consider removing the new operator.
⚠ eslint(no-new-symbol): Disallow new operators with the Symbol object
╭─[no_new_symbol.tsx:1:59]
1 │ function bar() { return function Symbol() {}; } var baz = new Symbol('baz');
· ───
╰────
help: Symbol is not intended to be used with the new operator, but to be called as a function. Consider removing the new operator.