Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function UnaryExpression(node) {
switch (node.operator) {
case 'void':
return t.binaryExpression('and', this.transform(node.argument), t.nilLiteral());
case 'delete':
return t.assignmentStatement([this.transform(node.argument)], [t.nilLiteral()]);
case '!':
return t.unaryExpression('not', this.transform(node.argument));
case '+':
return t.callExpression(t.identifier('tonumber'), [this.transform(node.argument)]);
case '-':
case '~':
return t.unaryExpression(node.operator, this.transform(node.argument));
default:
// FIXME: Use path.buildCodeFrameError
throw new Error(`${node.operator} is unsupported unary operator`);
}
}
export function ForOfStatement(node) {
const variable = bt.isVariableDeclaration(node.left)
? this.transform(node.left.declarations[0].id)
: this.transform(node.left);
const helper = t.memberExpression(t.identifier('Reflect'), ':', t.identifier('__forOf'));
let uid = node[FOR_OF_UID];
// TODO: Apply visitor before transform in testing
if (uid == null && process.env.NODE_ENV === 'test') uid = bt.identifier('_');
if (uid == null) throw new Error('UID not found for ForOfStatement transform');
return t.forGenericStatement(
[this.transform(uid), variable],
[t.callExpression(helper, [this.transform(node.right)])],
this.transformBlock(node.body),
);
}
export function CallExpression(node) {
if (bt.isIdentifier(node.callee, { name: '__lua' }) && node.arguments.length === 1) {
const argument = node.arguments[0];
// TODO: Use path.evaluate, ref: https://github.com/babel/babel/blob/6.x/packages/babel-plugin-transform-eval/src/index.js
if (bt.isStringLiteral(argument)) {
return t.luaRaw(argument.value);
}
}
return t.callExpression(this.transform(node.callee), this.transformList(node.arguments));
}
export function ForInStatement(node) {
const variable = bt.isVariableDeclaration(node.left)
? this.transform(node.left.declarations[0].id)
: this.transform(node.left);
return t.forGenericStatement(
[variable],
[t.callExpression(t.identifier('pairs'), [this.transform(node.right)])],
this.transformBlock(node.body),
);
}