Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function wrapSuperCall(bareSuper, superRef, thisRef, body) {
let bareSuperNode = bareSuper.node;
let call;
if (classState.isLoose) {
bareSuperNode.arguments.unshift(t.thisExpression());
if (
bareSuperNode.arguments.length === 2 &&
t.isSpreadElement(bareSuperNode.arguments[1]) &&
t.isIdentifier(bareSuperNode.arguments[1].argument, {
name: "arguments",
})
) {
// special case single arguments spread
bareSuperNode.arguments[1] = bareSuperNode.arguments[1].argument;
bareSuperNode.callee = t.memberExpression(
t.cloneNode(superRef),
t.identifier("apply"),
);
} else {
bareSuperNode.callee = t.memberExpression(
t.cloneNode(superRef),
})
) {
// special case single arguments spread
bareSuperNode.arguments[1] = bareSuperNode.arguments[1].argument;
bareSuperNode.callee = t.memberExpression(
t.cloneNode(superRef),
t.identifier("apply"),
);
} else {
bareSuperNode.callee = t.memberExpression(
t.cloneNode(superRef),
t.identifier("call"),
);
}
call = t.logicalExpression("||", bareSuperNode, t.thisExpression());
} else {
bareSuperNode = optimiseCall(
t.callExpression(classState.file.addHelper("getPrototypeOf"), [
t.cloneNode(classState.classRef),
]),
t.thisExpression(),
bareSuperNode.arguments,
);
call = t.callExpression(
classState.file.addHelper("possibleConstructorReturn"),
[t.thisExpression(), bareSuperNode],
);
}
if (
t.identifier("apply"),
);
} else {
bareSuperNode.callee = t.memberExpression(
t.cloneNode(superRef),
t.identifier("call"),
);
}
call = t.logicalExpression("||", bareSuperNode, t.thisExpression());
} else {
bareSuperNode = optimiseCall(
t.callExpression(classState.file.addHelper("getPrototypeOf"), [
t.cloneNode(classState.classRef),
]),
t.thisExpression(),
bareSuperNode.arguments,
);
call = t.callExpression(
classState.file.addHelper("possibleConstructorReturn"),
[t.thisExpression(), bareSuperNode],
);
}
if (
bareSuper.parentPath.isExpressionStatement() &&
bareSuper.parentPath.container === body.node.body &&
body.node.body.length - 1 === bareSuper.parentPath.key
) {
// this super call is the last statement in the body so we can just straight up
// turn it into a return
case isStatic && isPrivate && isMethod && loose:
needsClassRef = true;
staticNodes.push(
buildPrivateStaticMethodInitLoose(
t.cloneNode(ref),
prop,
state,
privateNamesMap,
),
);
staticNodes.unshift(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
case isInstance && isPublic && isField && loose:
instanceNodes.push(buildPublicFieldInitLoose(t.thisExpression(), prop));
break;
case isInstance && isPublic && isField && !loose:
instanceNodes.push(
buildPublicFieldInitSpec(t.thisExpression(), prop, state),
);
break;
default:
throw new Error("Unreachable.");
}
}
return {
staticNodes: staticNodes.filter(Boolean),
instanceNodes: instanceNodes.filter(Boolean),
wrapClass(path) {
for (const prop of props) {
const assigns = parameterProperties.map(p => {
let name;
if (t.isIdentifier(p)) {
name = p.name;
} else if (t.isAssignmentPattern(p) && t.isIdentifier(p.left)) {
name = p.left.name;
} else {
throw path.buildCodeFrameError(
"Parameter properties can not be destructuring patterns.",
);
}
const assign = t.assignmentExpression(
"=",
t.memberExpression(t.thisExpression(), t.identifier(name)),
t.identifier(name),
);
return t.expressionStatement(assign);
});
function insertInitializeInstanceElements(path, initializeInstanceId) {
const isBase = !path.node.superClass;
const initializeInstanceElements = t.callExpression(initializeInstanceId, [
t.thisExpression(),
]);
const constructorPath = getConstructorPath(path);
if (constructorPath) {
if (isBase) {
constructorPath
.get("body")
.unshiftContainer("body", [
t.expressionStatement(initializeInstanceElements),
]);
} else {
constructorPath.traverse(bareSupersVisitor, {
initializeInstanceElements,
});
}
} else {
case isInstance && isPrivate && isMethod && loose:
instanceNodes.unshift(
buildPrivateMethodInitLoose(
t.thisExpression(),
prop,
privateNamesMap,
),
);
staticNodes.push(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
case isInstance && isPrivate && isMethod && !loose:
instanceNodes.unshift(
buildPrivateInstanceMethodInitSpec(
t.thisExpression(),
prop,
privateNamesMap,
),
);
staticNodes.push(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
case isStatic && isPrivate && isMethod && !loose:
needsClassRef = true;
staticNodes.push(
buildPrivateStaticFieldInitSpec(prop, privateNamesMap),
);
staticNodes.unshift(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
JSXOpeningElement({ node }) {
const id = t.jsxIdentifier(TRACE_ID);
const trace = t.thisExpression();
node.attributes.push(t.jsxAttribute(id, t.jsxExpressionContainer(trace)));
},
};
buildPrivateFieldInitLoose(t.thisExpression(), prop, privateNamesMap),
);
break;
case isInstance && isPrivate && isField && !loose:
instanceNodes.push(
buildPrivateInstanceFieldInitSpec(
t.thisExpression(),
prop,
privateNamesMap,
),
);
break;
case isInstance && isPrivate && isMethod && loose:
instanceNodes.unshift(
buildPrivateMethodInitLoose(
t.thisExpression(),
prop,
privateNamesMap,
),
);
staticNodes.push(
buildPrivateMethodDeclaration(prop, privateNamesMap, loose),
);
break;
case isInstance && isPrivate && isMethod && !loose:
instanceNodes.unshift(
buildPrivateInstanceMethodInitSpec(
t.thisExpression(),
prop,
privateNamesMap,
),
);