@babel/plugin-transform-typescript
Advanced tools
Comparing version 7.22.9 to 8.0.0-alpha.0
@@ -1,9 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = transpileConstEnum; | ||
var _enum = require("./enum"); | ||
function transpileConstEnum(path, t) { | ||
import { translateEnumValues } from "./enum.js"; | ||
export default function transpileConstEnum(path, t) { | ||
const { | ||
@@ -19,3 +13,3 @@ name | ||
enumValues: entries | ||
} = (0, _enum.translateEnumValues)(path, t); | ||
} = translateEnumValues(path, t); | ||
if (isExported) { | ||
@@ -22,0 +16,0 @@ const obj = t.objectExpression(entries.map(([name, value]) => t.objectProperty(t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(name), value))); |
@@ -1,13 +0,6 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = transpileEnum; | ||
exports.translateEnumValues = translateEnumValues; | ||
var _core = require("@babel/core"); | ||
var _assert = require("assert"); | ||
var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure"); | ||
import { template, types as t } from "@babel/core"; | ||
import assert from "assert"; | ||
import annotateAsPure from "@babel/helper-annotate-as-pure"; | ||
const ENUMS = new WeakMap(); | ||
const buildEnumWrapper = _core.template.expression(` | ||
const buildEnumWrapper = template.expression(` | ||
(function (ID) { | ||
@@ -18,3 +11,3 @@ ASSIGNMENTS; | ||
`); | ||
function transpileEnum(path, t) { | ||
export default function transpileEnum(path, t) { | ||
const { | ||
@@ -48,3 +41,3 @@ node, | ||
})); | ||
if (isPure) (0, _helperAnnotateAsPure.default)(enumIIFE); | ||
if (isPure) annotateAsPure(enumIIFE); | ||
if (isSeen) { | ||
@@ -74,6 +67,6 @@ const toReplace = parentPath.isExportDeclaration() ? parentPath : path; | ||
} | ||
const buildStringAssignment = (0, _core.template)(` | ||
const buildStringAssignment = template(` | ||
ENUM["NAME"] = VALUE; | ||
`); | ||
const buildNumericAssignment = (0, _core.template)(` | ||
const buildNumericAssignment = template(` | ||
ENUM[ENUM["NAME"] = VALUE] = "NAME"; | ||
@@ -117,3 +110,3 @@ `); | ||
}; | ||
function translateEnumValues(path, t) { | ||
export function translateEnumValues(path, t) { | ||
const seen = new Map(); | ||
@@ -136,7 +129,7 @@ let constValue = -1; | ||
} else { | ||
_assert(typeof constValue === "string"); | ||
assert(typeof constValue === "string"); | ||
value = t.stringLiteral(constValue); | ||
} | ||
} else { | ||
isPure && (isPure = initializerPath.isPure()); | ||
isPure &&= initializerPath.isPure(); | ||
if (initializerPath.isReferencedIdentifier()) { | ||
@@ -224,3 +217,3 @@ ReferencedIdentifier(initializerPath, { | ||
const prop = expr.property; | ||
if (!_core.types.isIdentifier(obj) || (expr.computed ? !_core.types.isStringLiteral(prop) : !_core.types.isIdentifier(prop))) { | ||
if (!t.isIdentifier(obj) || (expr.computed ? !t.isStringLiteral(prop) : !t.isIdentifier(prop))) { | ||
return; | ||
@@ -237,3 +230,3 @@ } | ||
} | ||
let value = prevMembers == null ? void 0 : prevMembers.get(name); | ||
let value = prevMembers?.get(name); | ||
if (value !== undefined) { | ||
@@ -247,3 +240,3 @@ return value; | ||
value = computeConstantValue(bindingInitPath, undefined, seen); | ||
prevMembers == null ? void 0 : prevMembers.set(name, value); | ||
prevMembers?.set(name, value); | ||
return value; | ||
@@ -302,3 +295,3 @@ } | ||
case "**": | ||
return Math.pow(left, right); | ||
return left ** right; | ||
default: | ||
@@ -305,0 +298,0 @@ return undefined; |
561
lib/index.js
@@ -1,13 +0,499 @@ | ||
"use strict"; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import syntaxTypeScript from '@babel/plugin-syntax-typescript'; | ||
import { injectInitialization } from '@babel/helper-create-class-features-plugin'; | ||
import { template, types } from '@babel/core'; | ||
import assert from 'assert'; | ||
import annotateAsPure from '@babel/helper-annotate-as-pure'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _helperPluginUtils = require("@babel/helper-plugin-utils"); | ||
var _pluginSyntaxTypescript = require("@babel/plugin-syntax-typescript"); | ||
var _helperCreateClassFeaturesPlugin = require("@babel/helper-create-class-features-plugin"); | ||
var _constEnum = require("./const-enum"); | ||
var _enum = require("./enum"); | ||
var _namespace = require("./namespace"); | ||
const ENUMS = new WeakMap(); | ||
const buildEnumWrapper = template.expression(` | ||
(function (ID) { | ||
ASSIGNMENTS; | ||
return ID; | ||
})(INIT) | ||
`); | ||
function transpileEnum(path, t) { | ||
const { | ||
node, | ||
parentPath | ||
} = path; | ||
if (node.declare) { | ||
path.remove(); | ||
return; | ||
} | ||
const name = node.id.name; | ||
const { | ||
fill, | ||
data, | ||
isPure | ||
} = enumFill(path, t, node.id); | ||
switch (parentPath.type) { | ||
case "BlockStatement": | ||
case "ExportNamedDeclaration": | ||
case "Program": | ||
{ | ||
const isGlobal = t.isProgram(path.parent); | ||
const isSeen = seen(parentPath); | ||
let init = t.objectExpression([]); | ||
if (isSeen || isGlobal) { | ||
init = t.logicalExpression("||", t.cloneNode(fill.ID), init); | ||
} | ||
const enumIIFE = buildEnumWrapper(Object.assign({}, fill, { | ||
INIT: init | ||
})); | ||
if (isPure) annotateAsPure(enumIIFE); | ||
if (isSeen) { | ||
const toReplace = parentPath.isExportDeclaration() ? parentPath : path; | ||
toReplace.replaceWith(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(node.id), enumIIFE))); | ||
} else { | ||
path.scope.registerDeclaration(path.replaceWith(t.variableDeclaration(isGlobal ? "var" : "let", [t.variableDeclarator(node.id, enumIIFE)]))[0]); | ||
} | ||
ENUMS.set(path.scope.getBindingIdentifier(name), data); | ||
break; | ||
} | ||
default: | ||
throw new Error(`Unexpected enum parent '${path.parent.type}`); | ||
} | ||
function seen(parentPath) { | ||
if (parentPath.isExportDeclaration()) { | ||
return seen(parentPath.parentPath); | ||
} | ||
if (parentPath.getData(name)) { | ||
return true; | ||
} else { | ||
parentPath.setData(name, true); | ||
return false; | ||
} | ||
} | ||
} | ||
const buildStringAssignment = template(` | ||
ENUM["NAME"] = VALUE; | ||
`); | ||
const buildNumericAssignment = template(` | ||
ENUM[ENUM["NAME"] = VALUE] = "NAME"; | ||
`); | ||
const buildEnumMember = (isString, options) => (isString ? buildStringAssignment : buildNumericAssignment)(options); | ||
function enumFill(path, t, id) { | ||
const { | ||
enumValues: x, | ||
data, | ||
isPure | ||
} = translateEnumValues(path, t); | ||
const assignments = x.map(([memberName, memberValue]) => buildEnumMember(t.isStringLiteral(memberValue), { | ||
ENUM: t.cloneNode(id), | ||
NAME: memberName, | ||
VALUE: memberValue | ||
})); | ||
return { | ||
fill: { | ||
ID: t.cloneNode(id), | ||
ASSIGNMENTS: assignments | ||
}, | ||
data, | ||
isPure | ||
}; | ||
} | ||
function ReferencedIdentifier(expr, state) { | ||
const { | ||
seen, | ||
path, | ||
t | ||
} = state; | ||
const name = expr.node.name; | ||
if (seen.has(name) && !expr.scope.hasOwnBinding(name)) { | ||
expr.replaceWith(t.memberExpression(t.cloneNode(path.node.id), t.cloneNode(expr.node))); | ||
expr.skip(); | ||
} | ||
} | ||
const enumSelfReferenceVisitor = { | ||
ReferencedIdentifier | ||
}; | ||
function translateEnumValues(path, t) { | ||
const seen = new Map(); | ||
let constValue = -1; | ||
let lastName; | ||
let isPure = true; | ||
const enumValues = path.get("members").map(memberPath => { | ||
const member = memberPath.node; | ||
const name = t.isIdentifier(member.id) ? member.id.name : member.id.value; | ||
const initializerPath = memberPath.get("initializer"); | ||
const initializer = member.initializer; | ||
let value; | ||
if (initializer) { | ||
constValue = computeConstantValue(initializerPath, seen); | ||
if (constValue !== undefined) { | ||
seen.set(name, constValue); | ||
if (typeof constValue === "number") { | ||
value = t.numericLiteral(constValue); | ||
} else { | ||
assert(typeof constValue === "string"); | ||
value = t.stringLiteral(constValue); | ||
} | ||
} else { | ||
isPure &&= initializerPath.isPure(); | ||
if (initializerPath.isReferencedIdentifier()) { | ||
ReferencedIdentifier(initializerPath, { | ||
t, | ||
seen, | ||
path | ||
}); | ||
} else { | ||
initializerPath.traverse(enumSelfReferenceVisitor, { | ||
t, | ||
seen, | ||
path | ||
}); | ||
} | ||
value = initializerPath.node; | ||
seen.set(name, undefined); | ||
} | ||
} else if (typeof constValue === "number") { | ||
constValue += 1; | ||
value = t.numericLiteral(constValue); | ||
seen.set(name, constValue); | ||
} else if (typeof constValue === "string") { | ||
throw path.buildCodeFrameError("Enum member must have initializer."); | ||
} else { | ||
const lastRef = t.memberExpression(t.cloneNode(path.node.id), t.stringLiteral(lastName), true); | ||
value = t.binaryExpression("+", t.numericLiteral(1), lastRef); | ||
seen.set(name, undefined); | ||
} | ||
lastName = name; | ||
return [name, value]; | ||
}); | ||
return { | ||
isPure, | ||
data: seen, | ||
enumValues | ||
}; | ||
} | ||
function computeConstantValue(path, prevMembers, seen = new Set()) { | ||
return evaluate(path); | ||
function evaluate(path) { | ||
const expr = path.node; | ||
switch (expr.type) { | ||
case "MemberExpression": | ||
return evaluateRef(path, prevMembers, seen); | ||
case "StringLiteral": | ||
return expr.value; | ||
case "UnaryExpression": | ||
return evalUnaryExpression(path); | ||
case "BinaryExpression": | ||
return evalBinaryExpression(path); | ||
case "NumericLiteral": | ||
return expr.value; | ||
case "ParenthesizedExpression": | ||
return evaluate(path.get("expression")); | ||
case "Identifier": | ||
return evaluateRef(path, prevMembers, seen); | ||
case "TemplateLiteral": | ||
{ | ||
if (expr.quasis.length === 1) { | ||
return expr.quasis[0].value.cooked; | ||
} | ||
const paths = path.get("expressions"); | ||
const quasis = expr.quasis; | ||
let str = ""; | ||
for (let i = 0; i < quasis.length; i++) { | ||
str += quasis[i].value.cooked; | ||
if (i + 1 < quasis.length) { | ||
const value = evaluateRef(paths[i], prevMembers, seen); | ||
if (value === undefined) return undefined; | ||
str += value; | ||
} | ||
} | ||
return str; | ||
} | ||
default: | ||
return undefined; | ||
} | ||
} | ||
function evaluateRef(path, prevMembers, seen) { | ||
if (path.isMemberExpression()) { | ||
const expr = path.node; | ||
const obj = expr.object; | ||
const prop = expr.property; | ||
if (!types.isIdentifier(obj) || (expr.computed ? !types.isStringLiteral(prop) : !types.isIdentifier(prop))) { | ||
return; | ||
} | ||
const bindingIdentifier = path.scope.getBindingIdentifier(obj.name); | ||
const data = ENUMS.get(bindingIdentifier); | ||
if (!data) return; | ||
return data.get(prop.computed ? prop.value : prop.name); | ||
} else if (path.isIdentifier()) { | ||
const name = path.node.name; | ||
if (["Infinity", "NaN"].includes(name)) { | ||
return Number(name); | ||
} | ||
let value = prevMembers?.get(name); | ||
if (value !== undefined) { | ||
return value; | ||
} | ||
if (seen.has(path.node)) return; | ||
const bindingInitPath = path.resolve(); | ||
if (bindingInitPath) { | ||
seen.add(path.node); | ||
value = computeConstantValue(bindingInitPath, undefined, seen); | ||
prevMembers?.set(name, value); | ||
return value; | ||
} | ||
} | ||
} | ||
function evalUnaryExpression(path) { | ||
const value = evaluate(path.get("argument")); | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
switch (path.node.operator) { | ||
case "+": | ||
return value; | ||
case "-": | ||
return -value; | ||
case "~": | ||
return ~value; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
function evalBinaryExpression(path) { | ||
const left = evaluate(path.get("left")); | ||
if (left === undefined) { | ||
return undefined; | ||
} | ||
const right = evaluate(path.get("right")); | ||
if (right === undefined) { | ||
return undefined; | ||
} | ||
switch (path.node.operator) { | ||
case "|": | ||
return left | right; | ||
case "&": | ||
return left & right; | ||
case ">>": | ||
return left >> right; | ||
case ">>>": | ||
return left >>> right; | ||
case "<<": | ||
return left << right; | ||
case "^": | ||
return left ^ right; | ||
case "*": | ||
return left * right; | ||
case "/": | ||
return left / right; | ||
case "+": | ||
return left + right; | ||
case "-": | ||
return left - right; | ||
case "%": | ||
return left % right; | ||
case "**": | ||
return left ** right; | ||
default: | ||
return undefined; | ||
} | ||
} | ||
} | ||
function transpileConstEnum(path, t) { | ||
const { | ||
name | ||
} = path.node.id; | ||
const parentIsExport = path.parentPath.isExportNamedDeclaration(); | ||
let isExported = parentIsExport; | ||
if (!isExported && t.isProgram(path.parent)) { | ||
isExported = path.parent.body.some(stmt => t.isExportNamedDeclaration(stmt) && stmt.exportKind !== "type" && !stmt.source && stmt.specifiers.some(spec => t.isExportSpecifier(spec) && spec.exportKind !== "type" && spec.local.name === name)); | ||
} | ||
const { | ||
enumValues: entries | ||
} = translateEnumValues(path, t); | ||
if (isExported) { | ||
const obj = t.objectExpression(entries.map(([name, value]) => t.objectProperty(t.isValidIdentifier(name) ? t.identifier(name) : t.stringLiteral(name), value))); | ||
if (path.scope.hasOwnBinding(name)) { | ||
(parentIsExport ? path.parentPath : path).replaceWith(t.expressionStatement(t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), [path.node.id, obj]))); | ||
} else { | ||
path.replaceWith(t.variableDeclaration("var", [t.variableDeclarator(path.node.id, obj)])); | ||
path.scope.registerDeclaration(path); | ||
} | ||
return; | ||
} | ||
const entriesMap = new Map(entries); | ||
path.scope.path.traverse({ | ||
Scope(path) { | ||
if (path.scope.hasOwnBinding(name)) path.skip(); | ||
}, | ||
MemberExpression(path) { | ||
if (!t.isIdentifier(path.node.object, { | ||
name | ||
})) return; | ||
let key; | ||
if (path.node.computed) { | ||
if (t.isStringLiteral(path.node.property)) { | ||
key = path.node.property.value; | ||
} else { | ||
return; | ||
} | ||
} else if (t.isIdentifier(path.node.property)) { | ||
key = path.node.property.name; | ||
} else { | ||
return; | ||
} | ||
if (!entriesMap.has(key)) return; | ||
path.replaceWith(t.cloneNode(entriesMap.get(key))); | ||
} | ||
}); | ||
path.remove(); | ||
} | ||
function transpileNamespace(path, allowNamespaces) { | ||
if (path.node.declare || path.node.id.type === "StringLiteral") { | ||
path.remove(); | ||
return; | ||
} | ||
if (!allowNamespaces) { | ||
throw path.get("id").buildCodeFrameError("Namespace not marked type-only declare." + " Non-declarative namespaces are only supported experimentally in Babel." + " To enable and review caveats see:" + " https://babeljs.io/docs/en/babel-plugin-transform-typescript"); | ||
} | ||
const name = path.node.id.name; | ||
const value = handleNested(path, types.cloneNode(path.node, true)); | ||
if (value === null) { | ||
path.remove(); | ||
} else if (path.scope.hasOwnBinding(name)) { | ||
path.replaceWith(value); | ||
} else { | ||
path.scope.registerDeclaration(path.replaceWithMultiple([getDeclaration(name), value])[0]); | ||
} | ||
} | ||
function getDeclaration(name) { | ||
return types.variableDeclaration("let", [types.variableDeclarator(types.identifier(name))]); | ||
} | ||
function getMemberExpression(name, itemName) { | ||
return types.memberExpression(types.identifier(name), types.identifier(itemName)); | ||
} | ||
function handleVariableDeclaration(node, name, hub) { | ||
if (node.kind !== "const") { | ||
throw hub.file.buildCodeFrameError(node, "Namespaces exporting non-const are not supported by Babel." + " Change to const or see:" + " https://babeljs.io/docs/en/babel-plugin-transform-typescript"); | ||
} | ||
const { | ||
declarations | ||
} = node; | ||
if (declarations.every(declarator => types.isIdentifier(declarator.id))) { | ||
for (const declarator of declarations) { | ||
declarator.init = types.assignmentExpression("=", getMemberExpression(name, declarator.id.name), declarator.init); | ||
} | ||
return [node]; | ||
} | ||
const bindingIdentifiers = types.getBindingIdentifiers(node); | ||
const assignments = []; | ||
for (const idName in bindingIdentifiers) { | ||
assignments.push(types.assignmentExpression("=", getMemberExpression(name, idName), types.cloneNode(bindingIdentifiers[idName]))); | ||
} | ||
return [node, types.expressionStatement(types.sequenceExpression(assignments))]; | ||
} | ||
function buildNestedAmbientModuleError(path, node) { | ||
return path.hub.buildError(node, "Ambient modules cannot be nested in other modules or namespaces.", Error); | ||
} | ||
function handleNested(path, node, parentExport) { | ||
const names = new Set(); | ||
const realName = node.id; | ||
types.assertIdentifier(realName); | ||
const name = path.scope.generateUid(realName.name); | ||
const namespaceTopLevel = types.isTSModuleBlock(node.body) ? node.body.body : [types.exportNamedDeclaration(node.body)]; | ||
let isEmpty = true; | ||
for (let i = 0; i < namespaceTopLevel.length; i++) { | ||
const subNode = namespaceTopLevel[i]; | ||
switch (subNode.type) { | ||
case "TSModuleDeclaration": | ||
{ | ||
if (!types.isIdentifier(subNode.id)) { | ||
throw buildNestedAmbientModuleError(path, subNode); | ||
} | ||
const transformed = handleNested(path, subNode); | ||
if (transformed !== null) { | ||
isEmpty = false; | ||
const moduleName = subNode.id.name; | ||
if (names.has(moduleName)) { | ||
namespaceTopLevel[i] = transformed; | ||
} else { | ||
names.add(moduleName); | ||
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed); | ||
} | ||
} | ||
continue; | ||
} | ||
case "TSEnumDeclaration": | ||
case "FunctionDeclaration": | ||
case "ClassDeclaration": | ||
isEmpty = false; | ||
names.add(subNode.id.name); | ||
continue; | ||
case "VariableDeclaration": | ||
{ | ||
isEmpty = false; | ||
for (const name in types.getBindingIdentifiers(subNode)) { | ||
names.add(name); | ||
} | ||
continue; | ||
} | ||
default: | ||
isEmpty &&= types.isTypeScript(subNode); | ||
continue; | ||
case "ExportNamedDeclaration": | ||
} | ||
if ("declare" in subNode.declaration && subNode.declaration.declare) { | ||
continue; | ||
} | ||
switch (subNode.declaration.type) { | ||
case "TSEnumDeclaration": | ||
case "FunctionDeclaration": | ||
case "ClassDeclaration": | ||
{ | ||
isEmpty = false; | ||
const itemName = subNode.declaration.id.name; | ||
names.add(itemName); | ||
namespaceTopLevel.splice(i++, 1, subNode.declaration, types.expressionStatement(types.assignmentExpression("=", getMemberExpression(name, itemName), types.identifier(itemName)))); | ||
break; | ||
} | ||
case "VariableDeclaration": | ||
{ | ||
isEmpty = false; | ||
const nodes = handleVariableDeclaration(subNode.declaration, name, path.hub); | ||
namespaceTopLevel.splice(i, nodes.length, ...nodes); | ||
i += nodes.length - 1; | ||
break; | ||
} | ||
case "TSModuleDeclaration": | ||
{ | ||
if (!types.isIdentifier(subNode.declaration.id)) { | ||
throw buildNestedAmbientModuleError(path, subNode.declaration); | ||
} | ||
const transformed = handleNested(path, subNode.declaration, types.identifier(name)); | ||
if (transformed !== null) { | ||
isEmpty = false; | ||
const moduleName = subNode.declaration.id.name; | ||
if (names.has(moduleName)) { | ||
namespaceTopLevel[i] = transformed; | ||
} else { | ||
names.add(moduleName); | ||
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (isEmpty) return null; | ||
let fallthroughValue = types.objectExpression([]); | ||
if (parentExport) { | ||
const memberExpr = types.memberExpression(parentExport, realName); | ||
fallthroughValue = template.expression.ast` | ||
${types.cloneNode(memberExpr)} || | ||
(${types.cloneNode(memberExpr)} = ${fallthroughValue}) | ||
`; | ||
} | ||
return template.statement.ast` | ||
(function (${types.identifier(name)}) { | ||
${namespaceTopLevel} | ||
})(${realName} || (${types.cloneNode(realName)} = ${fallthroughValue})); | ||
`; | ||
} | ||
function isInType(path) { | ||
@@ -58,3 +544,3 @@ switch (path.parent.type) { | ||
} | ||
var _default = (0, _helperPluginUtils.declare)((api, opts) => { | ||
var index = declare((api, opts) => { | ||
const { | ||
@@ -73,7 +559,2 @@ types: t, | ||
} = opts; | ||
{ | ||
var { | ||
allowDeclareFields = false | ||
} = opts; | ||
} | ||
const classMemberVisitors = { | ||
@@ -84,7 +565,2 @@ field(path) { | ||
} = path; | ||
{ | ||
if (!allowDeclareFields && node.declare) { | ||
throw path.buildCodeFrameError(`The 'declare' modifier is only allowed when the 'allowDeclareFields' option of ` + `@babel/plugin-transform-typescript or @babel/preset-typescript is enabled.`); | ||
} | ||
} | ||
if (node.declare) { | ||
@@ -101,14 +577,5 @@ if (node.value) { | ||
} | ||
{ | ||
if (!allowDeclareFields && !node.decorators && !t.isClassPrivateProperty(node)) { | ||
path.remove(); | ||
} | ||
} | ||
} else if (node.abstract) { | ||
path.remove(); | ||
} else { | ||
if (!allowDeclareFields && !node.value && !node.decorators && !t.isClassPrivateProperty(node)) { | ||
path.remove(); | ||
} | ||
} | ||
} else ; | ||
if (node.accessibility) node.accessibility = null; | ||
@@ -157,3 +624,3 @@ if (node.abstract) node.abstract = null; | ||
} | ||
(0, _helperCreateClassFeaturesPlugin.injectInitialization)(classPath, path, assigns); | ||
injectInitialization(classPath, path, assigns); | ||
} | ||
@@ -163,3 +630,3 @@ }; | ||
name: "transform-typescript", | ||
inherits: _pluginSyntaxTypescript.default, | ||
inherits: syntaxTypeScript, | ||
visitor: { | ||
@@ -298,2 +765,16 @@ Pattern: visitPattern, | ||
} | ||
if (t.isTSModuleDeclaration(path.node.declaration)) { | ||
const namespace = path.node.declaration; | ||
const { | ||
id | ||
} = namespace; | ||
if (t.isIdentifier(id)) { | ||
if (path.scope.hasOwnBinding(id.name)) { | ||
path.replaceWith(namespace); | ||
} else { | ||
const [newExport] = path.replaceWithMultiple([t.exportNamedDeclaration(t.variableDeclaration("let", [t.variableDeclarator(t.cloneNode(id))])), namespace]); | ||
path.scope.registerDeclaration(newExport); | ||
} | ||
} | ||
} | ||
NEEDS_EXPLICIT_ESM.set(state.file.ast.program, false); | ||
@@ -381,3 +862,3 @@ }, | ||
TSModuleDeclaration(path) { | ||
(0, _namespace.default)(path, allowNamespaces); | ||
transpileNamespace(path, allowNamespaces); | ||
}, | ||
@@ -392,5 +873,5 @@ TSInterfaceDeclaration(path) { | ||
if (optimizeConstEnums && path.node.const) { | ||
(0, _constEnum.default)(path, t); | ||
transpileConstEnum(path, t); | ||
} else { | ||
(0, _enum.default)(path, t); | ||
transpileEnum(path, t); | ||
} | ||
@@ -429,6 +910,6 @@ }, | ||
node = node.expression; | ||
} while (t.isTSAsExpression(node) || t.isTSSatisfiesExpression != null && t.isTSSatisfiesExpression(node)); | ||
} while (t.isTSAsExpression(node) || t.isTSSatisfiesExpression?.(node)); | ||
path.replaceWith(node); | ||
}, | ||
[api.types.tsInstantiationExpression ? "TSNonNullExpression|TSInstantiationExpression" : "TSNonNullExpression"](path) { | ||
["TSNonNullExpression|TSInstantiationExpression"](path) { | ||
path.replaceWith(path.node.expression); | ||
@@ -489,4 +970,4 @@ }, | ||
}); | ||
exports.default = _default; | ||
export { index as default }; | ||
//# sourceMappingURL=index.js.map |
@@ -1,9 +0,3 @@ | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = transpileNamespace; | ||
var _core = require("@babel/core"); | ||
function transpileNamespace(path, allowNamespaces) { | ||
import { template, types as t } from "@babel/core"; | ||
export default function transpileNamespace(path, allowNamespaces) { | ||
if (path.node.declare || path.node.id.type === "StringLiteral") { | ||
@@ -17,13 +11,6 @@ path.remove(); | ||
const name = path.node.id.name; | ||
const value = handleNested(path, _core.types.cloneNode(path.node, true)); | ||
const bound = path.scope.hasOwnBinding(name); | ||
if (path.parent.type === "ExportNamedDeclaration") { | ||
if (!bound) { | ||
path.parentPath.insertAfter(value); | ||
path.replaceWith(getDeclaration(name)); | ||
path.scope.registerDeclaration(path.parentPath); | ||
} else { | ||
path.parentPath.replaceWith(value); | ||
} | ||
} else if (bound) { | ||
const value = handleNested(path, t.cloneNode(path.node, true)); | ||
if (value === null) { | ||
path.remove(); | ||
} else if (path.scope.hasOwnBinding(name)) { | ||
path.replaceWith(value); | ||
@@ -35,6 +22,6 @@ } else { | ||
function getDeclaration(name) { | ||
return _core.types.variableDeclaration("let", [_core.types.variableDeclarator(_core.types.identifier(name))]); | ||
return t.variableDeclaration("let", [t.variableDeclarator(t.identifier(name))]); | ||
} | ||
function getMemberExpression(name, itemName) { | ||
return _core.types.memberExpression(_core.types.identifier(name), _core.types.identifier(itemName)); | ||
return t.memberExpression(t.identifier(name), t.identifier(itemName)); | ||
} | ||
@@ -48,14 +35,14 @@ function handleVariableDeclaration(node, name, hub) { | ||
} = node; | ||
if (declarations.every(declarator => _core.types.isIdentifier(declarator.id))) { | ||
if (declarations.every(declarator => t.isIdentifier(declarator.id))) { | ||
for (const declarator of declarations) { | ||
declarator.init = _core.types.assignmentExpression("=", getMemberExpression(name, declarator.id.name), declarator.init); | ||
declarator.init = t.assignmentExpression("=", getMemberExpression(name, declarator.id.name), declarator.init); | ||
} | ||
return [node]; | ||
} | ||
const bindingIdentifiers = _core.types.getBindingIdentifiers(node); | ||
const bindingIdentifiers = t.getBindingIdentifiers(node); | ||
const assignments = []; | ||
for (const idName in bindingIdentifiers) { | ||
assignments.push(_core.types.assignmentExpression("=", getMemberExpression(name, idName), _core.types.cloneNode(bindingIdentifiers[idName]))); | ||
assignments.push(t.assignmentExpression("=", getMemberExpression(name, idName), t.cloneNode(bindingIdentifiers[idName]))); | ||
} | ||
return [node, _core.types.expressionStatement(_core.types.sequenceExpression(assignments))]; | ||
return [node, t.expressionStatement(t.sequenceExpression(assignments))]; | ||
} | ||
@@ -68,5 +55,6 @@ function buildNestedAmbientModuleError(path, node) { | ||
const realName = node.id; | ||
_core.types.assertIdentifier(realName); | ||
t.assertIdentifier(realName); | ||
const name = path.scope.generateUid(realName.name); | ||
const namespaceTopLevel = _core.types.isTSModuleBlock(node.body) ? node.body.body : [_core.types.exportNamedDeclaration(node.body)]; | ||
const namespaceTopLevel = t.isTSModuleBlock(node.body) ? node.body.body : [t.exportNamedDeclaration(node.body)]; | ||
let isEmpty = true; | ||
for (let i = 0; i < namespaceTopLevel.length; i++) { | ||
@@ -77,12 +65,15 @@ const subNode = namespaceTopLevel[i]; | ||
{ | ||
if (!_core.types.isIdentifier(subNode.id)) { | ||
if (!t.isIdentifier(subNode.id)) { | ||
throw buildNestedAmbientModuleError(path, subNode); | ||
} | ||
const transformed = handleNested(path, subNode); | ||
const moduleName = subNode.id.name; | ||
if (names.has(moduleName)) { | ||
namespaceTopLevel[i] = transformed; | ||
} else { | ||
names.add(moduleName); | ||
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed); | ||
if (transformed !== null) { | ||
isEmpty = false; | ||
const moduleName = subNode.id.name; | ||
if (names.has(moduleName)) { | ||
namespaceTopLevel[i] = transformed; | ||
} else { | ||
names.add(moduleName); | ||
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed); | ||
} | ||
} | ||
@@ -94,2 +85,3 @@ continue; | ||
case "ClassDeclaration": | ||
isEmpty = false; | ||
names.add(subNode.id.name); | ||
@@ -99,3 +91,4 @@ continue; | ||
{ | ||
for (const name in _core.types.getBindingIdentifiers(subNode)) { | ||
isEmpty = false; | ||
for (const name in t.getBindingIdentifiers(subNode)) { | ||
names.add(name); | ||
@@ -106,2 +99,3 @@ } | ||
default: | ||
isEmpty &&= t.isTypeScript(subNode); | ||
continue; | ||
@@ -118,5 +112,6 @@ case "ExportNamedDeclaration": | ||
{ | ||
isEmpty = false; | ||
const itemName = subNode.declaration.id.name; | ||
names.add(itemName); | ||
namespaceTopLevel.splice(i++, 1, subNode.declaration, _core.types.expressionStatement(_core.types.assignmentExpression("=", getMemberExpression(name, itemName), _core.types.identifier(itemName)))); | ||
namespaceTopLevel.splice(i++, 1, subNode.declaration, t.expressionStatement(t.assignmentExpression("=", getMemberExpression(name, itemName), t.identifier(itemName)))); | ||
break; | ||
@@ -126,2 +121,3 @@ } | ||
{ | ||
isEmpty = false; | ||
const nodes = handleVariableDeclaration(subNode.declaration, name, path.hub); | ||
@@ -134,12 +130,15 @@ namespaceTopLevel.splice(i, nodes.length, ...nodes); | ||
{ | ||
if (!_core.types.isIdentifier(subNode.declaration.id)) { | ||
if (!t.isIdentifier(subNode.declaration.id)) { | ||
throw buildNestedAmbientModuleError(path, subNode.declaration); | ||
} | ||
const transformed = handleNested(path, subNode.declaration, _core.types.identifier(name)); | ||
const moduleName = subNode.declaration.id.name; | ||
if (names.has(moduleName)) { | ||
namespaceTopLevel[i] = transformed; | ||
} else { | ||
names.add(moduleName); | ||
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed); | ||
const transformed = handleNested(path, subNode.declaration, t.identifier(name)); | ||
if (transformed !== null) { | ||
isEmpty = false; | ||
const moduleName = subNode.declaration.id.name; | ||
if (names.has(moduleName)) { | ||
namespaceTopLevel[i] = transformed; | ||
} else { | ||
names.add(moduleName); | ||
namespaceTopLevel.splice(i++, 1, getDeclaration(moduleName), transformed); | ||
} | ||
} | ||
@@ -149,14 +148,15 @@ } | ||
} | ||
let fallthroughValue = _core.types.objectExpression([]); | ||
if (isEmpty) return null; | ||
let fallthroughValue = t.objectExpression([]); | ||
if (parentExport) { | ||
const memberExpr = _core.types.memberExpression(parentExport, realName); | ||
fallthroughValue = _core.template.expression.ast` | ||
${_core.types.cloneNode(memberExpr)} || | ||
(${_core.types.cloneNode(memberExpr)} = ${fallthroughValue}) | ||
const memberExpr = t.memberExpression(parentExport, realName); | ||
fallthroughValue = template.expression.ast` | ||
${t.cloneNode(memberExpr)} || | ||
(${t.cloneNode(memberExpr)} = ${fallthroughValue}) | ||
`; | ||
} | ||
return _core.template.statement.ast` | ||
(function (${_core.types.identifier(name)}) { | ||
return template.statement.ast` | ||
(function (${t.identifier(name)}) { | ||
${namespaceTopLevel} | ||
})(${realName} || (${_core.types.cloneNode(realName)} = ${fallthroughValue})); | ||
})(${realName} || (${t.cloneNode(realName)} = ${fallthroughValue})); | ||
`; | ||
@@ -163,0 +163,0 @@ } |
{ | ||
"name": "@babel/plugin-transform-typescript", | ||
"version": "7.22.9", | ||
"version": "8.0.0-alpha.0", | ||
"description": "Transform TypeScript into ES.next", | ||
@@ -20,22 +20,26 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-annotate-as-pure": "^7.22.5", | ||
"@babel/helper-create-class-features-plugin": "^7.22.9", | ||
"@babel/helper-plugin-utils": "^7.22.5", | ||
"@babel/plugin-syntax-typescript": "^7.22.5" | ||
"@babel/helper-annotate-as-pure": "^8.0.0-alpha.0", | ||
"@babel/helper-create-class-features-plugin": "^8.0.0-alpha.0", | ||
"@babel/helper-plugin-utils": "^8.0.0-alpha.0", | ||
"@babel/plugin-syntax-typescript": "^8.0.0-alpha.0" | ||
}, | ||
"peerDependencies": { | ||
"@babel/core": "^7.0.0-0" | ||
"@babel/core": "^8.0.0-alpha.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.22.9", | ||
"@babel/helper-plugin-test-runner": "^7.22.5", | ||
"@babel/traverse": "^7.22.8", | ||
"@babel/types": "^7.22.5" | ||
"@babel/core": "^8.0.0-alpha.0", | ||
"@babel/helper-plugin-test-runner": "^8.0.0-alpha.0", | ||
"@babel/traverse": "^8.0.0-alpha.0", | ||
"@babel/types": "^8.0.0-alpha.0" | ||
}, | ||
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-typescript", | ||
"engines": { | ||
"node": ">=6.9.0" | ||
"node": "^16.20.0 || ^18.16.0 || >=20.0.0" | ||
}, | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
"type": "commonjs" | ||
"exports": { | ||
".": "./lib/index.js", | ||
"./package.json": "./package.json" | ||
}, | ||
"type": "module" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
189181
1454
Yes
1
+ Added@babel/code-frame@8.0.0-alpha.12(transitive)
+ Added@babel/compat-data@8.0.0-alpha.12(transitive)
+ Added@babel/core@8.0.0-alpha.12(transitive)
+ Added@babel/generator@8.0.0-alpha.12(transitive)
+ Added@babel/helper-annotate-as-pure@8.0.0-alpha.12(transitive)
+ Added@babel/helper-compilation-targets@8.0.0-alpha.12(transitive)
+ Added@babel/helper-create-class-features-plugin@8.0.0-alpha.12(transitive)
+ Added@babel/helper-member-expression-to-functions@8.0.0-alpha.12(transitive)
+ Added@babel/helper-optimise-call-expression@8.0.0-alpha.12(transitive)
+ Added@babel/helper-plugin-utils@8.0.0-alpha.12(transitive)
+ Added@babel/helper-replace-supers@8.0.0-alpha.12(transitive)
+ Added@babel/helper-skip-transparent-expression-wrappers@8.0.0-alpha.12(transitive)
+ Added@babel/helper-string-parser@8.0.0-alpha.12(transitive)
+ Added@babel/helper-validator-identifier@8.0.0-alpha.12(transitive)
+ Added@babel/helper-validator-option@8.0.0-alpha.12(transitive)
+ Added@babel/helpers@8.0.0-alpha.12(transitive)
+ Added@babel/highlight@8.0.0-alpha.12(transitive)
+ Added@babel/parser@8.0.0-alpha.12(transitive)
+ Added@babel/plugin-syntax-typescript@8.0.0-alpha.12(transitive)
+ Added@babel/template@8.0.0-alpha.12(transitive)
+ Added@babel/traverse@8.0.0-alpha.12(transitive)
+ Added@babel/types@8.0.0-alpha.12(transitive)
+ Addedglobals@15.9.0(transitive)
+ Addedjs-tokens@8.0.3(transitive)
+ Addedjsesc@3.0.2(transitive)
+ Addedlru-cache@7.18.3(transitive)
+ Addedsemver@7.6.3(transitive)
+ Addedto-fast-properties@3.0.1(transitive)
- Removed@babel/code-frame@7.24.7(transitive)
- Removed@babel/compat-data@7.25.4(transitive)
- Removed@babel/core@7.25.2(transitive)
- Removed@babel/generator@7.25.6(transitive)
- Removed@babel/helper-annotate-as-pure@7.24.7(transitive)
- Removed@babel/helper-compilation-targets@7.25.2(transitive)
- Removed@babel/helper-create-class-features-plugin@7.25.4(transitive)
- Removed@babel/helper-member-expression-to-functions@7.24.8(transitive)
- Removed@babel/helper-module-imports@7.24.7(transitive)
- Removed@babel/helper-module-transforms@7.25.2(transitive)
- Removed@babel/helper-optimise-call-expression@7.24.7(transitive)
- Removed@babel/helper-plugin-utils@7.24.8(transitive)
- Removed@babel/helper-replace-supers@7.25.0(transitive)
- Removed@babel/helper-simple-access@7.24.7(transitive)
- Removed@babel/helper-skip-transparent-expression-wrappers@7.24.7(transitive)
- Removed@babel/helper-string-parser@7.24.8(transitive)
- Removed@babel/helper-validator-identifier@7.24.7(transitive)
- Removed@babel/helper-validator-option@7.24.8(transitive)
- Removed@babel/helpers@7.25.6(transitive)
- Removed@babel/highlight@7.24.7(transitive)
- Removed@babel/parser@7.25.6(transitive)
- Removed@babel/plugin-syntax-typescript@7.25.4(transitive)
- Removed@babel/template@7.25.0(transitive)
- Removed@babel/traverse@7.25.6(transitive)
- Removed@babel/types@7.25.6(transitive)
- Removedansi-styles@3.2.1(transitive)
- Removedchalk@2.4.2(transitive)
- Removedcolor-convert@1.9.3(transitive)
- Removedcolor-name@1.1.3(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedglobals@11.12.0(transitive)
- Removedhas-flag@3.0.0(transitive)
- Removedjs-tokens@4.0.0(transitive)
- Removedjsesc@2.5.2(transitive)
- Removedlru-cache@5.1.1(transitive)
- Removedsemver@6.3.1(transitive)
- Removedsupports-color@5.5.0(transitive)
- Removedto-fast-properties@2.0.0(transitive)
- Removedyallist@3.1.1(transitive)
Updated@babel/helper-create-class-features-plugin@^8.0.0-alpha.0