* Splits a MemberExpression or CallExpression into parts. * E.g. foo.bar.baz becomes ['foo', 'bar', 'baz']
(path: NodePath<Node | null>)
| 9 | * E.g. foo.bar.baz becomes ['foo', 'bar', 'baz'] |
| 10 | */ |
| 11 | function toArray(path: NodePath<Node | null>): string[] { |
| 12 | const parts = [path]; |
| 13 | let result: string[] = []; |
| 14 | |
| 15 | while (parts.length > 0) { |
| 16 | path = parts.shift() as NodePath; |
| 17 | if (path.isCallExpression()) { |
| 18 | parts.push(path.get('callee')); |
| 19 | continue; |
| 20 | } else if (path.isMemberExpression()) { |
| 21 | parts.push(path.get('object')); |
| 22 | const property = path.get('property'); |
| 23 | |
| 24 | if (path.node.computed) { |
| 25 | const resolvedPath = resolveToValue(property); |
| 26 | |
| 27 | if (resolvedPath !== undefined) { |
| 28 | result = result.concat(toArray(resolvedPath)); |
| 29 | } else { |
| 30 | result.push('<computed>'); |
| 31 | } |
| 32 | } else if (property.isIdentifier()) { |
| 33 | result.push(property.node.name); |
| 34 | } else if (property.isPrivateName()) { |
| 35 | // new test |
| 36 | result.push(`#${property.get('id').node.name}`); |
| 37 | } |
| 38 | continue; |
| 39 | } else if (path.isIdentifier()) { |
| 40 | result.push(path.node.name); |
| 41 | continue; |
| 42 | } else if (path.isTSAsExpression()) { |
| 43 | const expression = path.get('expression'); |
| 44 | |
| 45 | if (expression.isIdentifier()) { |
| 46 | result.push(expression.node.name); |
| 47 | } |
| 48 | continue; |
| 49 | } else if (path.isLiteral() && path.node.extra?.raw) { |
| 50 | result.push(path.node.extra.raw as string); |
| 51 | continue; |
| 52 | } else if (path.isThisExpression()) { |
| 53 | result.push('this'); |
| 54 | continue; |
| 55 | } else if (path.isObjectExpression()) { |
| 56 | const properties = path.get('properties').map(function (property) { |
| 57 | if (property.isSpreadElement()) { |
| 58 | return `...${toString(property.get('argument'))}`; |
| 59 | } else if (property.isObjectProperty()) { |
| 60 | return ( |
| 61 | toString(property.get('key')) + |
| 62 | ': ' + |
| 63 | toString(property.get('value')) |
| 64 | ); |
| 65 | } else if (property.isObjectMethod()) { |
| 66 | return toString(property.get('key')) + ': <function>'; |
| 67 | } else { |
| 68 | throw new Error('Unrecognized object property type'); |
no test coverage detected
searching dependent graphs…