Skip to content

Commit f3889e7

Browse files
author
Esben Sparre Andreasen
committed
JS: simplify isReactImportForJSX to isReactForJSX
1 parent 7209252 commit f3889e7

5 files changed

Lines changed: 24 additions & 19 deletions

File tree

‎javascript/ql/src/Declarations/UnusedVariable.ql‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ predicate isPropertyFilter(UnusedLocal v) {
4949
)
5050
}
5151

52+
predicate hasJsxInScope(UnusedLocal v) {
53+
any(JSXNode n).getParent+() = v.getScope().getScopeElement()
54+
}
55+
5256
/**
53-
* Holds if `v` is an import of React, and there is a JSX element that implicitly
54-
* references it.
55-
*/
56-
predicate isReactImportForJSX(UnusedLocal v) {
57-
exists(JSXNode jsx, TopLevel tl |
58-
jsx.getTopLevel() = tl and
59-
v.getADeclaration().getTopLevel() = tl and
60-
(
61-
v.getName() = "React"
62-
or
57+
* Holds if `v` is a "React" variable that is implicitly used by a JSX element.
58+
*/
59+
predicate isReactForJSX(UnusedLocal v) {
60+
hasJsxInScope(v) and
61+
(
62+
v.getName() = "React"
63+
or
64+
exists(TopLevel tl |
65+
tl = v.getADeclaration().getTopLevel() |
6366
// legacy `@jsx` pragmas
6467
exists(JSXPragma p | p.getTopLevel() = tl | p.getDOMName() = v.getName())
6568
or
@@ -69,12 +72,6 @@ predicate isReactImportForJSX(UnusedLocal v) {
6972
plugin.getJsxFactoryVariableName() = v.getName()
7073
)
7174
)
72-
|
73-
v.getADeclaration() = any(ImportDeclaration imp).getASpecifier().getLocal()
74-
or
75-
exists(VariableDeclarator vd | vd.getBindingPattern() = v.getADeclaration() |
76-
vd.getInit() instanceof Require
77-
)
7875
)
7976
}
8077

@@ -158,7 +155,7 @@ predicate whitelisted(UnusedLocal v) {
158155
// exclude variables used to filter out unwanted properties
159156
isPropertyFilter(v) or
160157
// exclude imports of React that are implicitly referenced by JSX
161-
isReactImportForJSX(v) or
158+
isReactForJSX(v) or
162159
// exclude names that are used as types
163160
exists (VarDecl vd |
164161
v = vd.getVariable() |

‎javascript/ql/test/query-tests/Declarations/UnusedVariable/UnusedVariable.expected‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
| importWithoutPragma.jsx:1:1:1:27 | import ... react'; | Unused import h. |
88
| multi-imports.js:1:1:1:29 | import ... om 'x'; | Unused imports a, b, d. |
99
| multi-imports.js:2:1:2:42 | import ... om 'x'; | Unused imports alphabetically, ordered. |
10-
| require-react-3.js:1:7:1:11 | React | Unused variable React. |
10+
| require-react-in-other-scope.js:2:9:2:13 | React | Unused variable React. |
1111
| typeoftype.ts:9:7:9:7 | y | Unused variable y. |
1212
| unusedShadowed.ts:1:1:1:26 | import ... where'; | Unused import T. |
1313
| unusedShadowed.ts:2:1:2:31 | import ... where'; | Unused import object. |
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var React = x; // OK
2+
(<e/>);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
var { React } = require("probably-react"); // OK, but not yet supported
1+
var { React } = require("probably-react"); // OK
22
(<e/>);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(function() {
2+
var React = require("probably-react"); // NOT OK
3+
})
4+
(function() {
5+
(<e/>);
6+
})

0 commit comments

Comments
 (0)