|
| 1 | +import { |
| 2 | + DOT, |
| 3 | + at, |
| 4 | + define, |
| 5 | + set |
| 6 | +} from '@flex-development/tutils' |
| 7 | +import { ok, unreachable } from 'devlop' |
| 8 | +import { CONTINUE } from 'estree-util-visit' |
| 9 | +import { is } from 'unist-util-is' |
| 10 | + |
| 11 | +function CallExpression(node, key, index) { |
| 12 | + if ( |
| 13 | + ( |
| 14 | + is(node.callee, 'Identifier') && |
| 15 | + this.identifiers.has(node.callee.name) |
| 16 | + ) || |
| 17 | + ( |
| 18 | + is(node.callee, 'MemberExpression') && |
| 19 | + is(node.callee.object, 'Identifier') && |
| 20 | + this.identifiers.has(node.callee.object.name) |
| 21 | + ) || |
| 22 | + ( |
| 23 | + is(node.callee, 'MemberExpression') && |
| 24 | + is(node.callee.object, 'Identifier') && |
| 25 | + is(node.callee.property, 'Identifier') && |
| 26 | + node.callee.object.name === 'console' && |
| 27 | + node.callee.property.name === 'assert' |
| 28 | + ) |
| 29 | + ) { |
| 30 | + ok(this.parent, 'expected `parent`') |
| 31 | + ok(key, 'expected `key`') |
| 32 | + ok(key in this.parent, `expected \`parent.${key}\``) |
| 33 | + const zero = { raw: '0', type: 'Literal', value: 0 } |
| 34 | + const void0 = { |
| 35 | + argument: zero, |
| 36 | + operator: 'void', |
| 37 | + prefix: true, |
| 38 | + type: 'UnaryExpression' |
| 39 | + } |
| 40 | + switch (this.parent.type) { |
| 41 | + case 'ArrayExpression': |
| 42 | + case 'CallExpression': |
| 43 | + ok(typeof index === 'number', 'expected `index` to be a number') |
| 44 | + set(this.parent, key + DOT + index, void0) |
| 45 | + break |
| 46 | + case 'AssignmentExpression': |
| 47 | + if (is(this.grandparent, 'ExpressionStatement')) { |
| 48 | + this.trash.add(this.grandparent) |
| 49 | + } else { |
| 50 | + define(this.parent, key, { value: void0 }) |
| 51 | + } |
| 52 | + break |
| 53 | + case 'ArrowFunctionExpression': |
| 54 | + case 'AssignmentPattern': |
| 55 | + case 'ConditionalExpression': |
| 56 | + case 'LogicalExpression': |
| 57 | + case 'Property': |
| 58 | + define(this.parent, key, { value: void0 }) |
| 59 | + break |
| 60 | + case 'AwaitExpression': |
| 61 | + if (is(this.grandparent, 'ExpressionStatement')) { |
| 62 | + this.trash.add(this.grandparent) |
| 63 | + } else { |
| 64 | + define(this.parent, key, { value: void0 }) |
| 65 | + } |
| 66 | + break |
| 67 | + case 'ExpressionStatement': |
| 68 | + this.trash.add(this.parent) |
| 69 | + break |
| 70 | + case 'ExportDefaultDeclaration': |
| 71 | + define(this.parent, key, { value: at(node.arguments, 0, void0) }) |
| 72 | + break |
| 73 | + case 'ReturnStatement': |
| 74 | + case 'YieldExpression': |
| 75 | + define(this.parent, key, { value: null }) |
| 76 | + break |
| 77 | + case 'UnaryExpression': |
| 78 | + if (is(this.grandparent, 'ExpressionStatement')) { |
| 79 | + this.trash.add(this.grandparent) |
| 80 | + } else { |
| 81 | + define(this.parent, key, { |
| 82 | + value: this.parent.operator === 'void' ? zero : void0 |
| 83 | + }) |
| 84 | + } |
| 85 | + break |
| 86 | + default: |
| 87 | + console.dir(this.parent, { depth: 10 }) |
| 88 | + void unreachable(`unexpected parent: ${this.parent.type}`) |
| 89 | + } |
| 90 | + } |
| 91 | + return CONTINUE |
| 92 | +} |
| 93 | + |
| 94 | +var call_expression_default = CallExpression |
| 95 | +export { |
| 96 | + call_expression_default as default |
| 97 | +} |
0 commit comments