agsamantha/node_modules/jintr/dist/nodes/UpdateExpression.js
2024-10-02 15:15:21 -05:00

34 lines
1.6 KiB
JavaScript

import BaseJSNode from './BaseJSNode.js';
export default class UpdateExpression extends BaseJSNode {
run() {
const operator = this.node.operator;
switch (operator) {
case '++':
{
if (this.node.argument.type === 'MemberExpression') {
const target_node = this.visitor.visitNode(this.node.argument.object);
return target_node[this.visitor.visitNode(this.node.argument.property)]++;
}
else if (this.node.argument.type === 'Identifier') {
let target_node = this.visitor.visitNode(this.node.argument);
this.visitor.scope.set(this.node.argument.name, target_node + 1);
return this.node.prefix ? ++target_node : target_node;
}
}
break;
case '--':
{
if (this.node.argument.type === 'MemberExpression') {
const target_node = this.visitor.visitNode(this.node.argument.object);
return target_node[this.visitor.visitNode(this.node.argument.property)]--;
}
else if (this.node.argument.type === 'Identifier') {
let target_node = this.visitor.visitNode(this.node.argument);
this.visitor.scope.set(this.node.argument.name, target_node - 1);
return this.node.prefix ? --target_node : target_node;
}
}
break;
}
}
}