@babel/plugin-proposal-decorators
Advanced tools
Comparing version 7.23.5 to 7.23.6
@@ -11,2 +11,3 @@ "use strict"; | ||
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration"); | ||
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers"); | ||
function incrementId(id, idx = id.length - 1) { | ||
@@ -53,15 +54,15 @@ if (idx === -1) { | ||
} | ||
function replaceClassWithVar(path) { | ||
function replaceClassWithVar(path, className) { | ||
if (path.type === "ClassDeclaration") { | ||
const varId = path.scope.generateUidIdentifierBasedOnNode(path.node.id); | ||
const classId = _core.types.identifier(path.node.id.name); | ||
path.scope.rename(classId.name, varId.name); | ||
const id = path.node.id; | ||
const className = id.name; | ||
const varId = path.scope.generateUidIdentifierBasedOnNode(id); | ||
const classId = _core.types.identifier(className); | ||
path.scope.rename(className, varId.name); | ||
path.get("id").replaceWith(classId); | ||
return { | ||
id: _core.types.cloneNode(varId), | ||
path, | ||
needsDeclaration: true | ||
path | ||
}; | ||
} else { | ||
let className; | ||
let varId; | ||
@@ -72,14 +73,10 @@ if (path.node.id) { | ||
path.scope.rename(className, varId.name); | ||
} else if (path.parentPath.node.type === "VariableDeclarator" && path.parentPath.node.id.type === "Identifier") { | ||
className = path.parentPath.node.id.name; | ||
varId = path.scope.parent.generateDeclaredUidIdentifier(className); | ||
} else { | ||
varId = path.scope.parent.generateDeclaredUidIdentifier("decorated_class"); | ||
varId = path.scope.parent.generateDeclaredUidIdentifier(typeof className === "string" ? className : "decorated_class"); | ||
} | ||
const newClassExpr = _core.types.classExpression(className && _core.types.identifier(className), path.node.superClass, path.node.body); | ||
const newClassExpr = _core.types.classExpression(typeof className === "string" ? _core.types.identifier(className) : null, path.node.superClass, path.node.body); | ||
const [newPath] = path.replaceWith(_core.types.sequenceExpression([newClassExpr, varId])); | ||
return { | ||
id: _core.types.cloneNode(varId), | ||
path: newPath.get("expressions.0"), | ||
needsDeclaration: false | ||
path: newPath.get("expressions.0") | ||
}; | ||
@@ -240,3 +237,9 @@ } | ||
} | ||
function transformClass(path, state, constantSuper, version) { | ||
function createSetFunctionNameCall(state, className) { | ||
return _core.types.callExpression(state.addHelper("setFunctionName"), [_core.types.thisExpression(), className]); | ||
} | ||
function createToPropertyKeyCall(state, propertyKey) { | ||
return _core.types.callExpression(state.addHelper("toPropertyKey"), [propertyKey]); | ||
} | ||
function transformClass(path, state, constantSuper, version, className, propertyVisitor) { | ||
const body = path.get("body.body"); | ||
@@ -246,2 +249,9 @@ const classDecorators = path.node.decorators; | ||
const generateClassPrivateUid = createLazyPrivateUidGeneratorForClass(path); | ||
const assignments = []; | ||
const scopeParent = path.scope.parent; | ||
const memoiseExpression = (expression, hint) => { | ||
const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint); | ||
assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression)); | ||
return _core.types.cloneNode(localEvaluatedId); | ||
}; | ||
for (const element of body) { | ||
@@ -252,4 +262,16 @@ if (!isClassDecoratableElementPath(element)) { | ||
if (element.node.decorators && element.node.decorators.length > 0) { | ||
switch (element.node.type) { | ||
case "ClassProperty": | ||
propertyVisitor.ClassProperty(element, state); | ||
break; | ||
case "ClassPrivateProperty": | ||
propertyVisitor.ClassPrivateProperty(element, state); | ||
break; | ||
case "ClassAccessorProperty": | ||
propertyVisitor.ClassAccessorProperty(element, state); | ||
break; | ||
} | ||
hasElementDecorators = true; | ||
} else if (element.node.type === "ClassAccessorProperty") { | ||
propertyVisitor.ClassAccessorProperty(element, state); | ||
const { | ||
@@ -262,9 +284,15 @@ key, | ||
const newId = generateClassPrivateUid(); | ||
const valueNode = value ? _core.types.cloneNode(value) : undefined; | ||
const newField = generateClassProperty(newId, valueNode, isStatic); | ||
const newField = generateClassProperty(newId, value, isStatic); | ||
const keyPath = element.get("key"); | ||
const [newPath] = element.replaceWith(newField); | ||
addProxyAccessorsFor(path.node.id, newPath, key, newId, version, computed); | ||
addProxyAccessorsFor(path.node.id, newPath, computed && !keyPath.isConstantExpression() ? memoiseExpression(createToPropertyKeyCall(state, key), "computedKey") : key, newId, version, computed); | ||
} | ||
} | ||
if (!classDecorators && !hasElementDecorators) return; | ||
if (!classDecorators && !hasElementDecorators) { | ||
if (assignments.length > 0) { | ||
path.insertBefore(assignments.map(expr => _core.types.expressionStatement(expr))); | ||
path.scope.crawl(); | ||
} | ||
return; | ||
} | ||
const elementDecoratorInfo = []; | ||
@@ -277,9 +305,2 @@ let firstFieldPath; | ||
let protoInitLocal, staticInitLocal, classInitLocal, classIdLocal; | ||
const assignments = []; | ||
const scopeParent = path.scope.parent; | ||
const memoiseExpression = (expression, hint) => { | ||
const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint); | ||
assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression)); | ||
return _core.types.cloneNode(localEvaluatedId); | ||
}; | ||
const decoratorsThis = new Map(); | ||
@@ -309,7 +330,7 @@ const maybeExtractDecorator = decorator => { | ||
classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass"); | ||
needsDeclaraionForClassBinding = path.isClassDeclaration(); | ||
({ | ||
id: classIdLocal, | ||
path, | ||
needsDeclaration: needsDeclaraionForClassBinding | ||
} = replaceClassWithVar(path)); | ||
path | ||
} = replaceClassWithVar(path, className)); | ||
path.node.decorators = null; | ||
@@ -344,4 +365,4 @@ for (const classDecorator of classDecorators) { | ||
if (isComputed) { | ||
if (!scopeParent.isStatic(node.key)) { | ||
node.key = memoiseExpression(node.key, "computedKey"); | ||
if (!element.get("key").isConstantExpression()) { | ||
node.key = memoiseExpression(createToPropertyKeyCall(state, node.key), "computedKey"); | ||
} | ||
@@ -593,3 +614,3 @@ } | ||
} | ||
originalClass.body.body.unshift(_core.types.staticBlock([_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, _core.types.arrayExpression(classDecorations), _core.types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, _core.types.cloneNode(superClass), state, version)), requiresStaticInit && _core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()]))].filter(Boolean))); | ||
originalClass.body.body.unshift(_core.types.staticBlock([_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, _core.types.arrayExpression(classDecorations), _core.types.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, typeof className === "object" ? className : undefined, _core.types.cloneNode(superClass), state, version)), requiresStaticInit && _core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()]))].filter(Boolean))); | ||
path.insertBefore(assignments.map(expr => _core.types.expressionStatement(expr))); | ||
@@ -602,5 +623,5 @@ if (needsDeclaraionForClassBinding) { | ||
} | ||
function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBranName, superClass, state, version) { | ||
function createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorations, classDecorationsFlag, maybePrivateBranName, setClassName, superClass, state, version) { | ||
let lhs, rhs; | ||
const args = [_core.types.thisExpression(), elementDecorations, classDecorations]; | ||
const args = [setClassName ? createSetFunctionNameCall(state, setClassName) : _core.types.thisExpression(), elementDecorations, classDecorations]; | ||
{ | ||
@@ -649,2 +670,165 @@ if (version === "2021-12" || version === "2022-03" && !state.availableHelper("applyDecs2203R")) { | ||
} | ||
function isProtoKey(node) { | ||
return node.type === "Identifier" ? node.name === "__proto__" : node.value === "__proto__"; | ||
} | ||
function isDecorated(node) { | ||
return node.decorators && node.decorators.length > 0; | ||
} | ||
function shouldTransformElement(node) { | ||
switch (node.type) { | ||
case "ClassAccessorProperty": | ||
return true; | ||
case "ClassMethod": | ||
case "ClassProperty": | ||
case "ClassPrivateMethod": | ||
case "ClassPrivateProperty": | ||
return isDecorated(node); | ||
default: | ||
return false; | ||
} | ||
} | ||
function shouldTransformClass(node) { | ||
return isDecorated(node) || node.body.body.some(shouldTransformElement); | ||
} | ||
function NamedEvaluationVisitoryFactory(isAnonymous, visitor) { | ||
function handleComputedProperty(propertyPath, key, state) { | ||
switch (key.type) { | ||
case "StringLiteral": | ||
return _core.types.stringLiteral(key.value); | ||
case "NumericLiteral": | ||
case "BigIntLiteral": | ||
{ | ||
const keyValue = key.value + ""; | ||
propertyPath.get("key").replaceWith(_core.types.stringLiteral(keyValue)); | ||
return _core.types.stringLiteral(keyValue); | ||
} | ||
default: | ||
{ | ||
const ref = propertyPath.scope.maybeGenerateMemoised(key); | ||
propertyPath.get("key").replaceWith(_core.types.assignmentExpression("=", ref, _core.types.callExpression(state.addHelper("toPropertyKey"), [key]))); | ||
return _core.types.cloneNode(ref); | ||
} | ||
} | ||
} | ||
return { | ||
VariableDeclarator(path, state) { | ||
const id = path.node.id; | ||
if (id.type === "Identifier") { | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("init")); | ||
if (isAnonymous(initializer)) { | ||
const name = id.name; | ||
visitor(initializer, state, name); | ||
} | ||
} | ||
}, | ||
AssignmentExpression(path, state) { | ||
const id = path.node.left; | ||
if (id.type === "Identifier") { | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right")); | ||
if (isAnonymous(initializer)) { | ||
switch (path.node.operator) { | ||
case "=": | ||
case "&&=": | ||
case "||=": | ||
case "??=": | ||
visitor(initializer, state, id.name); | ||
} | ||
} | ||
} | ||
}, | ||
AssignmentPattern(path, state) { | ||
const id = path.node.left; | ||
if (id.type === "Identifier") { | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("right")); | ||
if (isAnonymous(initializer)) { | ||
const name = id.name; | ||
visitor(initializer, state, name); | ||
} | ||
} | ||
}, | ||
ObjectExpression(path, state) { | ||
for (const propertyPath of path.get("properties")) { | ||
const { | ||
node | ||
} = propertyPath; | ||
if (node.type !== "ObjectProperty") continue; | ||
const id = node.key; | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(propertyPath.get("value")); | ||
if (isAnonymous(initializer)) { | ||
if (!node.computed) { | ||
if (!isProtoKey(id)) { | ||
if (id.type === "Identifier") { | ||
visitor(initializer, state, id.name); | ||
} else { | ||
const className = _core.types.stringLiteral(id.value + ""); | ||
visitor(initializer, state, className); | ||
} | ||
} | ||
} else { | ||
const ref = handleComputedProperty(propertyPath, id, state); | ||
visitor(initializer, state, ref); | ||
} | ||
} | ||
} | ||
}, | ||
ClassPrivateProperty(path, state) { | ||
const { | ||
node | ||
} = path; | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value")); | ||
if (isAnonymous(initializer)) { | ||
const className = _core.types.stringLiteral("#" + node.key.id.name); | ||
visitor(initializer, state, className); | ||
} | ||
}, | ||
ClassAccessorProperty(path, state) { | ||
const { | ||
node | ||
} = path; | ||
const id = node.key; | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value")); | ||
if (isAnonymous(initializer)) { | ||
if (!node.computed) { | ||
if (id.type === "Identifier") { | ||
visitor(initializer, state, id.name); | ||
} else if (id.type === "PrivateName") { | ||
const className = _core.types.stringLiteral("#" + id.id.name); | ||
visitor(initializer, state, className); | ||
} else { | ||
const className = _core.types.stringLiteral(id.value + ""); | ||
visitor(initializer, state, className); | ||
} | ||
} else { | ||
const ref = handleComputedProperty(path, id, state); | ||
visitor(initializer, state, ref); | ||
} | ||
} | ||
}, | ||
ClassProperty(path, state) { | ||
const { | ||
node | ||
} = path; | ||
const id = node.key; | ||
const initializer = (0, _helperSkipTransparentExpressionWrappers.skipTransparentExprWrappers)(path.get("value")); | ||
if (isAnonymous(initializer)) { | ||
if (!node.computed) { | ||
if (id.type === "Identifier") { | ||
visitor(initializer, state, id.name); | ||
} else { | ||
const className = _core.types.stringLiteral(id.value + ""); | ||
visitor(initializer, state, className); | ||
} | ||
} else { | ||
const ref = handleComputedProperty(path, id, state); | ||
visitor(initializer, state, ref); | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
function isDecoratedAnonymousClassExpression(path) { | ||
return path.isClassExpression({ | ||
id: null | ||
}) && shouldTransformClass(path.node); | ||
} | ||
function _default({ | ||
@@ -668,12 +852,38 @@ assertVersion, | ||
const constantSuper = (_assumption = assumption("constantSuper")) != null ? _assumption : loose; | ||
const namedEvaluationVisitor = NamedEvaluationVisitoryFactory(isDecoratedAnonymousClassExpression, visitClass); | ||
function visitClass(path, state, className) { | ||
var _className, _node$id; | ||
if (VISITED.has(path)) return; | ||
const { | ||
node | ||
} = path; | ||
(_className = className) != null ? _className : className = (_node$id = node.id) == null ? void 0 : _node$id.name; | ||
const newPath = transformClass(path, state, constantSuper, version, className, namedEvaluationVisitor); | ||
if (newPath) { | ||
VISITED.add(newPath); | ||
return; | ||
} | ||
VISITED.add(path); | ||
} | ||
return { | ||
name: "proposal-decorators", | ||
inherits: _pluginSyntaxDecorators.default, | ||
visitor: { | ||
"ExportNamedDeclaration|ExportDefaultDeclaration"(path) { | ||
var _declaration$decorato; | ||
visitor: Object.assign({ | ||
ExportDefaultDeclaration(path, state) { | ||
const { | ||
declaration | ||
} = path.node; | ||
if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0) { | ||
if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) { | ||
const isAnonymous = !declaration.id; | ||
const updatedVarDeclarationPath = (0, _helperSplitExportDeclaration.default)(path); | ||
if (isAnonymous) { | ||
visitClass(updatedVarDeclarationPath, state, _core.types.stringLiteral("default")); | ||
} | ||
} | ||
}, | ||
ExportNamedDeclaration(path) { | ||
const { | ||
declaration | ||
} = path.node; | ||
if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && isDecorated(declaration)) { | ||
(0, _helperSplitExportDeclaration.default)(path); | ||
@@ -683,7 +893,5 @@ } | ||
Class(path, state) { | ||
if (VISITED.has(path)) return; | ||
const newPath = transformClass(path, state, constantSuper, version); | ||
if (newPath) VISITED.add(newPath); | ||
visitClass(path, state, undefined); | ||
} | ||
} | ||
}, namedEvaluationVisitor) | ||
}; | ||
@@ -690,0 +898,0 @@ } |
{ | ||
"name": "@babel/plugin-proposal-decorators", | ||
"version": "7.23.5", | ||
"version": "7.23.6", | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
@@ -23,5 +23,6 @@ "license": "MIT", | ||
"dependencies": { | ||
"@babel/helper-create-class-features-plugin": "^7.23.5", | ||
"@babel/helper-create-class-features-plugin": "^7.23.6", | ||
"@babel/helper-plugin-utils": "^7.22.5", | ||
"@babel/helper-replace-supers": "^7.22.20", | ||
"@babel/helper-skip-transparent-expression-wrappers": "^7.22.5", | ||
"@babel/helper-split-export-declaration": "^7.22.6", | ||
@@ -34,5 +35,5 @@ "@babel/plugin-syntax-decorators": "^7.23.3" | ||
"devDependencies": { | ||
"@babel/core": "^7.23.5", | ||
"@babel/core": "^7.23.6", | ||
"@babel/helper-plugin-test-runner": "^7.22.5", | ||
"@babel/traverse": "^7.23.5", | ||
"@babel/traverse": "^7.23.6", | ||
"@types/charcodes": "^0.2.0", | ||
@@ -39,0 +40,0 @@ "array.prototype.concat": "^1.0.2", |
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
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
166580
1095
7