Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72d5074

Browse files
committed
fix(linter): false positive in eslint/no-redeclare (#10402)
closes #10396 - Fix the false positive in `typescript` - Align with `eslint` -- set the default value of `builtinGlobals` to `true`
1 parent f58cf43 commit 72d5074

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

crates/oxc_linter/src/rules/eslint/no_redeclare.rs

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,29 +66,53 @@ impl Rule for NoRedeclare {
6666
.get(0)
6767
.and_then(|config| config.get("builtinGlobals"))
6868
.and_then(serde_json::Value::as_bool)
69-
.unwrap_or(false);
69+
.unwrap_or(true);
7070

7171
Self { built_in_globals }
7272
}
7373

7474
fn run_on_symbol(&self, symbol_id: SymbolId, ctx: &LintContext) {
7575
let name = ctx.scoping().symbol_name(symbol_id);
76+
let decl_span = ctx.scoping().symbol_span(symbol_id);
7677
let is_builtin = self.built_in_globals
7778
&& (GLOBALS["builtin"].contains_key(name) || ctx.globals().is_enabled(name));
7879

79-
let decl_span = ctx.scoping().symbol_span(symbol_id);
80-
8180
if is_builtin {
8281
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, decl_span));
8382
}
8483

85-
for window in ctx.scoping().symbol_redeclarations(symbol_id).windows(2) {
86-
let first = &window[0];
87-
let second = &window[1];
84+
if ctx.source_type().is_typescript() {
85+
let mut iter = ctx.scoping().symbol_redeclarations(symbol_id).iter().filter(|rd| {
86+
if is_builtin {
87+
if rd.span != decl_span {
88+
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, rd.span));
89+
}
90+
return false;
91+
}
92+
if rd.flags.is_function() {
93+
let node = ctx.nodes().get_node(rd.declaration);
94+
if let Some(func) = node.kind().as_function() {
95+
return !func.is_ts_declare_function();
96+
}
97+
}
98+
true
99+
});
100+
101+
if let Some(first) = iter.next() {
102+
iter.fold(first, |prev, next| {
103+
ctx.diagnostic(no_redeclare_diagnostic(name, prev.span, next.span));
104+
next
105+
});
106+
}
107+
108+
return;
109+
}
110+
111+
for windows in ctx.scoping().symbol_redeclarations(symbol_id).windows(2) {
88112
if is_builtin {
89-
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, second.span));
113+
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(name, windows[1].span));
90114
} else {
91-
ctx.diagnostic(no_redeclare_diagnostic(name, first.span, second.span));
115+
ctx.diagnostic(no_redeclare_diagnostic(name, windows[0].span, windows[1].span));
92116
}
93117
}
94118
}
@@ -122,6 +146,9 @@ fn test() {
122146
("var self = 1", Some(serde_json::json!([{ "builtinGlobals": false }]))),
123147
("var globalThis = foo", Some(serde_json::json!([{ "builtinGlobals": false }]))),
124148
("var globalThis = foo", Some(serde_json::json!([{ "builtinGlobals": false }]))),
149+
// Issue: <https://github.com/oxc-project/oxc/issues/10396>
150+
("export function foo(): void; export function foo() { }", None),
151+
("function foo(arg: string): void; function foo(arg: number): any {}", None),
125152
];
126153

127154
let fail = vec![
@@ -139,31 +166,21 @@ fn test() {
139166
("class C { static { var a; { var a; } } }", None),
140167
("class C { static { { var a; } var a; } }", None),
141168
("class C { static { { var a; } { var a; } } }", None),
142-
(
143-
"var Object = 0; var Object = 0; var globalThis = 0;",
144-
Some(serde_json::json!([{ "builtinGlobals": true }])),
145-
),
146-
(
147-
"var a; var {a = 0, b: Object = 0} = {};",
148-
Some(serde_json::json!([{ "builtinGlobals": true }])),
149-
),
150-
(
151-
"var a; var {a = 0, b: globalThis = 0} = {};",
152-
Some(serde_json::json!([{ "builtinGlobals": true }])),
153-
),
169+
("var Object = 0; var Object = 0; var globalThis = 0;", None),
170+
("var a; var {a = 0, b: Object = 0} = {};", None),
171+
("var a; var {a = 0, b: globalThis = 0} = {};", None),
154172
("function f() { var a; var a; }", None),
155173
("function f(a, b = 1) { var a; var b;}", None),
156174
("function f() { var a; if (test) { var a; } }", None),
157175
("for (var a, a;;);", None),
176+
// Issue: <https://github.com/oxc-project/oxc/issues/10396>
177+
("export function undefined(): void; export function undefined() { }", None),
178+
("type foo = 1; export function foo(): void; export function foo() { }", None),
158179
];
159180

160181
Tester::new(NoRedeclare::NAME, NoRedeclare::PLUGIN, pass, fail).test_and_snapshot();
161182

162-
let fail = vec![(
163-
"var foo;",
164-
Some(serde_json::json!([{ "builtinGlobals": true }])),
165-
Some(serde_json::json!({ "globals": { "foo": false }})),
166-
)];
183+
let fail = vec![("var foo;", None, Some(serde_json::json!({ "globals": { "foo": false }})))];
167184

168185
Tester::new(NoRedeclare::NAME, NoRedeclare::PLUGIN, vec![], fail).test();
169186
}

crates/oxc_linter/src/snapshots/eslint_no_redeclare.snap

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,23 @@ source: crates/oxc_linter/src/tester.rs
208208
· │ ╰── It can not be redeclare here.
209209
· ╰── 'a' is already defined.
210210
╰────
211+
212+
eslint(no-redeclare): 'undefined' is already defined as a built-in global variable.
213+
╭─[no_redeclare.tsx:1:17]
214+
1export function undefined(): void; export function undefined() { }
215+
· ─────────
216+
╰────
217+
218+
eslint(no-redeclare): 'undefined' is already defined as a built-in global variable.
219+
╭─[no_redeclare.tsx:1:52]
220+
1export function undefined(): void; export function undefined() { }
221+
· ─────────
222+
╰────
223+
224+
eslint(no-redeclare): 'foo' is already defined.
225+
╭─[no_redeclare.tsx:1:6]
226+
1type foo = 1; export function foo(): void; export function foo() { }
227+
· ─┬─ ─┬─
228+
· │ ╰── It can not be redeclare here.
229+
· ╰── 'foo' is already defined.
230+
╰────

0 commit comments

Comments
 (0)