@babel/plugin-transform-typescript
Advanced tools
Comparing version 7.14.3 to 7.14.4
183
lib/index.js
@@ -36,4 +36,5 @@ "use strict"; | ||
const GLOBAL_TYPES = new WeakMap(); | ||
const NEEDS_EXPLICIT_ESM = new WeakMap(); | ||
const PARSED_PARAMS = new WeakSet(); | ||
const GLOBAL_TYPES = new WeakMap(); | ||
@@ -108,2 +109,3 @@ function isGlobalType(path, name) { | ||
if (node.declare) node.declare = null; | ||
if (node.override) node.override = null; | ||
}, | ||
@@ -117,2 +119,3 @@ | ||
if (node.optional) node.optional = null; | ||
if (node.override) node.override = null; | ||
}, | ||
@@ -158,105 +161,125 @@ | ||
RestElement: visitPattern, | ||
Program: { | ||
enter(path, state) { | ||
const { | ||
file | ||
} = state; | ||
let fileJsxPragma = null; | ||
let fileJsxPragmaFrag = null; | ||
Program(path, state) { | ||
const { | ||
file | ||
} = state; | ||
let fileJsxPragma = null; | ||
let fileJsxPragmaFrag = null; | ||
if (!GLOBAL_TYPES.has(path.node)) { | ||
GLOBAL_TYPES.set(path.node, new Set()); | ||
} | ||
if (!GLOBAL_TYPES.has(path.node)) { | ||
GLOBAL_TYPES.set(path.node, new Set()); | ||
} | ||
if (file.ast.comments) { | ||
for (const comment of file.ast.comments) { | ||
const jsxMatches = JSX_PRAGMA_REGEX.exec(comment.value); | ||
if (file.ast.comments) { | ||
for (const comment of file.ast.comments) { | ||
const jsxMatches = JSX_PRAGMA_REGEX.exec(comment.value); | ||
if (jsxMatches) { | ||
if (jsxMatches[1]) { | ||
fileJsxPragmaFrag = jsxMatches[2]; | ||
} else { | ||
fileJsxPragma = jsxMatches[2]; | ||
if (jsxMatches) { | ||
if (jsxMatches[1]) { | ||
fileJsxPragmaFrag = jsxMatches[2]; | ||
} else { | ||
fileJsxPragma = jsxMatches[2]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
let pragmaImportName = fileJsxPragma || jsxPragma; | ||
let pragmaImportName = fileJsxPragma || jsxPragma; | ||
if (pragmaImportName) { | ||
[pragmaImportName] = pragmaImportName.split("."); | ||
} | ||
if (pragmaImportName) { | ||
[pragmaImportName] = pragmaImportName.split("."); | ||
} | ||
let pragmaFragImportName = fileJsxPragmaFrag || jsxPragmaFrag; | ||
let pragmaFragImportName = fileJsxPragmaFrag || jsxPragmaFrag; | ||
if (pragmaFragImportName) { | ||
[pragmaFragImportName] = pragmaFragImportName.split("."); | ||
} | ||
if (pragmaFragImportName) { | ||
[pragmaFragImportName] = pragmaFragImportName.split("."); | ||
} | ||
for (let stmt of path.get("body")) { | ||
if (stmt.isImportDeclaration()) { | ||
if (stmt.node.importKind === "type") { | ||
stmt.remove(); | ||
continue; | ||
} | ||
for (let stmt of path.get("body")) { | ||
if (stmt.isImportDeclaration()) { | ||
if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) { | ||
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, true); | ||
} | ||
if (!onlyRemoveTypeImports) { | ||
if (stmt.node.specifiers.length === 0) { | ||
if (stmt.node.importKind === "type") { | ||
stmt.remove(); | ||
continue; | ||
} | ||
let allElided = true; | ||
const importsToRemove = []; | ||
if (onlyRemoveTypeImports) { | ||
NEEDS_EXPLICIT_ESM.set(path.node, false); | ||
} else { | ||
if (stmt.node.specifiers.length === 0) { | ||
NEEDS_EXPLICIT_ESM.set(path.node, false); | ||
continue; | ||
} | ||
for (const specifier of stmt.node.specifiers) { | ||
const binding = stmt.scope.getBinding(specifier.local.name); | ||
let allElided = true; | ||
const importsToRemove = []; | ||
if (binding && isImportTypeOnly({ | ||
binding, | ||
programPath: path, | ||
pragmaImportName, | ||
pragmaFragImportName | ||
})) { | ||
importsToRemove.push(binding.path); | ||
for (const specifier of stmt.node.specifiers) { | ||
const binding = stmt.scope.getBinding(specifier.local.name); | ||
if (binding && isImportTypeOnly({ | ||
binding, | ||
programPath: path, | ||
pragmaImportName, | ||
pragmaFragImportName | ||
})) { | ||
importsToRemove.push(binding.path); | ||
} else { | ||
allElided = false; | ||
NEEDS_EXPLICIT_ESM.set(path.node, false); | ||
} | ||
} | ||
if (allElided) { | ||
stmt.remove(); | ||
} else { | ||
allElided = false; | ||
for (const importPath of importsToRemove) { | ||
importPath.remove(); | ||
} | ||
} | ||
} | ||
if (allElided) { | ||
stmt.remove(); | ||
} else { | ||
for (const importPath of importsToRemove) { | ||
importPath.remove(); | ||
} | ||
} | ||
continue; | ||
} | ||
continue; | ||
} | ||
if (stmt.isExportDeclaration()) { | ||
stmt = stmt.get("declaration"); | ||
} | ||
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); | ||
} | ||
} | ||
}, | ||
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); | ||
exit(path) { | ||
if (path.node.sourceType === "module" && NEEDS_EXPLICIT_ESM.get(path.node)) { | ||
path.pushContainer("body", _core.types.exportNamedDeclaration()); | ||
} | ||
} | ||
}, | ||
ExportNamedDeclaration(path) { | ||
ExportNamedDeclaration(path, state) { | ||
if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) { | ||
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, true); | ||
} | ||
if (path.node.exportKind === "type") { | ||
@@ -271,3 +294,6 @@ path.remove(); | ||
path.remove(); | ||
return; | ||
} | ||
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false); | ||
}, | ||
@@ -281,6 +307,13 @@ | ||
ExportDefaultDeclaration(path) { | ||
ExportDefaultDeclaration(path, state) { | ||
if (!NEEDS_EXPLICIT_ESM.has(state.file.ast.program)) { | ||
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, true); | ||
} | ||
if (_core.types.isIdentifier(path.node.declaration) && isGlobalType(path, path.node.declaration.name)) { | ||
path.remove(); | ||
return; | ||
} | ||
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false); | ||
}, | ||
@@ -287,0 +320,0 @@ |
{ | ||
"name": "@babel/plugin-transform-typescript", | ||
"version": "7.14.3", | ||
"version": "7.14.4", | ||
"description": "Transform TypeScript into ES.next", | ||
@@ -20,3 +20,3 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-create-class-features-plugin": "^7.14.3", | ||
"@babel/helper-create-class-features-plugin": "^7.14.4", | ||
"@babel/helper-plugin-utils": "^7.13.0", | ||
@@ -32,5 +32,5 @@ "@babel/plugin-syntax-typescript": "^7.12.13" | ||
"@babel/traverse": "7.14.2", | ||
"@babel/types": "7.14.2" | ||
"@babel/types": "7.14.4" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-typescript" | ||
} |
27364
737
1