@babel/plugin-transform-typescript
Advanced tools
Comparing version 7.21.0 to 7.21.3
148
lib/enum.js
@@ -10,6 +10,14 @@ "use strict"; | ||
var _assert = require("assert"); | ||
var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure"); | ||
const ENUMS = new WeakMap(); | ||
const buildEnumWrapper = _core.template.expression(` | ||
(function (ID) { | ||
ASSIGNMENTS; | ||
return ID; | ||
})(INIT) | ||
`); | ||
function transpileEnum(path, t) { | ||
const { | ||
node | ||
node, | ||
parentPath | ||
} = path; | ||
@@ -22,6 +30,7 @@ if (node.declare) { | ||
const { | ||
wrapper: fill, | ||
data | ||
fill, | ||
data, | ||
isPure | ||
} = enumFill(path, t, node.id); | ||
switch (path.parent.type) { | ||
switch (parentPath.type) { | ||
case "BlockStatement": | ||
@@ -31,10 +40,19 @@ case "ExportNamedDeclaration": | ||
{ | ||
path.insertAfter(fill); | ||
if (seen(path.parentPath)) { | ||
path.remove(); | ||
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) (0, _helperAnnotateAsPure.default)(enumIIFE); | ||
if (isSeen) { | ||
const toReplace = parentPath.isExportDeclaration() ? parentPath : path; | ||
toReplace.replaceWith(t.expressionStatement(t.assignmentExpression("=", t.cloneNode(node.id), enumIIFE))); | ||
} else { | ||
const isGlobal = t.isProgram(path.parent); | ||
path.scope.registerDeclaration(path.replaceWith(makeVar(node.id, t, isGlobal ? "var" : "let"))[0]); | ||
ENUMS.set(path.scope.getBindingIdentifier(name), data); | ||
path.scope.registerDeclaration(path.replaceWith(t.variableDeclaration(isGlobal ? "var" : "let", [t.variableDeclarator(node.id, enumIIFE)]))[0]); | ||
} | ||
ENUMS.set(path.scope.getBindingIdentifier(name), data); | ||
break; | ||
@@ -57,10 +75,2 @@ } | ||
} | ||
function makeVar(id, t, kind) { | ||
return t.variableDeclaration(kind, [t.variableDeclarator(id)]); | ||
} | ||
const buildEnumWrapper = (0, _core.template)(` | ||
(function (ID) { | ||
ASSIGNMENTS; | ||
})(ID || (ID = {})); | ||
`); | ||
const buildStringAssignment = (0, _core.template)(` | ||
@@ -76,3 +86,4 @@ ENUM["NAME"] = VALUE; | ||
enumValues: x, | ||
data | ||
data, | ||
isPure | ||
} = translateEnumValues(path, t); | ||
@@ -85,7 +96,8 @@ const assignments = x.map(([memberName, memberValue]) => buildEnumMember(t.isStringLiteral(memberValue), { | ||
return { | ||
wrapper: buildEnumWrapper({ | ||
fill: { | ||
ID: t.cloneNode(id), | ||
ASSIGNMENTS: assignments | ||
}), | ||
data: data | ||
}, | ||
data, | ||
isPure | ||
}; | ||
@@ -112,51 +124,55 @@ } | ||
let lastName; | ||
return { | ||
data: seen, | ||
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); | ||
} | ||
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 { | ||
if (initializerPath.isReferencedIdentifier()) { | ||
ReferencedIdentifier(initializerPath, { | ||
t, | ||
seen, | ||
path | ||
}); | ||
} else { | ||
initializerPath.traverse(enumSelfReferenceVisitor, { | ||
t, | ||
seen, | ||
path | ||
}); | ||
} | ||
value = initializerPath.node; | ||
seen.set(name, undefined); | ||
_assert(typeof constValue === "string"); | ||
value = t.stringLiteral(constValue); | ||
} | ||
} 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); | ||
isPure && (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); | ||
} | ||
lastName = name; | ||
return [name, value]; | ||
}) | ||
} 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 | ||
}; | ||
@@ -163,0 +179,0 @@ } |
@@ -22,3 +22,3 @@ "use strict"; | ||
case "ExportSpecifier": | ||
return path.parentPath.parent.exportKind === "type"; | ||
return path.parent.exportKind === "type" || path.parentPath.parent.exportKind === "type"; | ||
default: | ||
@@ -390,2 +390,3 @@ return false; | ||
path.replaceWith(t.variableDeclaration("var", [t.variableDeclarator(path.node.id, entityNameToExpr(path.node.moduleReference))])); | ||
path.scope.registerDeclaration(path); | ||
}, | ||
@@ -392,0 +393,0 @@ TSExportAssignment(path) { |
@@ -60,3 +60,3 @@ "use strict"; | ||
function buildNestedAmbientModuleError(path, node) { | ||
throw path.hub.buildError(node, "Ambient modules cannot be nested in other modules or namespaces.", Error); | ||
return path.hub.buildError(node, "Ambient modules cannot be nested in other modules or namespaces.", Error); | ||
} | ||
@@ -63,0 +63,0 @@ function handleNested(path, node, parentExport) { |
{ | ||
"name": "@babel/plugin-transform-typescript", | ||
"version": "7.21.0", | ||
"version": "7.21.3", | ||
"description": "Transform TypeScript into ES.next", | ||
@@ -20,2 +20,3 @@ "repository": { | ||
"dependencies": { | ||
"@babel/helper-annotate-as-pure": "^7.18.6", | ||
"@babel/helper-create-class-features-plugin": "^7.21.0", | ||
@@ -29,6 +30,6 @@ "@babel/helper-plugin-utils": "^7.20.2", | ||
"devDependencies": { | ||
"@babel/core": "^7.21.0", | ||
"@babel/core": "^7.21.3", | ||
"@babel/helper-plugin-test-runner": "^7.18.6", | ||
"@babel/traverse": "^7.21.0", | ||
"@babel/types": "^7.21.0" | ||
"@babel/traverse": "^7.21.3", | ||
"@babel/types": "^7.21.3" | ||
}, | ||
@@ -35,0 +36,0 @@ "homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-typescript", |
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
124095
967
131
3
7
2
25
5