-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathno_new_wrappers.rs
112 lines (102 loc) · 3.38 KB
/
no_new_wrappers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use oxc_ast::{ast::Expression, 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_wrappers_diagnostic(builtin_name: &str, new_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Do not use `{builtin_name}` as a constructor"))
.with_help("Remove the `new` operator.")
.with_label(new_span)
}
#[derive(Debug, Default, Clone)]
pub struct NoNewWrappers;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow `new` operators with the `String`, `Number`, and `Boolean` objects
///
/// ### Why is this bad?
///
/// The first problem is that primitive wrapper objects are, in fact,
/// objects. That means typeof will return `"object"` instead of `"string"`,
/// `"number"`, or `"boolean"`. The second problem comes with boolean
/// objects. Every object is truthy, that means an instance of `Boolean`
/// always resolves to `true` even when its actual value is `false`.
///
/// https://eslint.org/docs/latest/rules/no-new-wrappers
///
/// ### Example
///
/// Examples of **incorrect** code for this rule:
/// ```js
/// var stringObject = new String('Hello world');
/// var numberObject = new Number(33);
/// var booleanObject = new Boolean(false);
/// ```
///
/// Examples of **correct** code for this rule:
/// ```js
/// var stringObject = 'Hello world';
/// var stringObject2 = String(value);
/// var numberObject = Number(value);
/// var booleanObject = Boolean(value);
/// ```
NoNewWrappers,
pedantic,
pending
);
impl Rule for NoNewWrappers {
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 == "String" || ident.name == "Number" || ident.name == "Boolean")
&& ctx.semantic().is_reference_to_global_variable(ident)
{
ctx.diagnostic(no_new_wrappers_diagnostic(ident.name.as_str(), expr.span));
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
"var a = new Object();",
"var a = String('test'), b = String.fromCharCode(32);",
"function test(Number) { return new Number; }",
r#"
import String from "./string";
const str = new String(42);
"#,
"
if (foo) {
result = new Boolean(bar);
} else {
var Boolean = CustomBoolean;
}
",
// Disabled because the eslint-test uses languageOptions: { globals: { String: "off" } }
// "new String()",
// Disabled as the global option from the eslint-test does not work
// "
// /* global Boolean:off */
// assert(new Boolean);
// ",
];
let fail = vec![
"var a = new String('hello');",
"var a = new Number(10);",
"var a = new Boolean(false);",
"
const a = new String('bar');
{
const String = CustomString;
const b = new String('foo');
}
",
];
Tester::new(NoNewWrappers::NAME, pass, fail).test_and_snapshot();
}