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 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 UpdateExpression(node, parent) {
if (!bt.isExpressionStatement(parent)) {
// FIXME: Use path.buildCodeFrameError
throw new Error("UpdateExpression's not in ExpressionStatement's are not supported.");
}
if (node.operator !== '++' && node.operator !== '--') {
// FIXME: Use path.buildCodeFrameError
throw new Error(`Invalid UpdateExpression operator ${node.operator}`);
}
const right = t.binaryExpression(
node.operator === '++' ? '+' : '-',
this.transform(node.argument),
t.numericLiteral(1),
);
return t.assignmentStatement([this.transform(node.argument)], [right]);
}
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 LogicalExpression(node) {
if (node.operator !== '&&' && node.operator !== '||') {
// FIXME: Use path.buildCodeFrameError
throw new Error(`${node.operator} is unsupported logical operator`);
}
const operator = node.operator === '&&' ? 'and' : 'or';
return t.binaryExpression(operator, this.transform(node.left), this.transform(node.right));
}
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`);
}
}
return function FunctionHandler(node) {
const body = this.transformBlock(node.body);
const restParam = node.params.find(param => bt.isRestElement(param));
if (restParam) {
body.unshift(
t.localStatement(
[restParam.argument],
[t.tableConstructorExpression([t.tableValue(t.varargLiteral())])],
),
);
}
return t.functionStatement(
isExp ? null : this.transform(node.id),
this.transformList(node.params),
// Simulate module structure for declaration
!isExp,
body,
);
};
}
export function UpdateExpression(node, parent) {
if (!bt.isExpressionStatement(parent)) {
// FIXME: Use path.buildCodeFrameError
throw new Error("UpdateExpression's not in ExpressionStatement's are not supported.");
}
if (node.operator !== '++' && node.operator !== '--') {
// FIXME: Use path.buildCodeFrameError
throw new Error(`Invalid UpdateExpression operator ${node.operator}`);
}
const right = t.binaryExpression(
node.operator === '++' ? '+' : '-',
this.transform(node.argument),
t.numericLiteral(1),
);
return t.assignmentStatement([this.transform(node.argument)], [right]);
}