JS: support transform-react-jsx plugin

This commit is contained in:
Asger F
2018-10-26 12:06:53 +01:00
parent d7eb4ef40e
commit 91943ae2cb
3 changed files with 51 additions and 4 deletions

View File

@@ -56,10 +56,17 @@ predicate isPropertyFilter(UnusedLocal v) {
predicate isReactImportForJSX(UnusedLocal v) {
exists (ImportSpecifier is |
is.getLocal() = v.getADeclaration() and
exists (JSXNode jsx | jsx.getTopLevel() = is.getTopLevel()) |
v.getName() = "React" or
// also accept legacy `@jsx` pragmas
exists (JSXNode jsx | jsx.getTopLevel() = is.getTopLevel())
|
v.getName() = "React"
or
// legacy `@jsx` pragmas
exists (JSXPragma p | p.getTopLevel() = is.getTopLevel() | p.getDOMName() = v.getName())
or
// JSX pragma from a .babelrc file
exists (Babel::TransformReactJsxConfig plugin |
plugin.getConfig().getAContainerInScope() = is.getFile() and
plugin.getJSXFactoryVariableName() = v.getName())
)
}

View File

@@ -28,6 +28,18 @@ module Babel {
result.(JSONArray).getElementStringValue(0) = pluginName
)
}
/**
* Gets a file affected by this Babel configuration.
*/
Container getAContainerInScope() {
result = getFile().getParentContainer()
or
result = getAContainerInScope().getAChildContainer() and
// File-relative .babelrc search stops at any package.json or .babelrc file.
not result.getAChildContainer() = any(PackageJSON pkg).getFile() and
not result.getAChildContainer() = any(Config pkg).getFile()
}
}
/**
@@ -152,4 +164,33 @@ module Babel {
result = pathExpr.getConfig().getFolder()
}
}
/**
* A configuration object for the `transform-react-jsx` plugin.
*
* The plugin option `{"pragma": xxx}` specifies a variable name used to instantiate
* JSX elements.
*/
class TransformReactJsxConfig extends JSONArray {
Config cfg;
TransformReactJsxConfig() {
this = cfg.getPluginConfig("transform-react-jsx")
}
/** Gets the Babel configuration object. */
Config getConfig() {
result = cfg
}
/** Gets the options passed to this plugin. */
JSONObject getOptions() {
result = getElementValue(1)
}
/** Gets the name of the variable used to create JSX elements. */
string getJSXFactoryVariableName() {
result = getOptions().getPropStringValue("pragma")
}
}
}

View File

@@ -1,4 +1,3 @@
| Babelrc/importPragma.jsx:1:1:1:27 | import ... react'; | Unused import h. |
| Babelrc/importPragma.jsx:2:1:2:27 | import ... react'; | Unused import q. |
| decorated.ts:1:1:1:126 | import ... where'; | Unused import actionHandler. |
| decorated.ts:4:10:4:12 | fun | Unused function fun. |