diff --git a/packages/babel-plugin-transform-typescript/src/index.js b/packages/babel-plugin-transform-typescript/src/index.js index e579d2a8a3..149c7f1050 100644 --- a/packages/babel-plugin-transform-typescript/src/index.js +++ b/packages/babel-plugin-transform-typescript/src/index.js @@ -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)) { + 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)) { diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/input.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/input.mjs new file mode 100644 index 0000000000..450c0667d1 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/input.mjs @@ -0,0 +1,4 @@ +/* @jsx htm */ +// Don't elide htm if a JSX element appears somewhere. +import { htm } from "fake-jsx-package"; +
; diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/options.json b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/options.json new file mode 100644 index 0000000000..3a7f4c0405 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/options.json @@ -0,0 +1,4 @@ +{ + "plugins": [["transform-typescript", { "isTSX": true }]] +} + \ No newline at end of file diff --git a/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/output.mjs b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/output.mjs new file mode 100644 index 0000000000..450c0667d1 --- /dev/null +++ b/packages/babel-plugin-transform-typescript/test/fixtures/imports/elide-jsx-pragma-no/output.mjs @@ -0,0 +1,4 @@ +/* @jsx htm */ +// Don't elide htm if a JSX element appears somewhere. +import { htm } from "fake-jsx-package"; +
;