export function MemberExpression(node: Object) { this.print(node.object, node); if (!node.computed && t.isMemberExpression(node.property)) { throw new TypeError("Got a MemberExpression for MemberExpression property"); } let computed = node.computed; if (t.isLiteral(node.property) && isNumber(node.property.value)) { computed = true; } if (computed) { this.push("["); this.print(node.property, node); this.push("]"); } else { if (t.isNumericLiteral(node.object)) { let val = this.getPossibleRaw(node.object) || node.object.value; if (isInteger(+val) && !NON_DECIMAL_LITERAL.test(val) && !SCIENTIFIC_NOTATION.test(val) && !ZERO_DECIMAL_INTEGER.test(val) && !this.endsWith(".")) { this.push("."); } } this.push("."); this.print(node.property, node); } }
function optimiseIndexGetter(path, argsId, offset) { let index; if (t.isNumericLiteral(path.parent.property)) { index = t.numericLiteral(path.parent.property.value + offset); } else { index = t.binaryExpression("+", path.parent.property, t.numericLiteral(offset)); } path.parentPath.replaceWith(loadRest({ ARGUMENTS: argsId, INDEX: index, })); }
function getArgumentName(arg) { if (t.isThisExpression(arg)) { return 'this'; } else if (t.isNullLiteral(arg)) { return 'null'; } else if (t.isStringLiteral(arg) || t.isNumericLiteral(arg)) { return arg.value; } else if (t.isIdentifier(arg)) { return arg.name; } else { return babel_generator_1.default(arg).code; } throw new Error(`bind 不支持传入该参数: ${arg}`); }
// 入门+2 // http://forivall.com/astexplorer/ const babel = require('babel-core'); const t = require('babel-types'); const visitor = { BinaryExpression(path) { const node = path.node; let result; if (t.isNumericLiteral(node.left) && t.isNumericLiteral(node.right)) { switch (node.operator) { case '+': result = node.left.value + node.right.value; break; case '-': result = node.left.value - node.right.value; break; case '*': result = node.left.value * node.right.value; break; case '/': result = node.left.value / node.right.value; break; case '**': let i = node.right.value;
var isLiteral = (n) => t.isNumericLiteral(n) || t.isBooleanLiteral(n) || t.isNullLiteral(n) || t.isStringLiteral(n) || t.isRegExpLiteral(n);