1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | 12x 12x 11x 11x 8x 7x 7x 726x 7x 63x 150x 16x 19x 1x 18x 4x 14x 14x 11x 3x 2x 1x 56x 309x 34x 34x 25x 1x 24x 24x 21x 275x 141x 141x 17x 17x 16x 10x 309x 5x 16x 9x 7x 7x 4x 38x 15x 15x 1x 9x 728x 728x 728x 728x 728x 755x 755x 755x 40x 13x 32x 32x 7x 25x 6x 19x 9x 10x | import { types as t } from "@babel/core"; import flatten from "array-flatten"; export function isObjectAssignOrExtendsExpression(node) { return isObjectAssignExpression(node) || isExtendsHelperExpression(node); } export function isObjectAssignExpression(node) { if (!t.isCallExpression(node)) return false; const callee = node && node.callee; return !!(callee.object && callee.property && callee.object.name === "Object" && (callee.property.name === "assign")); } export function isExtendsHelperExpression(node) { if (!t.isCallExpression(node)) return false; const callee = node && node.callee; return isIdentifierNamed(callee, "_extends"); } // t.isImport only exists if the dynamic import syntax plugin is used, so avoid using that to make it optional. export function isImport(node) { return node && node.type === "Import"; } export function isIdentifierNamed(node, name) { return t.isIdentifier(node, { name: name }); } // This doesn't exist on babel-types export function isCommentBlock(node) { return node && node.type === "CommentBlock"; } export function getIdName(node) { return node.id && node.id.name; } export function isSuperCallExpression(expression) { return t.isCallExpression(expression) && expression.callee.type === "Super"; } // export function isSuperExpressionStatement(node) { // return isSuperCallExpression(node.expression) // } export function findPropertiesOfNode(blockScopeNode, declaration) { if (t.isFunctionDeclaration(declaration) || t.isArrowFunctionExpression(declaration)) { return null; } else if (t.isObjectExpression(declaration)) { return declaration.properties; } else Iif (t.isClass(declaration)) { return [ ...getInternalStaticThingsOfClass(declaration), ...getOtherPropertiesOfIdentifier(blockScopeNode, declaration.id.name) ]; } else if (t.isIdentifier(declaration)) { return getOtherPropertiesOfIdentifier(blockScopeNode, declaration.name); } else if (isObjectAssignOrExtendsExpression(declaration)) { return getPropertiesOfObjectAssignOrExtendHelper(declaration, blockScopeNode); } return null; } export function getInternalStaticThingsOfClass(classNode) { return classNode.body.body.filter(item => item.static); } /** * Traverse the top-level of the scope (program or function body) looking for either: * - ObjectExpression assignments to the object variable. * - Property assignments directly to our object (but not to nested properties). * * @return Array<{key, value}> */ export function getOtherPropertiesOfIdentifier(blockScopeNode, idName) { return flatten( blockScopeNode.body .map(node => { if (t.isExpressionStatement(node)) { // ID = value | ID.key = value | ID.key.nested = value const { left, right } = node.expression; if (t.isAssignmentExpression(node.expression)) { if (t.isIdentifier(left) && left.name === idName) { // ID = value Iif (t.isObjectExpression(right)) { // ID = {} return right.properties; // Array<ObjectProperty> } } else { const { object, property: key } = left; if (t.isIdentifier(object) && object.name === idName) { // ID.key = value return { key, value: right }; // ObjectProperty-like (key, value) } } } } else if (t.isVariableDeclaration(node)) { // console.log(require('util').inspect(node, { depth: 4 })); return node.declarations .filter(declaration => declaration.id.name === idName) .map(declaration => declaration.init) .filter(init => init) .filter(init => ( t.isObjectExpression(init) || isObjectAssignOrExtendsExpression(init)) ) .map(init => ( t.isObjectExpression(init) ? init.properties : getPropertiesOfObjectAssignOrExtendHelper(init, blockScopeNode) )); } }) .filter(item => item) ); } export function getPropertiesOfObjectAssignOrExtendHelper(node, blockScopeNode) { // Check all the args and recursively try to get props of identifiers (although they may be imported) return flatten( node.arguments.map(arg => { if (t.isObjectExpression(arg)) { return arg.properties; } else Eif (t.isIdentifier(arg)) { // Recursive, although props will be empty if arg is an imported object return getOtherPropertiesOfIdentifier(blockScopeNode, arg.name); } }) ); } export function getPropNames(props) { return props.map(prop => prop.key.name); } export function groupPropertiesByName(properties) { return properties && properties.reduce((accumulator, property) => { accumulator[property.key.name] = property.value; return accumulator; }, {}); } export function convertFunctionDeclarationToExpression(declaration) { return t.functionExpression(declaration.id, declaration.params, declaration.body, declaration.generator, declaration.async); } export function convertDeclarationToExpression(declaration) { if (t.isFunctionDeclaration(declaration)) { return convertFunctionDeclarationToExpression(declaration); } else { // console.log('-----', declaration.type) return declaration; } } export function isSuperPrototypeCallOf(expression, superClassName, superMethodName) { return t.isCallExpression(expression) && isCallExpressionCalling(expression, `${superClassName}.prototype.${superMethodName}.apply`); } /** * Helper to see if a call expression is calling a given method such as sap.ui.define(). * The AST is structured in reverse (defined > ui > sap) so we reverse the method call to compare. * * @param {CallExpression} expression * @param {String} dotNotationString For example, sap.ui.define or Class.prototype.method.apply */ export function isCallExpressionCalling(expression, dotNotationString) { Iif (!t.isCallExpression(expression)) return false; const callee = expression.callee; const parts = dotNotationString.split("."); let node = callee; for (const nextNamePart of parts.reverse()) { Iif (!node) return false; const nodeName = node.name || (node.property && node.property.name); // property won't be there for an anonymous function if (nodeName !== nextNamePart) return false; node = node.object; } return true; } /** * Recursively search through some parts of a node's for use of 'this'. * It checks the callee and the arguments but does not traverse into blocks. * * True scenarios include: * - this (ThisExpression) * - this.a.b (MemberExpression) * - this.thing() (CallExpression > callee > MemberExpression > ThisExpression) * - method(this) (CallExpression > arguments > ThisExpression) * - method(this.a) (CallExpression > arguments > MemberExpression > ThisExpression) * * @param {*} node */ export function isThisExpressionUsed(node) { Iif (!node) { return false; } if (t.isThisExpression(node)) { return true; } else if (t.isCallExpression(node)) { return isThisExpressionUsed(node.callee) || node.arguments.some(isThisExpressionUsed); } else if (t.isMemberExpression(node)) { // TODO: instead of recursion, we could traverse the member expressions until the deepest object, and see if that's ThisExpression return isThisExpressionUsed(node.object); } else { return false; } } |