@babel/plugin-transform-typescript
Advanced tools
Comparing version 7.5.1 to 7.5.2
@@ -68,3 +68,16 @@ "use strict"; | ||
const PARSED_PARAMS = new WeakSet(); | ||
const GLOBAL_TYPES = new WeakMap(); | ||
function isGlobalType(path, name) { | ||
const program = path.find(path => path.isProgram()).node; | ||
if (path.scope.hasOwnBinding(name)) return false; | ||
if (GLOBAL_TYPES.get(program).has(name)) return true; | ||
console.warn(`The exported identifier "${name}" is not declared in Babel's scope tracker\n` + `as a JavaScript value binding, and "@babel/plugin-transform-typescript"\n` + `never encountered it as a TypeScript type declaration.\n` + `It will be treated as a JavaScript value.\n\n` + `This problem is likely caused by another plugin injecting\n` + `"${name}" without registering it in the scope tracker. If you are the author\n` + ` of that plugin, please use "scope.registerDeclaration(declarationPath)".`); | ||
return false; | ||
} | ||
function registerGlobalType(programScope, name) { | ||
GLOBAL_TYPES.get(programScope.path.node).add(name); | ||
} | ||
var _default = (0, _helperPluginUtils().declare)((api, { | ||
@@ -90,2 +103,6 @@ jsxPragma = "React", | ||
if (!GLOBAL_TYPES.has(path.node)) { | ||
GLOBAL_TYPES.set(path.node, new Set()); | ||
} | ||
if (file.ast.comments) { | ||
@@ -101,3 +118,3 @@ for (const comment of file.ast.comments) { | ||
for (const stmt of path.get("body")) { | ||
for (let stmt of path.get("body")) { | ||
if (_core().types.isImportDeclaration(stmt)) { | ||
@@ -132,3 +149,25 @@ if (stmt.node.specifiers.length === 0) { | ||
} | ||
continue; | ||
} | ||
if (stmt.isExportDeclaration()) { | ||
stmt = stmt.get("declaration"); | ||
} | ||
if (stmt.isVariableDeclaration({ | ||
declare: true | ||
})) { | ||
for (const name of Object.keys(stmt.getBindingIdentifiers())) { | ||
registerGlobalType(path.scope, name); | ||
} | ||
} else if (stmt.isTSTypeAliasDeclaration() || stmt.isTSDeclareFunction() || stmt.isTSInterfaceDeclaration() || stmt.isClassDeclaration({ | ||
declare: true | ||
}) || stmt.isTSEnumDeclaration({ | ||
declare: true | ||
}) || stmt.isTSModuleDeclaration({ | ||
declare: true | ||
}) && stmt.get("id").isIdentifier()) { | ||
registerGlobalType(path.scope, stmt.node.id.name); | ||
} | ||
} | ||
@@ -138,3 +177,5 @@ }, | ||
ExportNamedDeclaration(path) { | ||
if (!path.node.source && path.node.specifiers.length > 0 && !path.node.specifiers.find(exportSpecifier => path.scope.hasOwnBinding(exportSpecifier.local.name))) { | ||
if (!path.node.source && path.node.specifiers.length > 0 && path.node.specifiers.every(({ | ||
local | ||
}) => isGlobalType(path, local.name))) { | ||
path.remove(); | ||
@@ -145,3 +186,3 @@ } | ||
ExportSpecifier(path) { | ||
if (!path.parent.source && !path.scope.hasOwnBinding(path.node.local.name)) { | ||
if (!path.parent.source && isGlobalType(path, path.node.local.name)) { | ||
path.remove(); | ||
@@ -152,3 +193,3 @@ } | ||
ExportDefaultDeclaration(path) { | ||
if (_core().types.isIdentifier(path.node.declaration) && !path.scope.hasOwnBinding(path.node.declaration.name)) { | ||
if (_core().types.isIdentifier(path.node.declaration) && isGlobalType(path, path.node.declaration.name)) { | ||
path.remove(); | ||
@@ -167,3 +208,5 @@ } | ||
VariableDeclaration(path) { | ||
if (path.node.declare) path.remove(); | ||
if (path.node.declare) { | ||
path.remove(); | ||
} | ||
}, | ||
@@ -170,0 +213,0 @@ |
{ | ||
"name": "@babel/plugin-transform-typescript", | ||
"version": "7.5.1", | ||
"version": "7.5.2", | ||
"description": "Transform TypeScript into ES.next", | ||
@@ -27,3 +27,3 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-typescript", | ||
}, | ||
"gitHead": "d0519fa9076ad2b50fe0a4d745c863c61c226117" | ||
"gitHead": "0dbf99bedb1d82b8414685181416c19336dd0d96" | ||
} |
23364
656