mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
feat(linter): implement no-new-symbol rule (#201)
This commit is contained in:
parent
0d8d67b593
commit
71e575ab41
5 changed files with 95 additions and 2 deletions
|
|
@ -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,
|
||||
|
|
|
|||
73
crates/oxc_linter/src/rules/no_new_symbol.rs
Normal file
73
crates/oxc_linter/src/rules/no_new_symbol.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
19
crates/oxc_linter/src/snapshots/no_new_symbol.snap
Normal file
19
crates/oxc_linter/src/snapshots/no_new_symbol.snap
Normal file
|
|
@ -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.
|
||||
|
||||
|
|
@ -1 +1 @@
|
|||
Subproject commit 6925ac555c62c67886b86e38c0c6a7dca60b3cb7
|
||||
Subproject commit 032203ea18288b9ae51f0c18dfece03a59555113
|
||||
|
|
@ -1 +1 @@
|
|||
Subproject commit 7f292bf2a19aa14ed69a55e646111af9533d8f1c
|
||||
Subproject commit 7009c76d00218195c905bb0231033e92cc87fbcd
|
||||
Loading…
Reference in a new issue