@babel/helper-create-class-features-plugin
Advanced tools
Comparing version 7.23.7 to 7.23.9
@@ -11,2 +11,3 @@ "use strict"; | ||
var _helperSkipTransparentExpressionWrappers = require("@babel/helper-skip-transparent-expression-wrappers"); | ||
var _fields = require("./fields.js"); | ||
function incrementId(id, idx = id.length - 1) { | ||
@@ -240,2 +241,21 @@ if (idx === -1) { | ||
} | ||
function checkPrivateMethodUpdateError(path, decoratedPrivateMethods) { | ||
const privateNameVisitor = (0, _fields.privateNameVisitorFactory)({ | ||
PrivateName(path, state) { | ||
if (!state.privateNamesMap.has(path.node.id.name)) return; | ||
const parentPath = path.parentPath; | ||
const parentParentPath = parentPath.parentPath; | ||
if (parentParentPath.node.type === "AssignmentExpression" && parentParentPath.node.left === parentPath.node || parentParentPath.node.type === "UpdateExpression" || parentParentPath.node.type === "RestElement" || parentParentPath.node.type === "ArrayPattern" || parentParentPath.node.type === "ObjectProperty" && parentParentPath.node.value === parentPath.node && parentParentPath.parentPath.type === "ObjectPattern" || parentParentPath.node.type === "ForOfStatement" && parentParentPath.node.left === parentPath.node) { | ||
throw path.buildCodeFrameError(`Decorated private methods are read-only, but "#${path.node.id.name}" is updated via this expression.`); | ||
} | ||
} | ||
}); | ||
const privateNamesMap = new Map(); | ||
for (const name of decoratedPrivateMethods) { | ||
privateNamesMap.set(name, null); | ||
} | ||
path.traverse(privateNameVisitor, { | ||
privateNamesMap: privateNamesMap | ||
}); | ||
} | ||
function transformClass(path, state, constantSuper, version, className, propertyVisitor) { | ||
@@ -254,6 +274,7 @@ const body = path.get("body.body"); | ||
for (const element of body) { | ||
var _element$node$decorat; | ||
if (!isClassDecoratableElementPath(element)) { | ||
continue; | ||
} | ||
if (element.node.decorators && element.node.decorators.length > 0) { | ||
if ((_element$node$decorat = element.node.decorators) != null && _element$node$decorat.length) { | ||
switch (element.node.type) { | ||
@@ -301,23 +322,41 @@ case "ClassProperty": | ||
const decoratorsThis = new Map(); | ||
const maybeExtractDecorator = decorator => { | ||
const { | ||
expression | ||
} = decorator; | ||
if (version === "2023-05" && _core.types.isMemberExpression(expression)) { | ||
let object; | ||
if (_core.types.isSuper(expression.object) || _core.types.isThisExpression(expression.object)) { | ||
object = memoiseExpression(_core.types.thisExpression(), "obj"); | ||
} else if (!scopeParent.isStatic(expression.object)) { | ||
object = memoiseExpression(expression.object, "obj"); | ||
expression.object = object; | ||
} else { | ||
object = expression.object; | ||
const maybeExtractDecorators = (decorators, memoiseInPlace) => { | ||
let needMemoise = false; | ||
for (const decorator of decorators) { | ||
const { | ||
expression | ||
} = decorator; | ||
if (version === "2023-05" && _core.types.isMemberExpression(expression)) { | ||
let object; | ||
if (_core.types.isSuper(expression.object) || _core.types.isThisExpression(expression.object)) { | ||
needMemoise = true; | ||
if (memoiseInPlace) { | ||
object = memoiseExpression(_core.types.thisExpression(), "obj"); | ||
} else { | ||
object = _core.types.thisExpression(); | ||
} | ||
} else { | ||
if (!scopeParent.isStatic(expression.object)) { | ||
needMemoise = true; | ||
if (memoiseInPlace) { | ||
expression.object = memoiseExpression(expression.object, "obj"); | ||
} | ||
} | ||
object = _core.types.cloneNode(expression.object); | ||
} | ||
decoratorsThis.set(decorator, object); | ||
} | ||
decoratorsThis.set(decorator, _core.types.cloneNode(object)); | ||
if (!scopeParent.isStatic(expression)) { | ||
needMemoise = true; | ||
if (memoiseInPlace) { | ||
decorator.expression = memoiseExpression(expression, "dec"); | ||
} | ||
} | ||
} | ||
if (!scopeParent.isStatic(expression)) { | ||
decorator.expression = memoiseExpression(expression, "dec"); | ||
} | ||
return needMemoise && !memoiseInPlace; | ||
}; | ||
let needsDeclaraionForClassBinding = false; | ||
let classDecorationsFlag = 0; | ||
let classDecorations = []; | ||
let classDecorationsId; | ||
if (classDecorators) { | ||
@@ -331,4 +370,11 @@ classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass"); | ||
path.node.decorators = null; | ||
for (const classDecorator of classDecorators) { | ||
maybeExtractDecorator(classDecorator); | ||
const needMemoise = maybeExtractDecorators(classDecorators, false); | ||
const { | ||
hasThis, | ||
decs | ||
} = generateDecorationList(classDecorators.map(el => el.expression), classDecorators.map(dec => decoratorsThis.get(dec)), version); | ||
classDecorationsFlag = hasThis ? 1 : 0; | ||
classDecorations = decs; | ||
if (needMemoise) { | ||
classDecorationsId = memoiseExpression(_core.types.arrayExpression(classDecorations), "classDecs"); | ||
} | ||
@@ -351,10 +397,8 @@ } else { | ||
} = element; | ||
const decorators = element.get("decorators"); | ||
const hasDecorators = Array.isArray(decorators) && decorators.length > 0; | ||
const decorators = element.node.decorators; | ||
const hasDecorators = !!(decorators != null && decorators.length); | ||
if (hasDecorators) { | ||
for (const decoratorPath of decorators) { | ||
maybeExtractDecorator(decoratorPath.node); | ||
} | ||
maybeExtractDecorators(decorators, true); | ||
} | ||
const isComputed = "computed" in element.node && element.node.computed === true; | ||
const isComputed = "computed" in element.node && element.node.computed; | ||
if (isComputed) { | ||
@@ -370,3 +414,3 @@ if (!element.get("key").isConstantExpression()) { | ||
const isPrivate = key.type === "PrivateName"; | ||
const isStatic = !!element.node.static; | ||
const isStatic = element.node.static; | ||
let name = "computedKey"; | ||
@@ -463,4 +507,4 @@ if (isPrivate) { | ||
kind, | ||
decorators: decorators.map(d => d.node.expression), | ||
decoratorsThis: decorators.map(d => decoratorsThis.get(d.node)), | ||
decorators: decorators.map(d => d.expression), | ||
decoratorsThis: decorators.map(d => decoratorsThis.get(d)), | ||
name: nameExpr, | ||
@@ -488,12 +532,2 @@ isStatic, | ||
const elementDecorations = generateDecorationExprs(elementDecoratorInfo, version); | ||
let classDecorationsFlag = 0; | ||
let classDecorations = []; | ||
if (classDecorators) { | ||
const { | ||
hasThis, | ||
decs | ||
} = generateDecorationList(classDecorators.map(el => el.expression), classDecorators.map(dec => decoratorsThis.get(dec)), version); | ||
classDecorationsFlag = hasThis ? 1 : 0; | ||
classDecorations = decs; | ||
} | ||
const elementLocals = extractElementLocalAssignments(elementDecoratorInfo); | ||
@@ -535,3 +569,3 @@ if (requiresProtoInit) { | ||
} | ||
path.node.body.body.unshift(_core.types.classMethod("constructor", _core.types.identifier("constructor"), [_core.types.restElement(_core.types.identifier("args"))], _core.types.blockStatement(body))); | ||
path.node.body.body.unshift(_core.types.classMethod("constructor", _core.types.identifier("constructor"), path.node.superClass ? [_core.types.restElement(_core.types.identifier("args"))] : [], _core.types.blockStatement(body))); | ||
} | ||
@@ -543,14 +577,2 @@ } | ||
} | ||
if (decoratedPrivateMethods.size > 0) { | ||
path.traverse({ | ||
PrivateName(path) { | ||
if (!decoratedPrivateMethods.has(path.node.id.name)) return; | ||
const parentPath = path.parentPath; | ||
const parentParentPath = parentPath.parentPath; | ||
if (parentParentPath.node.type === "AssignmentExpression" && parentParentPath.node.left === parentPath.node || parentParentPath.node.type === "UpdateExpression" || parentParentPath.node.type === "RestElement" || parentParentPath.node.type === "ArrayPattern" || parentParentPath.node.type === "ObjectProperty" && parentParentPath.node.value === parentPath.node && parentParentPath.parentPath.type === "ObjectPattern" || parentParentPath.node.type === "ForOfStatement" && parentParentPath.node.left === parentPath.node) { | ||
throw path.buildCodeFrameError(`Decorated private methods are not updatable, but "#${path.node.id.name}" is updated via this expression.`); | ||
} | ||
} | ||
}); | ||
} | ||
const classLocals = []; | ||
@@ -619,3 +641,3 @@ let classInitInjected = false; | ||
} | ||
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))); | ||
originalClass.body.body.unshift(_core.types.staticBlock([_core.types.expressionStatement(createLocalsAssignment(elementLocals, classLocals, elementDecorations, classDecorationsId ? _core.types.cloneNode(classDecorationsId) : _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))); | ||
@@ -625,2 +647,5 @@ if (needsDeclaraionForClassBinding) { | ||
} | ||
if (decoratedPrivateMethods.size > 0) { | ||
checkPrivateMethodUpdateError(path, decoratedPrivateMethods); | ||
} | ||
path.scope.crawl(); | ||
@@ -627,0 +652,0 @@ return path; |
@@ -10,2 +10,3 @@ "use strict"; | ||
exports.buildPrivateNamesNodes = buildPrivateNamesNodes; | ||
exports.privateNameVisitorFactory = privateNameVisitorFactory; | ||
exports.transformPrivateNamesUsage = transformPrivateNamesUsage; | ||
@@ -711,3 +712,3 @@ var _core = require("@babel/core"); | ||
}; | ||
const classRefForInnerBinding = (_ref = ref) != null ? _ref : props[0].scope.generateUidIdentifier("class"); | ||
const classRefForInnerBinding = (_ref = ref) != null ? _ref : props[0].scope.generateUidIdentifier((innerBindingRef == null ? void 0 : innerBindingRef.name) || "Class"); | ||
(_ref2 = ref) != null ? _ref2 : ref = _core.types.cloneNode(innerBindingRef); | ||
@@ -714,0 +715,0 @@ for (const prop of props) { |
@@ -98,8 +98,8 @@ "use strict"; | ||
if (typeof file.get(versionKey) === "number") { | ||
file.set(versionKey, "7.23.7"); | ||
file.set(versionKey, "7.23.9"); | ||
return; | ||
} | ||
} | ||
if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.23.7")) { | ||
file.set(versionKey, "7.23.7"); | ||
if (!file.get(versionKey) || _semver.lt(file.get(versionKey), "7.23.9")) { | ||
file.set(versionKey, "7.23.9"); | ||
} | ||
@@ -112,3 +112,3 @@ }, | ||
var _ref; | ||
if (file.get(versionKey) !== "7.23.7") return; | ||
if (file.get(versionKey) !== "7.23.9") return; | ||
if (!(0, _features.shouldTransform)(path, file)) return; | ||
@@ -172,3 +172,3 @@ const pathIsClassDeclaration = path.isClassDeclaration(); | ||
(0, _helperFunctionName.default)(path); | ||
ref = path.scope.generateUidIdentifier("class"); | ||
ref = path.scope.generateUidIdentifier((innerBinding == null ? void 0 : innerBinding.name) || "Class"); | ||
} | ||
@@ -230,3 +230,3 @@ const classRefForDefine = (_ref = ref) != null ? _ref : _core.types.cloneNode(innerBinding); | ||
{ | ||
if (file.get(versionKey) !== "7.23.7") return; | ||
if (file.get(versionKey) !== "7.23.9") return; | ||
const decl = path.get("declaration"); | ||
@@ -233,0 +233,0 @@ if (decl.isClassDeclaration() && (0, _decorators2.hasDecorators)(decl.node)) { |
{ | ||
"name": "@babel/helper-create-class-features-plugin", | ||
"version": "7.23.7", | ||
"version": "7.23.9", | ||
"author": "The Babel Team (https://babel.dev/team)", | ||
@@ -35,5 +35,5 @@ "license": "MIT", | ||
"devDependencies": { | ||
"@babel/core": "^7.23.7", | ||
"@babel/core": "^7.23.9", | ||
"@babel/helper-plugin-test-runner": "^7.22.5", | ||
"@babel/preset-env": "^7.23.7", | ||
"@babel/preset-env": "^7.23.9", | ||
"@types/charcodes": "^0.2.0", | ||
@@ -40,0 +40,0 @@ "charcodes": "^0.2.0" |
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
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
328981
2404