Fix handling of different JSX pragmas in Typescript (#7878)

Fixes #7631

Before, if a JSX pragma was imported, the import was removed because
it did not think it was used as a value. This changes it so it knows
that the import is being used as a JSX pragma
This commit is contained in:
Caleb Eby
2018-05-14 15:01:45 -07:00
committed by Henry Zhu
parent 5f58117790
commit 25810d2609
9 changed files with 26 additions and 5 deletions

View File

@@ -56,6 +56,15 @@ require("@babel/core").transform("code", {
plugins: ["@babel/plugin-transform-typescript"]
});
```
## Options
### `jsxPragma`
`string`
Replace the function used when compiling JSX expressions.
This is so that we know that the import is not a type import, and should not be removed
[const_enum]: https://www.typescriptlang.org/docs/handbook/enums.html#const-enums
[namespace]: https://www.typescriptlang.org/docs/handbook/namespaces.html

View File

@@ -20,7 +20,7 @@ interface State {
programPath: any;
}
export default declare(api => {
export default declare((api, { jsxPragma = "React" }) => {
api.assertVersion(7);
return {
@@ -269,11 +269,11 @@ export default declare(api => {
}
}
if (binding.identifier.name != "React") {
if (binding.identifier.name !== jsxPragma) {
return true;
}
// "React" is referenced as a value if there are any JSX elements in the code.
// "React" or the JSX pragma is referenced as a value if there are any JSX elements in the code.
let sourceFileHasJsx = false;
programPath.traverse({
JSXElement() {

View File

@@ -0,0 +1,3 @@
// Don't elide Preact if a JSX element appears somewhere.
import { h, render } from "preact";
<div></div>;

View File

@@ -0,0 +1,3 @@
{
"plugins": ["syntax-jsx", ["transform-typescript", { "jsxPragma": "h" }]]
}

View File

@@ -0,0 +1,3 @@
// Don't elide Preact if a JSX element appears somewhere.
import { h } from "preact";
<div></div>;

View File

@@ -0,0 +1,2 @@
import { FooBar, h } from "preact";
const x: FooBar = 0;

View File

@@ -0,0 +1 @@
const x = 0;

View File

@@ -1,3 +1,3 @@
{
"plugins": ["syntax-jsx"]
"plugins": ["syntax-jsx", "transform-typescript"]
}

View File

@@ -1,3 +1,3 @@
{
"plugins": ["syntax-jsx"]
"plugins": ["syntax-jsx", "transform-typescript"]
}