Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
FunctionDeclaration(path) {
const { node } = path;
if (!node.generator) return;
/* path.remove();
path.scope.parent.push({ id: node.id, init: t.toExpression(node) }); */
const declaration = t.variableDeclaration('const', [
t.variableDeclarator(node.id, t.toExpression(node)),
]);
node.id = null;
// TODO: FunctionDeclaration in Lua has different scoping rules from JS, so hoisting it breaks helper
// declaration._blockHoist = 2;
path.replaceWith(declaration);
},
FunctionDeclaration(path) {
const { node } = path;
const declaration = t.variableDeclaration('const', [
t.variableDeclarator(node.id, t.toExpression(node)),
]);
node.id = null;
// TODO: FunctionDeclaration in Lua has different scoping rules from JS, so hoisting it breaks helper
// declaration._blockHoist = 2;
path.replaceWith(declaration);
},
if (!t.isVariableDeclaration(left)) return;
const pattern = left.declarations[0].id;
if (!t.isObjectPattern(pattern)) return;
const key = scope.generateUidIdentifier("ref");
node.left = t.variableDeclaration(left.kind, [
t.variableDeclarator(key, null),
]);
path.ensureBlock();
node.body.body.unshift(
t.variableDeclaration(node.left.kind, [
t.variableDeclarator(pattern, t.cloneNode(key)),
]),
);
},
// var a = { ...b, ...c }
function decoratedClassToExpression({ node, scope }) {
if (!hasClassDecorators(node) && !hasMethodDecorators(node.body.body)) {
return;
}
const ref = node.id
? t.cloneNode(node.id)
: scope.generateUidIdentifier("class");
return t.variableDeclaration("let", [
t.variableDeclarator(ref, t.toExpression(node)),
]);
}
return;
}
if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
const elements = paramPath.get("elements");
for (let i = 0; i < elements.length; i++) {
replaceRestElement(parentPath, elements[i], i, elements.length);
}
}
if (paramPath.isObjectPattern() && hasRestElement(paramPath)) {
const uid = parentPath.scope.generateUidIdentifier("ref");
const declar = t.variableDeclaration("let", [
t.variableDeclarator(paramPath.node, uid),
]);
parentPath.ensureBlock();
parentPath.get("body").unshiftContainer("body", declar);
paramPath.replaceWith(t.cloneNode(uid));
}
}
function insertProtoAliasOnce() {
if (classState.protoAlias === null) {
setState({ protoAlias: classState.scope.generateUidIdentifier("proto") });
const classProto = t.memberExpression(
classState.classRef,
t.identifier("prototype"),
);
const protoDeclaration = t.variableDeclaration("var", [
t.variableDeclarator(classState.protoAlias, classProto),
]);
classState.body.push(protoDeclaration);
}
}
ForOfStatement(path) {
const { scope } = path;
const { left, right, body } = path.node;
const i = scope.generateUidIdentifier("i");
let array = scope.maybeGenerateMemoised(right, true);
const inits = [t.variableDeclarator(i, t.numericLiteral(0))];
if (array) {
inits.push(t.variableDeclarator(array, right));
} else {
array = right;
}
const item = t.memberExpression(
t.cloneNode(array),
t.cloneNode(i),
true,
);
let assignment;
if (t.isVariableDeclaration(left)) {
assignment = left;
assignment.declarations[0].init = item;
} else {
assignment = t.expressionStatement(
t.assignmentExpression("=", left, item),
methodId,
params,
body,
generator,
async,
);
const isGetter = getId && !getterDeclared && params.length === 0;
const isSetter = setId && !setterDeclared && params.length > 0;
if (isGetter) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
getterDeclared: true,
});
return t.variableDeclaration("var", [
t.variableDeclarator(getId, methodValue),
]);
}
if (isSetter) {
privateNamesMap.set(prop.node.key.id.name, {
...privateName,
setterDeclared: true,
});
return t.variableDeclaration("var", [
t.variableDeclarator(setId, methodValue),
]);
}
if (isStatic && !loose) {
return t.variableDeclaration("var", [
t.variableDeclarator(
id,
t.functionExpression(id, params, body, generator, async),
TSEnumDeclaration(path) {
if (path.node.const) {
path.replaceWith(
types.variableDeclaration('const', [
types.variableDeclarator(
types.identifier(path.node.id.name),
types.objectExpression(
TSEnumMembersToObjectProperties(path.get('members')),
),
),
]),
);
path.skip();
}
},
};