(
literal: Extract<Node, { type: "ObjectExpression" }>,
module: SourceModule,
property: PropertyNode
)
| 2815 | // another object: neither through references to `literal`'s binding nor through |
| 2816 | // `this` inside `literal`'s own methods/getters or external methods called on it |
| 2817 | const isContained = ( |
| 2818 | literal: Extract<Node, { type: "ObjectExpression" }>, |
| 2819 | module: SourceModule, |
| 2820 | property: PropertyNode |
| 2821 | ): boolean => { |
| 2822 | const fnKey = propertyKeyOf(property) |
| 2823 | if (fnKey === undefined) return false |
| 2824 | const isMethod = property.kind === "init" |
| 2825 | const [operand, declarator] = operatorOf(literal) |
| 2826 | if ( |
| 2827 | declarator?.type !== "VariableDeclarator" || |
| 2828 | declarator.init !== operand || |
| 2829 | declarator.id.type !== "Identifier" |
| 2830 | ) |
| 2831 | return false |
| 2832 | const binding = module.scopes.bindingsById.get(declarator.id) |
| 2833 | if (!binding || binding.exportNames.length) return false |
| 2834 | const ownKeys = new Set<string>() |
| 2835 | // keys whose value is not inline, so calling `literal.<key>()` runs external |
| 2836 | // code with `this === literal` that could detach `this.<fnKey>` |
| 2837 | const externalKeys = new Set<string>() |
| 2838 | for (const prop of literal.properties) { |
| 2839 | if (prop.type !== "Property") return false |
| 2840 | const key = propertyKeyOf(prop) |
| 2841 | if (key === undefined) return false |
| 2842 | // a non-null __proto__ inherits getters/methods that run with `this === literal` |
| 2843 | if (key === "__proto__" && !prop.computed && prop.kind === "init") { |
| 2844 | if (isNonNullProtoProperty(prop)) return false |
| 2845 | continue |
| 2846 | } |
| 2847 | ownKeys.add(key) |
| 2848 | if (prop !== property && prop.kind === "init") { |
| 2849 | const value = unwrapExpression(prop.value) |
| 2850 | if ( |
| 2851 | value.type !== "FunctionExpression" && |
| 2852 | value.type !== "ArrowFunctionExpression" && |
| 2853 | value.type !== "Literal" && |
| 2854 | value.type !== "TemplateLiteral" && |
| 2855 | value.type !== "ObjectExpression" && |
| 2856 | value.type !== "ArrayExpression" |
| 2857 | ) |
| 2858 | externalKeys.add(key) |
| 2859 | } |
| 2860 | } |
| 2861 | const refsContained = binding.references.every(reference => { |
| 2862 | if (reference.isWrite) return false |
| 2863 | const [read, use] = operatorOf(reference.id) |
| 2864 | if ( |
| 2865 | use?.type !== "MemberExpression" || |
| 2866 | use.object !== read || |
| 2867 | isWriteTarget(use) |
| 2868 | ) |
| 2869 | return false |
| 2870 | const name = memberNameOf(use) |
| 2871 | if (typeof name !== "string") return false |
| 2872 | // e.g. `other.valueOf()`, `other.__lookupGetter__("x")`, `other.__proto__` |
| 2873 | if (!ownKeys.has(name) && name in Object.prototype) return false |
| 2874 | const isDirectCall = isDirectCallMember(use) |
no test coverage detected