/
no_redeclare.rs
180 lines (165 loc) · 6.97 KB
/
no_redeclare.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use oxc_ast::{
ast::{BindingIdentifier, BindingPatternKind},
AstKind,
};
use oxc_diagnostics::{
miette::{self, Diagnostic},
thiserror::{self, Error},
};
use oxc_macros::declare_oxc_lint;
use oxc_span::{CompactString, Span};
use crate::{context::LintContext, rule::Rule};
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-redeclare): '{0}' is already defined.")]
#[diagnostic(severity(warning))]
struct NoRedeclareDiagnostic(
CompactString,
#[label("'{0}' is already defined.")] pub Span,
#[label("It can not be redeclare here.")] pub Span,
);
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-redeclare): '{0}' is already defined as a built-in global variable.")]
#[diagnostic(severity(warning))]
struct NoRedeclareAsBuiltiInDiagnostic(
CompactString,
#[label("'{0}' is already defined as a built-in global variable.")] pub Span,
);
#[derive(Debug, Error, Diagnostic)]
#[error("eslint(no-redeclare): '{0}' is already defined by a variable declaration.")]
#[diagnostic(severity(warning))]
struct NoRedeclareBySyntaxDiagnostic(
CompactString,
#[label("'{0}' is already defined by a variable declaration.")] pub Span,
#[label("It cannot be redeclared here.")] pub Span,
);
#[derive(Debug, Default, Clone)]
pub struct NoRedeclare {
built_in_globals: bool,
}
declare_oxc_lint!(
/// ### What it does
///
/// Disallow variable redeclaration
///
/// ### Why is this bad?
///
/// n JavaScript, it’s possible to redeclare the same variable name using var. This can lead to confusion as to where the variable is actually declared and initialized.
///
/// ### Example
/// ```javascript
/// var a = 3;
/// var a = 10;
/// ```
NoRedeclare,
pedantic
);
impl Rule for NoRedeclare {
fn from_configuration(value: serde_json::Value) -> Self {
let built_in_globals = value
.get(0)
.and_then(|config| config.get("builtinGlobals"))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false);
Self { built_in_globals }
}
fn run_once(&self, ctx: &LintContext) {
let symbol_table = ctx.semantic().symbols();
for symbol_id in ctx.symbols().iter() {
let decl = symbol_table.get_declaration(symbol_id);
let symbol_name = symbol_table.get_name(symbol_id);
match ctx.nodes().kind(decl) {
AstKind::VariableDeclarator(var) => {
if let BindingPatternKind::BindingIdentifier(ident) = &var.id.kind {
if symbol_name == ident.name.as_str() {
for span in ctx.symbols().get_redeclare_variables(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
}
}
}
AstKind::FormalParameter(param) => {
if let BindingPatternKind::BindingIdentifier(ident) = ¶m.pattern.kind {
if symbol_name == ident.name.as_str() {
for span in ctx.symbols().get_redeclare_variables(symbol_id) {
self.report_diagnostic(ctx, *span, ident);
}
}
}
}
_ => {}
}
}
}
}
impl NoRedeclare {
fn report_diagnostic(&self, ctx: &LintContext, span: Span, ident: &BindingIdentifier) {
if self.built_in_globals && ctx.env_contains_var(&ident.name) {
ctx.diagnostic(NoRedeclareAsBuiltiInDiagnostic(
ident.name.to_compact_string(),
ident.span,
));
} else {
ctx.diagnostic(NoRedeclareDiagnostic(ident.name.to_compact_string(), ident.span, span));
}
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
("var a = 3; var b = function() { var a = 10; };", None),
("var a = 3; a = 10;", None),
("if (true) {\n let b = 2;\n} else { \nlet b = 3;\n}", None),
("var a; class C { static { var a; } }", None),
("class C { static { var a; } } var a; ", None),
("function a(){} class C { static { var a; } }", None),
("var a; class C { static { function a(){} } }", None),
("class C { static { var a; } static { var a; } }", None),
("class C { static { function a(){} } static { function a(){} } }", None),
("class C { static { var a; { function a(){} } } }", None),
("class C { static { function a(){}; { function a(){} } } }", None),
("class C { static { var a; { let a; } } }", None),
("class C { static { let a; { let a; } } }", None),
("class C { static { { let a; } { let a; } } }", None),
("var Object = 0;", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var Object = 0;", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var Object = 0;", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var top = 0;", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var top = 0;", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var top = 0;", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var self = 1", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var globalThis = foo", Some(serde_json::json!([{ "builtinGlobals": false }]))),
("var globalThis = foo", Some(serde_json::json!([{ "builtinGlobals": false }]))),
];
let fail = vec![
("switch(foo) { case a: var b = 3;\ncase b: var b = 4}", None),
("var a = 3; var a = 10;", None),
("var a = {}; var a = [];", None),
("var a; function a() {}", None),
("function a() {} function a() {}", None),
("var a = function() { }; var a = function() { }", None),
("var a = function() { }; var a = new Date();", None),
("var a = 3; var a = 10; var a = 15;", None),
("var a; var a;", None),
("export var a; var a;", None),
("class C { static { var a; var a; } }", None),
("class C { static { var a; { var a; } } }", None),
("class C { static { { var a; } var a; } }", None),
("class C { static { { var a; } { var a; } } }", None),
// ("var Object = 0;", Some(serde_json::json!([{ "builtinGlobals": true }]))),
(
"var a; var {a = 0, b: Object = 0} = {};",
Some(serde_json::json!([{ "builtinGlobals": true }])),
),
// ("var globalThis = 0;", Some(serde_json::json!([{ "builtinGlobals": true }]))),
(
"var a; var {a = 0, b: globalThis = 0} = {};",
Some(serde_json::json!([{ "builtinGlobals": true }])),
),
("function f() { var a; var a; }", None),
("function f(a) { var a; }", None),
("function f() { var a; if (test) { var a; } }", None),
("for (var a, a;;);", None),
];
Tester::new(NoRedeclare::NAME, pass, fail).test_and_snapshot();
}