Skip to content

Commit

Permalink
fix(linter): jsx no undef match scope should check with ancestors (#2027
Browse files Browse the repository at this point in the history
)
  • Loading branch information
xxleyi authored Jan 27, 2024
1 parent 1b5bbb9 commit f32228e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
17 changes: 10 additions & 7 deletions crates/oxc_linter/src/rules/react/jsx_no_undef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ impl Rule for JsxNoUndef {
if ident.name.as_str() == "this" {
return;
}
let has_binding = ctx
.symbols()
.get_scope_id_from_name(&ident.name)
.map_or(false, |scope_id| ctx.scopes().has_binding(scope_id, &ident.name));

if !has_binding {
ctx.diagnostic(JsxNoUndefDiagnostic(ident.name.clone(), ident.span));
let jsx_scope_id = node.scope_id();
for scope_id in ctx.scopes().ancestors(jsx_scope_id) {
if ctx.scopes().has_binding(scope_id, &ident.name) {
return;
}
}
ctx.diagnostic(JsxNoUndefDiagnostic(ident.name.clone(), ident.span));
}
}
}
Expand Down Expand Up @@ -110,6 +109,8 @@ fn test() {
"#,
None,
),
("var App; var React; enum A { App }; React.render(<App />);", None),
("var React; enum A { App }; var App; React.render(<App />);", None),
];

let fail = vec![
Expand All @@ -119,6 +120,8 @@ fn test() {
("var React; React.render(<appp.foo.Bar />);", None),
("var React; React.render(<Foo />);", None),
("var React; Unknown; React.render(<Unknown />)", None),
("var React; { const App = null; }; React.render(<App />);", None),
("var React; enum A { App }; React.render(<App />);", None),
];

Tester::new(JsxNoUndef::NAME, pass, fail).test_and_snapshot();
Expand Down
14 changes: 14 additions & 0 deletions crates/oxc_linter/src/snapshots/jsx_no_undef.snap
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,18 @@ expression: jsx_no_undef
╰────
help: 'Unknown' is not defined.

eslint-plugin-react(jsx-no-undef): Disallow undeclared variables in JSX
╭─[jsx_no_undef.tsx:1:1]
1var React; { const App = null; }; React.render(<App />);
· ───
╰────
help: 'App' is not defined.

eslint-plugin-react(jsx-no-undef): Disallow undeclared variables in JSX
╭─[jsx_no_undef.tsx:1:1]
1var React; enum A { App }; React.render(<App />);
· ───
╰────
help: 'App' is not defined.


0 comments on commit f32228e

Please sign in to comment.