JS: simplify isReactImportForJSX to isReactForJSX

This commit is contained in:
Esben Sparre Andreasen
2018-11-28 15:06:18 +01:00
parent 72092529d1
commit f3889e715e
5 changed files with 24 additions and 19 deletions

View File

@@ -49,17 +49,20 @@ predicate isPropertyFilter(UnusedLocal v) {
)
}
predicate hasJsxInScope(UnusedLocal v) {
any(JSXNode n).getParent+() = v.getScope().getScopeElement()
}
/**
* Holds if `v` is an import of React, and there is a JSX element that implicitly
* references it.
*/
predicate isReactImportForJSX(UnusedLocal v) {
exists(JSXNode jsx, TopLevel tl |
jsx.getTopLevel() = tl and
v.getADeclaration().getTopLevel() = tl and
(
v.getName() = "React"
or
* Holds if `v` is a "React" variable that is implicitly used by a JSX element.
*/
predicate isReactForJSX(UnusedLocal v) {
hasJsxInScope(v) and
(
v.getName() = "React"
or
exists(TopLevel tl |
tl = v.getADeclaration().getTopLevel() |
// legacy `@jsx` pragmas
exists(JSXPragma p | p.getTopLevel() = tl | p.getDOMName() = v.getName())
or
@@ -69,12 +72,6 @@ predicate isReactImportForJSX(UnusedLocal v) {
plugin.getJsxFactoryVariableName() = v.getName()
)
)
|
v.getADeclaration() = any(ImportDeclaration imp).getASpecifier().getLocal()
or
exists(VariableDeclarator vd | vd.getBindingPattern() = v.getADeclaration() |
vd.getInit() instanceof Require
)
)
}
@@ -158,7 +155,7 @@ predicate whitelisted(UnusedLocal v) {
// exclude variables used to filter out unwanted properties
isPropertyFilter(v) or
// exclude imports of React that are implicitly referenced by JSX
isReactImportForJSX(v) or
isReactForJSX(v) or
// exclude names that are used as types
exists (VarDecl vd |
v = vd.getVariable() |