function getPropRef(node, nodes, file, scope) { const prop = node.property; const key = t.toComputedKey(node, prop); if (t.isLiteral(key) && t.isPureish(key)) return key; const temp = scope.generateUidIdentifierBasedOnNode(prop); nodes.push(t.variableDeclaration("var", [ t.variableDeclarator(temp, prop) ])); return temp; }
isPure(node, constantsOnly?: boolean) { if (t.isIdentifier(node)) { let binding = this.getBinding(node.name); if (!binding) return false; if (constantsOnly) return binding.constant; return true; } else if (t.isClass(node)) { if (node.superClass && !this.isPure(node.superClass, constantsOnly)) return false; return this.isPure(node.body, constantsOnly); } else if (t.isClassBody(node)) { for (let method of node.body) { if (!this.isPure(method, constantsOnly)) return false; } return true; } else if (t.isBinary(node)) { return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly); } else if (t.isArrayExpression(node)) { for (let elem of (node.elements: Array<Object>)) { if (!this.isPure(elem, constantsOnly)) return false; } return true; } else if (t.isObjectExpression(node)) { for (let prop of (node.properties: Array<Object>)) { if (!this.isPure(prop, constantsOnly)) return false; } return true; } else if (t.isClassMethod(node)) { if (node.computed && !this.isPure(node.key, constantsOnly)) return false; if (node.kind === "get" || node.kind === "set") return false; return true; } else if (t.isClassProperty(node) || t.isObjectProperty(node)) { if (node.computed && !this.isPure(node.key, constantsOnly)) return false; return this.isPure(node.value, constantsOnly); } else if (t.isUnaryExpression(node)) { return this.isPure(node.argument, constantsOnly); } else { return t.isPureish(node); } }