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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isObjectAssignOrExtendsExpression = isObjectAssignOrExtendsExpression; exports.isObjectAssignExpression = isObjectAssignExpression; exports.isExtendsHelperExpression = isExtendsHelperExpression; exports.isImport = isImport; exports.isIdentifierNamed = isIdentifierNamed; exports.isCommentBlock = isCommentBlock; exports.getIdName = getIdName; exports.isSuperCallExpression = isSuperCallExpression; exports.findPropertiesOfNode = findPropertiesOfNode; exports.getInternalStaticThingsOfClass = getInternalStaticThingsOfClass; exports.getOtherPropertiesOfIdentifier = getOtherPropertiesOfIdentifier; exports.getPropertiesOfObjectAssignOrExtendHelper = getPropertiesOfObjectAssignOrExtendHelper; exports.getPropNames = getPropNames; exports.groupPropertiesByName = groupPropertiesByName; exports.convertFunctionDeclarationToExpression = convertFunctionDeclarationToExpression; exports.convertDeclarationToExpression = convertDeclarationToExpression; exports.isSuperPrototypeCallOf = isSuperPrototypeCallOf; exports.isCallExpressionCalling = isCallExpressionCalling; exports.isThisExpressionUsed = isThisExpressionUsed; require("source-map-support/register"); var t = _interopRequireWildcard(require("@babel/types")); var _arrayFlatten = _interopRequireDefault(require("array-flatten")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { Eif (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } } function isObjectAssignOrExtendsExpression(node) { return isObjectAssignExpression(node) || isExtendsHelperExpression(node); } 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'); } 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. function isImport(node) { return node && node.type === 'Import'; } function isIdentifierNamed(node, name) { return t.isIdentifier(node, { name: name }); } // This doesn't exist on babel-types function isCommentBlock(node) { return node && node.type === 'CommentBlock'; } function getIdName(node) { return node.id && node.id.name; } function isSuperCallExpression(expression) { return t.isCallExpression(expression) && expression.callee.type === 'Super'; } // export function isSuperExpressionStatement(node) { // return isSuperCallExpression(node.expression) // } function findPropertiesOfNode(blockScopeNode, declaration) { if (t.isFunctionDeclaration(declaration) || t.isArrowFunctionExpression(declaration)) { return null; } else if (t.isObjectExpression(declaration)) { return declaration.properties; } else if (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; } 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}> */ function getOtherPropertiesOfIdentifier(blockScopeNode, idName) { return (0, _arrayFlatten.default)(blockScopeNode.body.map(node => { Iif (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 if (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 Iif (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)); } function getPropertiesOfObjectAssignOrExtendHelper(node, blockScopeNode) { // Check all the args and recursively try to get props of identifiers (although they may be imported) return (0, _arrayFlatten.default)(node.arguments.map(arg => { if (t.isObjectExpression(arg)) { return arg.properties; } else if (t.isIdentifier(arg)) { // Recursive, although props will be empty if arg is an imported object return getOtherPropertiesOfIdentifier(blockScopeNode, arg.name); } })); } function getPropNames(props) { return props.map(prop => prop.key.name); } function groupPropertiesByName(properties) { return properties && properties.reduce((accumulator, property) => { accumulator[property.key.name] = property.value; return accumulator; }, {}); } function convertFunctionDeclarationToExpression(declaration) { return t.functionExpression(declaration.id, declaration.params, declaration.body, declaration.generator, declaration.async); } function convertDeclarationToExpression(declaration) { if (t.isFunctionDeclaration(declaration)) { return convertFunctionDeclarationToExpression(declaration); } else { // console.log('-----', declaration.type) return declaration; } } 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 */ 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 Eif (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 */ function isThisExpressionUsed(node) { if (!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; } } |