/
no_shadow_restricted_names.rs
151 lines (139 loc) · 5.11 KB
/
no_shadow_restricted_names.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use oxc_ast::AstKind;
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::Error,
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{Atom, CompactStr, Span};
use crate::{context::LintContext, globals::PRE_DEFINE_VAR, rule::Rule};
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-shadow-restricted-names): Shadowing of global properties such as 'undefined' is not allowed.")]
#[diagnostic(severity(warning), help("Shadowing of global properties '{0}'."))]
struct NoShadowRestrictedNamesDiagnostic(CompactStr, #[label] pub Span);
#[derive(Debug, Default, Clone)]
pub struct NoShadowRestrictedNames;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow redefine the global variables like 'undefined', 'NaN', 'Infinity', 'eval', 'arguments'.
///
/// ### Why is this bad?
///
///
/// ### Example
/// ```javascript
/// function NaN(){}
///
/// !function(Infinity){};
///
/// var undefined = 5;
///
/// try {} catch(eval){}
/// ```
NoShadowRestrictedNames,
correctness
);
#[inline]
fn check_and_diagnostic(s: &str, span: Span, ctx: &LintContext) {
if PRE_DEFINE_VAR.contains_key(s) {
ctx.diagnostic(NoShadowRestrictedNamesDiagnostic(s.into(), span));
}
}
impl Rule for NoShadowRestrictedNames {
fn run_once(&self, ctx: &LintContext<'_>) {
ctx.symbols().iter().for_each(|symbol_id| {
let name = ctx.symbols().get_name(symbol_id);
if name == "undefined" {
// Allow to declare `undefined` variable but not allow to assign value to it.
let node_id = ctx.semantic().symbols().get_declaration(symbol_id);
if let AstKind::VariableDeclarator(declarator) = ctx.nodes().kind(node_id) {
if declarator.init.is_none()
&& ctx
.symbols()
.get_resolved_references(symbol_id)
.all(|reference| !reference.is_write())
{
return;
}
}
}
check_and_diagnostic(name, ctx.symbols().get_span(symbol_id), ctx);
for span in ctx.symbols().get_redeclare_variables(symbol_id) {
check_and_diagnostic(name, *span, ctx);
}
});
}
}
#[test]
fn test() {
use crate::tester::Tester;
use serde_json::json;
let pass = vec![
("function foo(bar){ var baz; }", None),
("!function foo(bar){ var baz; }", None),
("!function(bar){ var baz; }", None),
("try {} catch(e) {}", None),
("try {} catch(e: undefined) {}", None),
(
"export default function() {}",
Some(json!({
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
}
})),
),
(
"try {} catch {}",
Some(json!({
"parserOptions": { "ecmaVersion": 2019 }
})),
),
("var undefined;", None),
("var undefined;var undefined", None),
(
"let undefined",
Some(json!({
"parserOptions": { "ecmaVersion": 2015 }
})),
),
("var normal, undefined;", None),
("var undefined; doSomething(undefined);", None),
("class foo { undefined() { } }", None),
(
"class foo { #undefined() { } }",
Some(json!({
"parserOptions": { "ecmaVersion": 2019 }
})),
),
("var normal, undefined; var undefined;", None),
];
let fail = vec![
("function NaN(NaN) { var NaN; !function NaN(NaN) { try {} catch(NaN) {} }; }", None),
("function undefined(undefined) { !function undefined(undefined) { try {} catch(undefined) {} }; }", None),
("function Infinity(Infinity) { var Infinity; !function Infinity(Infinity) { try {} catch(Infinity) {} }; }", None),
("function arguments(arguments) { var arguments; !function arguments(arguments) { try {} catch(arguments) {} }; }", None),
("function eval(eval) { var eval; !function eval(eval) { try {} catch(eval) {} }; }", None),
("var eval = (eval) => { var eval; !function eval(eval) { try {} catch(eval) {} }; }", Some(json!({
"parserOptions": {
"ecmaVersion": 6
}
}))),
("var {undefined} = obj; var {a: undefined} = obj; var {a: {b: {undefined}}} = obj; var {a, ...undefined} = obj;", Some(json!({
"parserOptions": {
"ecmaVersion": 9
}
}))),
("var normal, undefined; undefined = 5;", None),
("try {} catch(undefined: undefined) {}", None),
("var [undefined] = [1]", Some(json!({
"parserOptions": {
"ecmaVersion": 6
}
}))),
("class undefined { }", None),
("class foo { undefined(undefined) { } }", None),
("class foo { #undefined(undefined) { } }", None),
];
Tester::new(NoShadowRestrictedNames::NAME, pass, fail).test_and_snapshot();
}