Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Socket
Sign inDemoInstall

@babel/helper-create-class-features-plugin

Package Overview
Dependencies
Maintainers
4
Versions
109
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@babel/helper-create-class-features-plugin - npm Package Compare versions

Comparing version 7.19.0 to 7.20.2

41

lib/decorators.js

@@ -9,7 +9,4 @@ "use strict";

exports.hasOwnDecorators = hasOwnDecorators;
var _core = require("@babel/core");
var _helperReplaceSupers = require("@babel/helper-replace-supers");
var _helperFunctionName = require("@babel/helper-function-name");

@@ -20,7 +17,5 @@

}
function hasDecorators(node) {
return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
}
function prop(key, value) {

@@ -30,18 +25,13 @@ if (!value) return null;

}
function method(key, body) {
return _core.types.objectMethod("method", _core.types.identifier(key), [], _core.types.blockStatement(body));
}
function takeDecorators(node) {
let result;
if (node.decorators && node.decorators.length > 0) {
result = _core.types.arrayExpression(node.decorators.map(decorator => decorator.expression));
}
node.decorators = undefined;
return result;
}
function getKey(node) {

@@ -53,21 +43,17 @@ if (node.computed) {

} else {
return _core.types.stringLiteral(String(node.key.value));
return _core.types.stringLiteral(String(
node.key.value));
}
}
function extractElementDescriptor(file, classRef, superRef, path) {
const isMethod = path.isClassMethod();
if (path.isPrivate()) {
throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
}
if (path.node.type === "ClassAccessorProperty") {
throw path.buildCodeFrameError(`Accessor properties are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
}
if (path.node.type === "StaticBlock") {
throw path.buildCodeFrameError(`Static blocks are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
}
const {

@@ -77,3 +63,2 @@ node,

} = path;
if (!path.isTSDeclareMethod()) {

@@ -88,10 +73,6 @@ new _helperReplaceSupers.default({

}
const properties = [prop("kind", _core.types.stringLiteral(_core.types.isClassMethod(node) ? node.kind : "field")), prop("decorators", takeDecorators(node)), prop("static", node.static && _core.types.booleanLiteral(true)), prop("key", getKey(node))].filter(Boolean);
if (_core.types.isClassMethod(node)) {
const id = node.computed ? null : node.key;
const transformed = _core.types.toExpression(node);
properties.push(prop("value", (0, _helperFunctionName.default)({

@@ -107,7 +88,5 @@ node: transformed,

}
path.remove();
return _core.types.objectExpression(properties);
}
function addDecorateHelper(file) {

@@ -120,7 +99,5 @@ try {

}
throw err;
}
}
function buildDecoratedClass(ref, path, elements, file) {

@@ -140,3 +117,2 @@ const {

let superId;
if (superClass) {

@@ -146,7 +122,6 @@ superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");

}
const classDecorators = takeDecorators(node);
const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId, path)));
const definitions = _core.types.arrayExpression(elements.filter(element =>
!element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId,
path)));
const wrapperCall = _core.template.expression.ast`

@@ -162,10 +137,7 @@ ${addDecorateHelper(file)}(

`;
if (!isStrict) {
wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
}
let replacement = wrapperCall;
let classPathDesc = "arguments.1.body.body.0";
if (isDeclaration) {

@@ -175,6 +147,4 @@ replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;

}
return {
instanceNodes: [_core.template.statement.ast`${_core.types.cloneNode(initializeId)}(this)`],
wrapClass(path) {

@@ -184,3 +154,2 @@ path.replaceWith(replacement);

}
};

@@ -187,0 +156,0 @@ }

39

lib/features.js

@@ -10,5 +10,3 @@ "use strict";

exports.shouldTransform = shouldTransform;
var _decorators = require("./decorators");
const FEATURES = Object.freeze({

@@ -23,14 +21,16 @@ fields: 1 << 1,

const featuresSameLoose = new Map([[FEATURES.fields, "@babel/plugin-proposal-class-properties"], [FEATURES.privateMethods, "@babel/plugin-proposal-private-methods"], [FEATURES.privateIn, "@babel/plugin-proposal-private-property-in-object"]]);
const featuresKey = "@babel/plugin-class-features/featuresKey";
const looseKey = "@babel/plugin-class-features/looseKey";
const looseLowPriorityKey = "@babel/plugin-class-features/looseLowPriorityKey/#__internal__@babel/preset-env__please-overwrite-loose-instead-of-throwing";
function enableFeature(file, feature, loose) {
if (!hasFeature(file, feature) || canIgnoreLoose(file, feature)) {
file.set(featuresKey, file.get(featuresKey) | feature);
if (loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
if (
loose === "#__internal__@babel/preset-env__prefer-true-but-false-is-ok-if-it-prevents-an-error") {
setLoose(file, feature, true);
file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);
} else if (loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error") {
} else if (
loose === "#__internal__@babel/preset-env__prefer-false-but-true-is-ok-if-it-prevents-an-error") {
setLoose(file, feature, false);

@@ -42,10 +42,7 @@ file.set(looseLowPriorityKey, file.get(looseLowPriorityKey) | feature);

}
let resolvedLoose;
let higherPriorityPluginName;
for (const [mask, name] of featuresSameLoose) {
if (!hasFeature(file, mask)) continue;
const loose = isLoose(file, mask);
if (canIgnoreLoose(file, mask)) {

@@ -60,3 +57,2 @@ continue;

}
if (resolvedLoose !== undefined) {

@@ -71,11 +67,8 @@ for (const [mask, name] of featuresSameLoose) {

}
function hasFeature(file, feature) {
return !!(file.get(featuresKey) & feature);
}
function isLoose(file, feature) {
return !!(file.get(looseKey) & feature);
}
function setLoose(file, feature, loose) {

@@ -85,7 +78,5 @@ if (loose) file.set(looseKey, file.get(looseKey) | feature);else file.set(looseKey, file.get(looseKey) & ~feature);

}
function canIgnoreLoose(file, feature) {
return !!(file.get(looseLowPriorityKey) & feature);
}
function shouldTransform(path, file) {

@@ -97,7 +88,5 @@ let decoratorPath = null;

let staticBlockPath = null;
if ((0, _decorators.hasOwnDecorators)(path.node)) {
decoratorPath = path.get("decorators.0");
}
for (const el of path.get("body.body")) {

@@ -107,15 +96,11 @@ if (!decoratorPath && (0, _decorators.hasOwnDecorators)(el.node)) {

}
if (!publicFieldPath && el.isClassProperty()) {
publicFieldPath = el;
}
if (!privateFieldPath && el.isClassPrivateProperty()) {
privateFieldPath = el;
}
if (!privateMethodPath && el.isClassPrivateMethod != null && el.isClassPrivateMethod()) {
privateMethodPath = el;
}
if (!staticBlockPath && el.isStaticBlock != null && el.isStaticBlock()) {

@@ -125,35 +110,27 @@ staticBlockPath = el;

}
if (decoratorPath && privateFieldPath) {
throw privateFieldPath.buildCodeFrameError("Private fields in decorated classes are not supported yet.");
}
if (decoratorPath && privateMethodPath) {
throw privateMethodPath.buildCodeFrameError("Private methods in decorated classes are not supported yet.");
}
if (decoratorPath && !hasFeature(file, FEATURES.decorators)) {
throw path.buildCodeFrameError("Decorators are not enabled." + "\nIf you are using " + '["@babel/plugin-proposal-decorators", { "version": "legacy" }], ' + 'make sure it comes *before* "@babel/plugin-proposal-class-properties" ' + "and enable loose mode, like so:\n" + '\t["@babel/plugin-proposal-decorators", { "version": "legacy" }]\n' + '\t["@babel/plugin-proposal-class-properties", { "loose": true }]');
}
if (privateMethodPath && !hasFeature(file, FEATURES.privateMethods)) {
throw privateMethodPath.buildCodeFrameError("Class private methods are not enabled. " + "Please add `@babel/plugin-proposal-private-methods` to your configuration.");
}
if ((publicFieldPath || privateFieldPath) && !hasFeature(file, FEATURES.fields) && !hasFeature(file, FEATURES.privateMethods)) {
if ((publicFieldPath || privateFieldPath) && !hasFeature(file, FEATURES.fields) &&
!hasFeature(file, FEATURES.privateMethods)) {
throw path.buildCodeFrameError("Class fields are not enabled. " + "Please add `@babel/plugin-proposal-class-properties` to your configuration.");
}
if (staticBlockPath && !hasFeature(file, FEATURES.staticBlocks)) {
throw path.buildCodeFrameError("Static class blocks are not enabled. " + "Please add `@babel/plugin-proposal-class-static-block` to your configuration.");
}
if (decoratorPath || privateMethodPath || staticBlockPath) {
return true;
}
if ((publicFieldPath || privateFieldPath) && hasFeature(file, FEATURES.fields)) {
return true;
}
return false;

@@ -160,0 +137,0 @@ }

@@ -10,20 +10,11 @@ "use strict";

exports.transformPrivateNamesUsage = transformPrivateNamesUsage;
var _core = require("@babel/core");
var _helperReplaceSupers = require("@babel/helper-replace-supers");
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
var _helperMemberExpressionToFunctions = require("@babel/helper-member-expression-to-functions");
var _helperOptimiseCallExpression = require("@babel/helper-optimise-call-expression");
var _helperAnnotateAsPure = require("@babel/helper-annotate-as-pure");
var ts = require("./typescript");
function buildPrivateNamesMap(props) {
const privateNamesMap = new Map();
for (const prop of props) {

@@ -39,3 +30,2 @@ if (prop.isPrivate()) {

};
if (prop.isClassPrivateMethod()) {

@@ -50,13 +40,9 @@ if (prop.node.kind === "get") {

}
privateNamesMap.set(name, update);
}
}
return privateNamesMap;
}
function buildPrivateNamesNodes(privateNamesMap, privateFieldsAsProperties, state) {
const initNodes = [];
for (const [name, value] of privateNamesMap) {

@@ -70,7 +56,4 @@ const {

const isAccessor = getId || setId;
const id = _core.types.cloneNode(value.id);
let init;
if (privateFieldsAsProperties) {

@@ -81,3 +64,2 @@ init = _core.types.callExpression(state.addHelper("classPrivateFieldLooseKey"), [_core.types.stringLiteral(name)]);

}
if (init) {

@@ -88,7 +70,6 @@ (0, _helperAnnotateAsPure.default)(init);

}
return initNodes;
}
function privateNameVisitorFactory(visitor) {
const nestedVisitor = _core.traverse.visitors.merge([Object.assign({}, visitor), _helperEnvironmentVisitor.default]);
const privateNameVisitor = Object.assign({}, visitor, {

@@ -102,3 +83,2 @@ Class(path) {

const redeclared = [];
for (const prop of body) {

@@ -123,12 +103,8 @@ if (!prop.isPrivate()) continue;

}));
path.skipKey("body");
}
});
const nestedVisitor = _core.traverse.visitors.merge([Object.assign({}, visitor), _helperEnvironmentVisitor.default]);
return privateNameVisitor;
}
const privateNameVisitor = privateNameVisitorFactory({

@@ -146,3 +122,2 @@ PrivateName(path, {

} = path;
if (!parentPath.isMemberExpression({

@@ -155,3 +130,2 @@ property: node

}
const {

@@ -164,3 +138,2 @@ name

}
});

@@ -171,3 +144,2 @@

var _scope;
scope.rename(name);

@@ -177,3 +149,2 @@ scope = scope.parent;

}
const privateInVisitor = privateNameVisitorFactory({

@@ -198,4 +169,4 @@ BinaryExpression(path) {

if (redeclared && redeclared.includes(name)) return;
unshadow(this.classRef.name, path.scope, this.innerBinding);
if (privateFieldsAsProperties) {

@@ -210,3 +181,2 @@ const {

}
const {

@@ -216,3 +186,2 @@ id,

} = privateNamesMap.get(name);
if (isStatic) {

@@ -222,6 +191,4 @@ path.replaceWith(_core.template.expression.ast`${right} === ${this.classRef}`);

}
path.replaceWith(_core.template.expression.ast`${_core.types.cloneNode(id)}.has(${right})`);
}
});

@@ -237,10 +204,7 @@ const privateNameHandlerSpec = {

const memo = scope.maybeGenerateMemoised(object);
if (!memo) {
return;
}
this.memoiser.set(object, memo, count);
},
receiver(member) {

@@ -250,10 +214,7 @@ const {

} = member.node;
if (this.memoiser.has(object)) {
return _core.types.cloneNode(this.memoiser.get(object));
}
return _core.types.cloneNode(object);
},
get(member) {

@@ -278,9 +239,8 @@ const {

const isAccessor = getId || setId;
if (isStatic) {
const helperName = isMethod && !isAccessor ? "classStaticPrivateMethodGet" : "classStaticPrivateFieldSpecGet";
unshadow(classRef.name, member.scope, innerBinding);
return _core.types.callExpression(file.addHelper(helperName), [this.receiver(member), _core.types.cloneNode(classRef), _core.types.cloneNode(id)]);
}
if (isMethod) {

@@ -292,15 +252,10 @@ if (isAccessor) {

}
console.warn(`@babel/helpers is outdated, update it to silence this warning.`);
}
return _core.types.callExpression(file.addHelper("classPrivateFieldGet"), [this.receiver(member), _core.types.cloneNode(id)]);
}
return _core.types.callExpression(file.addHelper("classPrivateMethodGet"), [this.receiver(member), _core.types.cloneNode(id), _core.types.cloneNode(methodId)]);
}
return _core.types.callExpression(file.addHelper("classPrivateFieldGet"), [this.receiver(member), _core.types.cloneNode(id)]);
},
boundGet(member) {

@@ -310,3 +265,2 @@ this.memoise(member, 1);

},
set(member, value) {

@@ -329,3 +283,2 @@ const {

const isAccessor = getId || setId;
if (isStatic) {

@@ -335,3 +288,2 @@ const helperName = isMethod && !isAccessor ? "classStaticPrivateMethodSet" : "classStaticPrivateFieldSpecSet";

}
if (isMethod) {

@@ -341,9 +293,6 @@ if (setId) {

}
return _core.types.sequenceExpression([this.receiver(member), value, _core.types.callExpression(file.addHelper("readOnlyError"), [_core.types.stringLiteral(`#${name}`)])]);
}
return _core.types.callExpression(file.addHelper("classPrivateFieldSet"), [this.receiver(member), _core.types.cloneNode(id), value]);
},
destructureSet(member) {

@@ -362,3 +311,2 @@ const {

} = privateNamesMap.get(name);
if (isStatic) {

@@ -370,9 +318,6 @@ try {

}
return _core.types.memberExpression(_core.types.callExpression(helper, [this.receiver(member), _core.types.cloneNode(classRef), _core.types.cloneNode(id)]), _core.types.identifier("value"));
}
return _core.types.memberExpression(_core.types.callExpression(file.addHelper("classPrivateFieldDestructureSet"), [this.receiver(member), _core.types.cloneNode(id)]), _core.types.identifier("value"));
},
call(member, args) {

@@ -382,3 +327,2 @@ this.memoise(member, 1);

},
optionalCall(member, args) {

@@ -388,3 +332,2 @@ this.memoise(member, 1);

}
};

@@ -409,29 +352,21 @@ const privateNameHandlerLoose = {

},
set() {
throw new Error("private name handler with loose = true don't need set()");
},
boundGet(member) {
return _core.types.callExpression(_core.types.memberExpression(this.get(member), _core.types.identifier("bind")), [_core.types.cloneNode(member.node.object)]);
},
simpleSet(member) {
return this.get(member);
},
destructureSet(member) {
return this.get(member);
},
call(member, args) {
return _core.types.callExpression(this.get(member), args);
},
optionalCall(member, args) {
return _core.types.optionalCallExpression(this.get(member), args, true);
}
};
function transformPrivateNamesUsage(ref, path, privateNamesMap, {

@@ -461,3 +396,2 @@ privateFieldsAsProperties,

}
function buildPrivateFieldInitLoose(ref, prop, privateNamesMap) {

@@ -477,3 +411,2 @@ const {

}
function buildPrivateInstanceFieldInitSpec(ref, prop, privateNamesMap, state) {

@@ -504,3 +437,2 @@ const {

}
function buildPrivateStaticFieldInitSpec(prop, privateNamesMap) {

@@ -516,3 +448,2 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

if (!prop.isProperty() && (initAdded || !isAccessor)) return;
if (isAccessor) {

@@ -532,3 +463,2 @@ privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {

}
const value = prop.node.value || prop.scope.buildUndefinedNode();

@@ -544,3 +474,2 @@ return _core.template.statement.ast`

}
function buildPrivateMethodInitLoose(ref, prop, privateNamesMap) {

@@ -556,3 +485,2 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

if (initAdded) return;
if (methodId) {

@@ -568,5 +496,3 @@ return _core.template.statement.ast`

}
const isAccessor = getId || setId;
if (isAccessor) {

@@ -587,3 +513,2 @@ privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {

}
function buildPrivateInstanceMethodInitSpec(ref, prop, privateNamesMap, state) {

@@ -598,10 +523,7 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

const isAccessor = getId || setId;
if (isAccessor) {
return buildPrivateAccessorInitialization(ref, prop, privateNamesMap, state);
}
return buildPrivateInstanceMethodInitalization(ref, prop, privateNamesMap, state);
}
function buildPrivateAccessorInitialization(ref, prop, privateNamesMap, state) {

@@ -637,3 +559,2 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

}
function buildPrivateInstanceMethodInitalization(ref, prop, privateNamesMap, state) {

@@ -655,3 +576,2 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

}
function buildPublicFieldInitLoose(ref, prop) {

@@ -665,3 +585,2 @@ const {

}
function buildPublicFieldInitSpec(ref, prop, state) {

@@ -675,3 +594,2 @@ const {

}
function buildPrivateStaticMethodInitLoose(ref, prop, state, privateNamesMap) {

@@ -688,3 +606,2 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

const isAccessor = getId || setId;
if (isAccessor) {

@@ -704,3 +621,2 @@ privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {

}
return _core.template.statement.ast`

@@ -715,3 +631,2 @@ Object.defineProperty(${ref}, ${id}, {

}
function buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties = false) {

@@ -737,3 +652,2 @@ const privateName = privateNamesMap.get(prop.node.key.id.name);

let declId = methodId;
if (isGetter) {

@@ -752,6 +666,5 @@ privateNamesMap.set(prop.node.key.id.name, Object.assign({}, privateName, {

}
return _core.types.functionDeclaration(_core.types.cloneNode(declId), params, body, generator, async);
return _core.types.functionDeclaration(_core.types.cloneNode(declId),
params, body, generator, async);
}
const thisContextVisitor = _core.traverse.visitors.merge([{

@@ -762,3 +675,2 @@ ThisExpression(path, state) {

},
MetaProperty(path) {

@@ -770,3 +682,2 @@ const meta = path.get("meta");

} = path;
if (meta.isIdentifier({

@@ -780,5 +691,3 @@ name: "new"

}
}, _helperEnvironmentVisitor.default]);
const innerReferencesVisitor = {

@@ -791,8 +700,5 @@ ReferencedIdentifier(path, state) {

}
};
function replaceThisContext(path, ref, getSuperRef, file, isStaticBlock, constantSuper, innerBindingRef) {
var _state$classRef;
const state = {

@@ -809,3 +715,2 @@ classRef: ref,

getSuperRef,
getObjectRef() {

@@ -815,6 +720,4 @@ state.needsClassRef = true;

}
});
replacer.replace();
if (isStaticBlock || path.isProperty()) {

@@ -827,6 +730,4 @@ path.traverse(thisContextVisitor, state);

}
return state.needsClassRef;
}
function isNameOrLength({

@@ -839,10 +740,7 @@ key,

}
if (key.type === "StringLiteral") {
return key.value === "name" || key.value === "length";
}
return false;
}
function buildFieldsInitNodes(ref, superRef, props, privateNamesMap, state, setPublicClassFields, privateFieldsAsProperties, constantSuper, innerBindingRef) {

@@ -856,9 +754,8 @@ let needsClassRef = false;

var _injectSuperRef;
(_injectSuperRef = injectSuperRef) != null ? _injectSuperRef : injectSuperRef = props[0].scope.generateUidIdentifierBasedOnNode(superRef);
return injectSuperRef;
};
for (const prop of props) {
prop.isClassProperty() && ts.assertFieldTransformed(prop);
const isStatic = !(_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node)) && prop.node.static;

@@ -871,3 +768,2 @@ const isInstance = !isStatic;

const isStaticBlock = prop.isStaticBlock == null ? void 0 : prop.isStaticBlock();
if (isStatic || isMethod && isPrivate || isStaticBlock) {

@@ -882,3 +778,2 @@ const replaced = replaceThisContext(prop, ref, getSuperRef, state, isStaticBlock, constantSuper, innerBindingRef);

const blockBody = prop.node.body;
if (blockBody.length === 1 && _core.types.isExpressionStatement(blockBody[0])) {

@@ -889,16 +784,14 @@ staticNodes.push(blockBody[0]);

}
break;
}
case isStatic && isPrivate && isField && privateFieldsAsProperties:
needsClassRef = true;
staticNodes.push(buildPrivateFieldInitLoose(_core.types.cloneNode(ref), prop, privateNamesMap));
staticNodes.push(
buildPrivateFieldInitLoose(_core.types.cloneNode(ref), prop, privateNamesMap));
break;
case isStatic && isPrivate && isField && !privateFieldsAsProperties:
needsClassRef = true;
staticNodes.push(buildPrivateStaticFieldInitSpec(prop, privateNamesMap));
staticNodes.push(
buildPrivateStaticFieldInitSpec(prop, privateNamesMap));
break;
case isStatic && isPublic && isField && setPublicClassFields:

@@ -910,46 +803,48 @@ if (!isNameOrLength(prop.node)) {

}
case isStatic && isPublic && isField && !setPublicClassFields:
needsClassRef = true;
staticNodes.push(buildPublicFieldInitSpec(_core.types.cloneNode(ref), prop, state));
staticNodes.push(
buildPublicFieldInitSpec(_core.types.cloneNode(ref), prop, state));
break;
case isInstance && isPrivate && isField && privateFieldsAsProperties:
instanceNodes.push(buildPrivateFieldInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
instanceNodes.push(
buildPrivateFieldInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
break;
case isInstance && isPrivate && isField && !privateFieldsAsProperties:
instanceNodes.push(buildPrivateInstanceFieldInitSpec(_core.types.thisExpression(), prop, privateNamesMap, state));
instanceNodes.push(buildPrivateInstanceFieldInitSpec(_core.types.thisExpression(),
prop, privateNamesMap, state));
break;
case isInstance && isPrivate && isMethod && privateFieldsAsProperties:
instanceNodes.unshift(buildPrivateMethodInitLoose(_core.types.thisExpression(), prop, privateNamesMap));
pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
instanceNodes.unshift(buildPrivateMethodInitLoose(_core.types.thisExpression(),
prop, privateNamesMap));
pureStaticNodes.push(buildPrivateMethodDeclaration(
prop, privateNamesMap, privateFieldsAsProperties));
break;
case isInstance && isPrivate && isMethod && !privateFieldsAsProperties:
instanceNodes.unshift(buildPrivateInstanceMethodInitSpec(_core.types.thisExpression(), prop, privateNamesMap, state));
pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
instanceNodes.unshift(buildPrivateInstanceMethodInitSpec(_core.types.thisExpression(),
prop, privateNamesMap, state));
pureStaticNodes.push(buildPrivateMethodDeclaration(
prop, privateNamesMap, privateFieldsAsProperties));
break;
case isStatic && isPrivate && isMethod && !privateFieldsAsProperties:
needsClassRef = true;
staticNodes.unshift(buildPrivateStaticFieldInitSpec(prop, privateNamesMap));
pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
staticNodes.unshift(
buildPrivateStaticFieldInitSpec(prop, privateNamesMap));
pureStaticNodes.push(buildPrivateMethodDeclaration(
prop, privateNamesMap, privateFieldsAsProperties));
break;
case isStatic && isPrivate && isMethod && privateFieldsAsProperties:
needsClassRef = true;
staticNodes.unshift(buildPrivateStaticMethodInitLoose(_core.types.cloneNode(ref), prop, state, privateNamesMap));
pureStaticNodes.push(buildPrivateMethodDeclaration(prop, privateNamesMap, privateFieldsAsProperties));
staticNodes.unshift(buildPrivateStaticMethodInitLoose(_core.types.cloneNode(ref),
prop, state, privateNamesMap));
pureStaticNodes.push(buildPrivateMethodDeclaration(
prop, privateNamesMap, privateFieldsAsProperties));
break;
case isInstance && isPublic && isField && setPublicClassFields:
instanceNodes.push(buildPublicFieldInitLoose(_core.types.thisExpression(), prop));
break;
case isInstance && isPublic && isField && !setPublicClassFields:
instanceNodes.push(buildPublicFieldInitSpec(_core.types.thisExpression(), prop, state));
instanceNodes.push(
buildPublicFieldInitSpec(_core.types.thisExpression(), prop, state));
break;
default:

@@ -959,3 +854,2 @@ throw new Error("Unreachable.");

}
return {

@@ -965,3 +859,2 @@ staticNodes: staticNodes.filter(Boolean),

pureStaticNodes: pureStaticNodes.filter(Boolean),
wrapClass(path) {

@@ -971,3 +864,2 @@ for (const prop of props) {

}
if (injectSuperRef) {

@@ -979,5 +871,3 @@ path.scope.push({

}
if (!needsClassRef) return path;
if (path.isClassExpression()) {

@@ -991,6 +881,4 @@ path.scope.push({

}
return path;
}
};

@@ -997,0 +885,0 @@ }

@@ -25,22 +25,12 @@ "use strict";

});
var _core = require("@babel/core");
var _helperFunctionName = require("@babel/helper-function-name");
var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
var _fields = require("./fields");
var _decorators = require("./decorators");
var _misc = require("./misc");
var _features = require("./features");
var _typescript = require("./typescript");
const version = "7.19.0".split(".").reduce((v, x) => v * 1e5 + +x, 0);
const version = "7.20.2".split(".").reduce((v, x) => v * 1e5 + +x, 0);
const versionKey = "@babel/plugin-class-features/version";
function createClassFeaturePlugin({

@@ -60,14 +50,10 @@ name,

const noDocumentAll = api.assumption("noDocumentAll");
if (loose === true) {
const explicit = [];
if (setPublicClassFields !== undefined) {
explicit.push(`"setPublicClassFields"`);
}
if (privateFieldsAsProperties !== undefined) {
explicit.push(`"privateFieldsAsProperties"`);
}
if (explicit.length !== 0) {

@@ -77,3 +63,2 @@ console.warn(`[${name}]: You are using the "loose: true" option and you are` + ` explicitly setting a value for the ${explicit.join(" and ")}` + ` assumption${explicit.length > 1 ? "s" : ""}. The "loose" option` + ` can cause incompatibilities with the other class features` + ` plugins, so it's recommended that you replace it with the` + ` following top-level option:\n` + `\t"assumptions": {\n` + `\t\t"setPublicClassFields": true,\n` + `\t\t"privateFieldsAsProperties": true\n` + `\t}`);

}
return {

@@ -83,6 +68,4 @@ name,

inherits,
pre(file) {
(0, _features.enableFeature)(file, feature, loose);
if (!file.get(versionKey) || file.get(versionKey) < version) {

@@ -92,3 +75,2 @@ file.set(versionKey, version);

},
visitor: {

@@ -109,8 +91,7 @@ Class(path, {

const body = path.get("body");
for (const path of body.get("body")) {
if ((path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
if (
(path.isClassProperty() || path.isClassMethod()) && path.node.computed) {
computedPaths.push(path);
}
if (path.isPrivate()) {

@@ -122,3 +103,2 @@ const {

const setName = `set ${name}`;
if (path.isClassPrivateMethod()) {

@@ -129,3 +109,2 @@ if (path.node.kind === "get") {

}
privateNames.add(getName).add(name);

@@ -136,3 +115,2 @@ } else if (path.node.kind === "set") {

}
privateNames.add(setName).add(name);

@@ -144,7 +122,5 @@ }

}
privateNames.add(name);
}
}
if (path.isClassMethod({

@@ -156,3 +132,2 @@ kind: "constructor"

elements.push(path);
if (path.isProperty() || path.isPrivate() || path.isStaticBlock != null && path.isStaticBlock()) {

@@ -163,3 +138,2 @@ props.push(path);

}
{

@@ -170,3 +144,2 @@ if (!props.length && !isDecorated) return;

let ref;
if (!innerBinding || path.isClassExpression()) {

@@ -204,3 +177,2 @@ (0, _helperFunctionName.default)(path);

}
if (instanceNodes.length > 0) {

@@ -211,3 +183,2 @@ (0, _misc.injectInitialization)(path, constructor, instanceNodes, (referenceVisitor, state) => {

}
for (const prop of props) {

@@ -222,7 +193,5 @@ if (_core.types.isStaticBlock != null && _core.types.isStaticBlock(prop.node) || prop.node.static) continue;

wrappedPath.insertBefore([...privateNamesNodes, ...keysNodes]);
if (staticNodes.length > 0) {
wrappedPath.insertAfter(staticNodes);
}
if (pureStaticNodes.length > 0) {

@@ -232,3 +201,2 @@ wrappedPath.find(parent => parent.isStatement() || parent.isDeclaration()).insertAfter(pureStaticNodes);

},
ExportDefaultDeclaration(path, {

@@ -240,3 +208,2 @@ file

const decl = path.get("declaration");
if (decl.isClassDeclaration() && (0, _decorators.hasDecorators)(decl.node)) {

@@ -251,3 +218,2 @@ if (decl.node.id) {

}
}

@@ -254,0 +220,0 @@ };

@@ -8,7 +8,4 @@ "use strict";

exports.injectInitialization = injectInitialization;
var _core = require("@babel/core");
var _helperEnvironmentVisitor = require("@babel/helper-environment-visitor");
const findBareSupers = _core.traverse.visitors.merge([{

@@ -20,3 +17,2 @@ Super(path) {

} = path;
if (parentPath.isCallExpression({

@@ -28,5 +24,3 @@ callee: node

}
}, _helperEnvironmentVisitor.default]);
const referenceVisitor = {

@@ -36,3 +30,2 @@ "TSTypeAnnotation|TypeAnnotation"(path) {

},
ReferencedIdentifier(path, {

@@ -46,11 +39,7 @@ scope

}
};
function handleClassTDZ(path, state) {
if (state.classBinding && state.classBinding === path.scope.getBinding(path.node.name)) {
const classNameTDZError = state.file.addHelper("classNameTDZError");
const throwNode = _core.types.callExpression(classNameTDZError, [_core.types.stringLiteral(path.node.name)]);
path.replaceWith(_core.types.sequenceExpression([throwNode, path.node]));

@@ -60,14 +49,10 @@ path.skip();

}
const classFieldDefinitionEvaluationTDZVisitor = {
ReferencedIdentifier: handleClassTDZ
};
function injectInitialization(path, constructor, nodes, renamer) {
if (!nodes.length) return;
const isDerived = !!path.node.superClass;
if (!constructor) {
const newConstructor = _core.types.classMethod("constructor", _core.types.identifier("constructor"), [], _core.types.blockStatement([]));
if (isDerived) {

@@ -77,6 +62,4 @@ newConstructor.params = [_core.types.restElement(_core.types.identifier("args"))];

}
[constructor] = path.get("body").unshiftContainer("body", newConstructor);
}
if (renamer) {

@@ -87,3 +70,2 @@ renamer(referenceVisitor, {

}
if (isDerived) {

@@ -93,3 +75,2 @@ const bareSupers = [];

let isFirst = true;
for (const bareSuper of bareSupers) {

@@ -107,3 +88,2 @@ if (isFirst) {

}
function extractComputedKeys(path, computedPaths, file) {

@@ -115,6 +95,4 @@ const declarations = [];

};
for (const computedPath of computedPaths) {
const computedKey = computedPath.get("key");
if (computedKey.isReferencedIdentifier()) {

@@ -125,5 +103,3 @@ handleClassTDZ(computedKey, state);

}
const computedNode = computedPath.node;
if (!computedKey.isConstantExpression()) {

@@ -139,3 +115,2 @@ const ident = path.scope.generateUidIdentifierBasedOnNode(computedNode.key);

}
return declarations;

@@ -142,0 +117,0 @@ }

@@ -7,4 +7,4 @@ "use strict";

exports.assertFieldTransformed = assertFieldTransformed;
function assertFieldTransformed(path) {
function assertFieldTransformed(path) {
if (path.node.declare) {

@@ -11,0 +11,0 @@ throw path.buildCodeFrameError(`TypeScript 'declare' fields must first be transformed by ` + `@babel/plugin-transform-typescript.\n` + `If you have already enabled that plugin (or '@babel/preset-typescript'), make sure ` + `that it runs before any plugin related to additional class features:\n` + ` - @babel/plugin-proposal-class-properties\n` + ` - @babel/plugin-proposal-private-methods\n` + ` - @babel/plugin-proposal-decorators`);

{
"name": "@babel/helper-create-class-features-plugin",
"version": "7.19.0",
"version": "7.20.2",
"author": "The Babel Team (https://babel.dev/team)",

@@ -26,3 +26,3 @@ "license": "MIT",

"@babel/helper-optimise-call-expression": "^7.18.6",
"@babel/helper-replace-supers": "^7.18.9",
"@babel/helper-replace-supers": "^7.19.1",
"@babel/helper-split-export-declaration": "^7.18.6"

@@ -34,6 +34,6 @@ },

"devDependencies": {
"@babel/core": "^7.19.0",
"@babel/core": "^7.20.2",
"@babel/helper-plugin-test-runner": "^7.18.6",
"@babel/plugin-syntax-class-static-block": "^7.14.5",
"@babel/preset-env": "^7.19.0"
"@babel/preset-env": "^7.20.2"
},

@@ -40,0 +40,0 @@ "engines": {

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

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc