Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
t.identifier(stepKey),
t.identifier("value"),
);
if (
t.isIdentifier(left) ||
t.isPattern(left) ||
t.isMemberExpression(left)
) {
// for (i of test), for ({ i } of test)
declar = t.expressionStatement(
t.assignmentExpression("=", left, stepValue),
);
} else if (t.isVariableDeclaration(left)) {
// for (let i of test)
declar = t.variableDeclaration(left.kind, [
t.variableDeclarator(left.declarations[0].id, stepValue),
]);
} else {
throw file.buildCodeFrameError(
left,
`Unknown node type ${left.type} in ForStatement`,
);
}
const template = buildForOf({
ITERATOR_HAD_ERROR_KEY: scope.generateUidIdentifier("didIteratorError"),
ITERATOR_COMPLETION: scope.generateUidIdentifier(
"iteratorNormalCompletion",
),
ITERATOR_ERROR_KEY: scope.generateUidIdentifier("iteratorError"),
ITERATOR_KEY: scope.generateUidIdentifier("iterator"),
function statementList(key, path) {
const paths: Array = path.get(key);
for (const path of paths) {
const func = path.node;
if (!path.isFunctionDeclaration()) continue;
const declar = t.variableDeclaration("let", [
t.variableDeclarator(func.id, t.toExpression(func)),
]);
// hoist it up above everything else
declar._blockHoist = 2;
// todo: name this
func.id = null;
path.replaceWith(declar);
}
}
export default function convertFunctionRest(path) {
const { node, scope } = path;
if (!hasRest(node)) return false;
let rest = node.params.pop().argument;
const argsId = t.identifier("arguments");
if (t.isPattern(rest)) {
const pattern = rest;
rest = scope.generateUidIdentifier("ref");
const declar = t.variableDeclaration("let", [
t.variableDeclarator(pattern, rest),
]);
node.body.body.unshift(declar);
}
// check and optimise for extremely common cases
const state = {
references: [],
offset: node.params.length,
argumentsNode: argsId,
outerBinding: scope.getBindingIdentifier(rest.name),
// candidate member expressions we could optimise if there are no other references
candidates: [],
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);
},
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();
}
},
};
let assignment;
if (t.isVariableDeclaration(left)) {
assignment = left;
assignment.declarations[0].init = item;
} else {
assignment = t.expressionStatement(
t.assignmentExpression("=", left, item),
);
}
const block = t.toBlock(body);
block.body.unshift(assignment);
path.replaceWith(
t.forStatement(
t.variableDeclaration("let", inits),
t.binaryExpression(
"<",
t.cloneNode(i),
t.memberExpression(t.cloneNode(array), t.identifier("length")),
),
t.updateExpression("++", t.cloneNode(i)),
block,
),
);
},
},
buildVariableDeclaration(id, init) {
const declar = t.variableDeclaration("var", [
t.variableDeclarator(t.cloneNode(id), t.cloneNode(init)),
]);
declar._blockHoist = this.blockHoist;
return declar;
}
function createCreateElementRef(path) {
const reference = path.scope.generateUidIdentifier('createElement')
const declaration = t.variableDeclaration('const', [
t.variableDeclarator(
reference,
t.memberExpression(t.identifier('React'), t.identifier('createElement'))
)
])
path.insertAfter(declaration)
path.scope.registerDeclaration(path.getNextSibling())
return reference
}
replaceRestElement(parentPath, paramPath.get("left"));
return;
}
if (paramPath.isArrayPattern() && hasRestElement(paramPath)) {
const elements = paramPath.get("elements");
for (let i = 0; i < elements.length; i++) {
replaceRestElement(parentPath, elements[i]);
}
}
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);
}
}