( module: SourceModule, name: string )
| 659 | // `exports.name = value`, `module.exports = { name }` or TypeScript's |
| 660 | // re-export, `Object.defineProperty(exports, "name", { get: () => value })` |
| 661 | const commonExportOf = ( |
| 662 | module: SourceModule, |
| 663 | name: string |
| 664 | ): Node | undefined => { |
| 665 | const values: Node[] = [] |
| 666 | let isKnown = true |
| 667 | walk(module.program, node => { |
| 668 | if (!isKnown) return false |
| 669 | if (node.type === "CallExpression") { |
| 670 | const [target, key, descriptor] = node.arguments |
| 671 | if (!target || !isExportsOf(target)) return true |
| 672 | if (key?.type !== "Literal" || typeof key.value !== "string") |
| 673 | return (isKnown = false) |
| 674 | if (key.value !== name) return true |
| 675 | const property = |
| 676 | descriptor?.type === "ObjectExpression" ? |
| 677 | descriptor.properties.find( |
| 678 | property => |
| 679 | property.type === "Property" && |
| 680 | (keyNameOf(property) === "get" || keyNameOf(property) === "value") |
| 681 | ) |
| 682 | : undefined |
| 683 | const value = |
| 684 | property?.type === "Property" ? |
| 685 | unwrapExpression(property.value) |
| 686 | : undefined |
| 687 | const exported = |
| 688 | !value || property!.type !== "Property" ? undefined |
| 689 | : keyNameOf(property as never) === "value" ? value |
| 690 | : isFunctionNode(value) ? returnedOf(value) |
| 691 | : undefined |
| 692 | if (!exported) return (isKnown = false) |
| 693 | values.push(exported) |
| 694 | return true |
| 695 | } |
| 696 | if (node.type !== "AssignmentExpression") return true |
| 697 | const target = unwrapExpression(node.left) |
| 698 | const value = unwrapExpression(node.right) |
| 699 | if ( |
| 700 | target.type === "MemberExpression" && |
| 701 | isExportsOf(target.object) && |
| 702 | memberNameOf(target) === name |
| 703 | ) { |
| 704 | // e.g. `exports.a = exports.b = void 0` |
| 705 | if (node.operator !== "=") isKnown = false |
| 706 | else if ( |
| 707 | !(value.type === "UnaryExpression" && value.operator === "void") && |
| 708 | value.type !== "AssignmentExpression" |
| 709 | ) |
| 710 | values.push(value) |
| 711 | } else if (isExportsOf(target)) { |
| 712 | if (node.operator !== "=") isKnown = false |
| 713 | else if (name === "default") values.push(value) |
| 714 | // `module.exports = fn` replaces each name it exported |
| 715 | else if (value.type !== "ObjectExpression") isKnown = false |
| 716 | else { |
| 717 | for (const property of value.properties) { |
| 718 | if (property.type !== "Property") isKnown = false |
no test coverage detected