From 71e575ab413bf19d68c18554d7557963ed332801 Mon Sep 17 00:00:00 2001 From: Xuan <97ssps30212@gmail.com> Date: Thu, 23 Mar 2023 18:29:57 +0800 Subject: [PATCH] feat(linter): implement no-new-symbol rule (#201) --- crates/oxc_linter/src/rules.rs | 1 + crates/oxc_linter/src/rules/no_new_symbol.rs | 73 +++++++++++++++++++ .../src/snapshots/no_new_symbol.snap | 19 +++++ tasks/coverage/babel | 2 +- tasks/coverage/typescript | 2 +- 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 crates/oxc_linter/src/rules/no_new_symbol.rs create mode 100644 crates/oxc_linter/src/snapshots/no_new_symbol.snap diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index b38b4f46f..fa3c86e2f 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -16,6 +16,7 @@ oxc_macros::declare_all_lint_rules! { no_dupe_class_members, no_empty, no_empty_pattern, + no_new_symbol, no_self_compare, no_mixed_operators, no_constant_binary_expression, diff --git a/crates/oxc_linter/src/rules/no_new_symbol.rs b/crates/oxc_linter/src/rules/no_new_symbol.rs new file mode 100644 index 000000000..34a0be8b2 --- /dev/null +++ b/crates/oxc_linter/src/rules/no_new_symbol.rs @@ -0,0 +1,73 @@ +use oxc_ast::{ast::Expression, AstKind, Span}; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::Error, +}; +use oxc_macros::declare_oxc_lint; + +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>) { + if let AstKind::NewExpression(expr) = node.get().kind() + && let Expression::Identifier(ident) = &expr.callee + && ident.name == "Symbol" + && ctx.scope(node).unresolved_references.get("Symbol").is_some() + { + 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(); +} diff --git a/crates/oxc_linter/src/snapshots/no_new_symbol.snap b/crates/oxc_linter/src/snapshots/no_new_symbol.snap new file mode 100644 index 000000000..4552231be --- /dev/null +++ b/crates/oxc_linter/src/snapshots/no_new_symbol.snap @@ -0,0 +1,19 @@ +--- +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:1] + 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:1] + 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. + diff --git a/tasks/coverage/babel b/tasks/coverage/babel index 6925ac555..032203ea1 160000 --- a/tasks/coverage/babel +++ b/tasks/coverage/babel @@ -1 +1 @@ -Subproject commit 6925ac555c62c67886b86e38c0c6a7dca60b3cb7 +Subproject commit 032203ea18288b9ae51f0c18dfece03a59555113 diff --git a/tasks/coverage/typescript b/tasks/coverage/typescript index 7f292bf2a..7009c76d0 160000 --- a/tasks/coverage/typescript +++ b/tasks/coverage/typescript @@ -1 +1 @@ -Subproject commit 7f292bf2a19aa14ed69a55e646111af9533d8f1c +Subproject commit 7009c76d00218195c905bb0231033e92cc87fbcd