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

Skip to content

Commit 91943ae

Browse files
committed
JS: support transform-react-jsx plugin
1 parent d7eb4ef commit 91943ae

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

javascript/ql/src/Declarations/UnusedVariable.ql

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,17 @@ predicate isPropertyFilter(UnusedLocal v) {
5656
predicate isReactImportForJSX(UnusedLocal v) {
5757
exists (ImportSpecifier is |
5858
is.getLocal() = v.getADeclaration() and
59-
exists (JSXNode jsx | jsx.getTopLevel() = is.getTopLevel()) |
60-
v.getName() = "React" or
61-
// also accept legacy `@jsx` pragmas
59+
exists (JSXNode jsx | jsx.getTopLevel() = is.getTopLevel())
60+
|
61+
v.getName() = "React"
62+
or
63+
// legacy `@jsx` pragmas
6264
exists (JSXPragma p | p.getTopLevel() = is.getTopLevel() | p.getDOMName() = v.getName())
65+
or
66+
// JSX pragma from a .babelrc file
67+
exists (Babel::TransformReactJsxConfig plugin |
68+
plugin.getConfig().getAContainerInScope() = is.getFile() and
69+
plugin.getJSXFactoryVariableName() = v.getName())
6370
)
6471
}
6572

javascript/ql/src/semmle/javascript/frameworks/Babel.qll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ module Babel {
2828
result.(JSONArray).getElementStringValue(0) = pluginName
2929
)
3030
}
31+
32+
/**
33+
* Gets a file affected by this Babel configuration.
34+
*/
35+
Container getAContainerInScope() {
36+
result = getFile().getParentContainer()
37+
or
38+
result = getAContainerInScope().getAChildContainer() and
39+
// File-relative .babelrc search stops at any package.json or .babelrc file.
40+
not result.getAChildContainer() = any(PackageJSON pkg).getFile() and
41+
not result.getAChildContainer() = any(Config pkg).getFile()
42+
}
3143
}
3244

3345
/**
@@ -152,4 +164,33 @@ module Babel {
152164
result = pathExpr.getConfig().getFolder()
153165
}
154166
}
167+
168+
/**
169+
* A configuration object for the `transform-react-jsx` plugin.
170+
*
171+
* The plugin option `{"pragma": xxx}` specifies a variable name used to instantiate
172+
* JSX elements.
173+
*/
174+
class TransformReactJsxConfig extends JSONArray {
175+
Config cfg;
176+
177+
TransformReactJsxConfig() {
178+
this = cfg.getPluginConfig("transform-react-jsx")
179+
}
180+
181+
/** Gets the Babel configuration object. */
182+
Config getConfig() {
183+
result = cfg
184+
}
185+
186+
/** Gets the options passed to this plugin. */
187+
JSONObject getOptions() {
188+
result = getElementValue(1)
189+
}
190+
191+
/** Gets the name of the variable used to create JSX elements. */
192+
string getJSXFactoryVariableName() {
193+
result = getOptions().getPropStringValue("pragma")
194+
}
195+
}
155196
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| Babelrc/importPragma.jsx:1:1:1:27 | import ... react'; | Unused import h. |
21
| Babelrc/importPragma.jsx:2:1:2:27 | import ... react'; | Unused import q. |
32
| decorated.ts:1:1:1:126 | import ... where'; | Unused import actionHandler. |
43
| decorated.ts:4:10:4:12 | fun | Unused function fun. |

0 commit comments

Comments
 (0)