Retain JSX pragma if defined as a comment (#9095)

This commit is contained in:
Dylan Staley 2019-01-26 13:36:10 -08:00 committed by Brian Ng
parent 7dc157f9be
commit 03230eaa9c
4 changed files with 25 additions and 0 deletions

View File

@ -25,6 +25,8 @@ const PARSED_PARAMS = new WeakSet();
export default declare((api, { jsxPragma = "React" }) => {
api.assertVersion(7);
const JSX_ANNOTATION_REGEX = /\*?\s*@jsx\s+([^\s]+)/;
return {
name: "transform-typescript",
inherits: syntaxTypeScript,
@ -38,6 +40,17 @@ export default declare((api, { jsxPragma = "React" }) => {
Program(path, state: State) {
state.programPath = path;
const { file } = state;
if (file.ast.comments) {
for (const comment of (file.ast.comments: Array<Object>)) {
const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value);
if (jsxMatches) {
jsxPragma = jsxMatches[1];
}
}
}
// remove type imports
for (const stmt of path.get("body")) {
if (t.isImportDeclaration(stmt)) {

View File

@ -0,0 +1,4 @@
/* @jsx htm */
// Don't elide htm if a JSX element appears somewhere.
import { htm } from "fake-jsx-package";
<div></div>;

View File

@ -0,0 +1,4 @@
{
"plugins": [["transform-typescript", { "isTSX": true }]]
}

View File

@ -0,0 +1,4 @@
/* @jsx htm */
// Don't elide htm if a JSX element appears somewhere.
import { htm } from "fake-jsx-package";
<div></div>;