diff --git a/packages/babel-plugin-transform-typescript/src/index.js b/packages/babel-plugin-transform-typescript/src/index.js index b558447733..2a78a433ea 100644 --- a/packages/babel-plugin-transform-typescript/src/index.js +++ b/packages/babel-plugin-transform-typescript/src/index.js @@ -21,6 +21,7 @@ interface State { } const PARSED_PARAMS = new WeakSet(); +const PRAGMA_KEY = "@babel/plugin-transform-typescript/jsxPragma"; export default declare((api, { jsxPragma = "React" }) => { api.assertVersion(7); @@ -46,7 +47,7 @@ export default declare((api, { jsxPragma = "React" }) => { for (const comment of (file.ast.comments: Array)) { const jsxMatches = JSX_ANNOTATION_REGEX.exec(comment.value); if (jsxMatches) { - jsxPragma = jsxMatches[1]; + file.set(PRAGMA_KEY, jsxMatches[1]); } } } @@ -72,7 +73,10 @@ export default declare((api, { jsxPragma = "React" }) => { // just bail if there is no binding, since chances are good that if // the import statement was injected then it wasn't a typescript type // import anyway. - if (binding && isImportTypeOnly(binding, state.programPath)) { + if ( + binding && + isImportTypeOnly(file, binding, state.programPath) + ) { importsToRemove.push(binding.path); } else { allElided = false; @@ -313,14 +317,15 @@ export default declare((api, { jsxPragma = "React" }) => { // 'access' and 'readonly' are only for parameter properties, so constructor visitor will handle them. } - function isImportTypeOnly(binding, programPath) { + function isImportTypeOnly(file, binding, programPath) { for (const path of binding.referencePaths) { if (!isInType(path)) { return false; } } - if (binding.identifier.name !== jsxPragma) { + const fileJsxPragma = file.get(PRAGMA_KEY) || jsxPragma; + if (binding.identifier.name !== fileJsxPragma) { return true; }