@@ -5,7 +5,10 @@ import * as t from "@babel/types";
55import { traverseJavaScriptAst } from "./javascriptSemanticTraversal.js" ;
66
77import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js" ;
8- import { propertyName } from "./javascriptAstValues.js" ;
8+ import {
9+ propertyName ,
10+ semanticStaticPropertyKey ,
11+ } from "./javascriptAstValues.js" ;
912import { stringValue } from "./javascriptStaticAnalysisHelpers.js" ;
1013
1114/** Static CommonJS export names. */
@@ -71,35 +74,54 @@ const collectAssignmentExports = (
7174 output : Set < string > ,
7275) : void => {
7376 const path = memberPath ( left ) ;
74- if ( path === "exports" || path === "module.exports" ) {
77+ if ( path === null ) return ;
78+ // Rebinding the CommonJS alias does not replace module.exports.
79+ if ( path . length === 1 && path [ 0 ] === "exports" ) return ;
80+ const exportOffset =
81+ path [ 0 ] === "exports"
82+ ? 1
83+ : path [ 0 ] === "module" && path [ 1 ] === "exports"
84+ ? 2
85+ : null ;
86+ if ( exportOffset === null ) return ;
87+ if ( path . length === exportOffset ) {
7588 if ( t . isObjectExpression ( right ) ) collectObjectKeys ( right , output ) ;
7689 else addExport ( output , "default" ) ;
7790 return ;
7891 }
79- if ( path . startsWith ( "exports." ) )
80- addExport ( output , path . slice ( "exports." . length ) ) ;
81- if ( path . startsWith ( "module.exports." ) )
82- addExport ( output , path . slice ( "module.exports." . length ) ) ;
92+ // A CommonJS export assignment exposes the first property below the
93+ // exports object. Deeper writes mutate that exported value, rather than
94+ // creating an export whose name is the dotted member path.
95+ addExport ( output , path [ exportOffset ] ! ) ;
8396} ;
8497
8598const collectCallExports = (
8699 call : t . CallExpression ,
87100 output : Set < string > ,
88101) : void => {
89102 const callee = memberPath ( call . callee ) ;
90- if ( callee === "Object.defineProperty" ) {
91- const target = memberPath ( call . arguments [ 0 ] ) ;
103+ if ( callee === null ) return ;
104+ const definePropertyCall =
105+ callee . length === 2 &&
106+ callee [ 0 ] === "Object" &&
107+ callee [ 1 ] === "defineProperty" ;
108+ const declarationCall = callee . length >= 2 && callee . at ( - 1 ) === "d" ;
109+ if ( ! definePropertyCall && ! declarationCall ) return ;
110+ const target = memberPath ( call . arguments [ 0 ] ) ;
111+ const exportTarget =
112+ target !== null &&
113+ ( ( target . length === 1 && target [ 0 ] === "exports" ) ||
114+ ( target . length === 2 &&
115+ target [ 0 ] === "module" &&
116+ target [ 1 ] === "exports" ) ) ;
117+ if ( ! exportTarget ) return ;
118+ if ( definePropertyCall ) {
92119 const name = stringValue ( call . arguments [ 1 ] ) ;
93- if ( ( target === "exports" || target === "module.exports" ) && name )
94- addExport ( output , name ) ;
120+ if ( name !== undefined ) addExport ( output , name ) ;
121+ return ;
95122 }
96- if ( ! callee . endsWith ( ".d" ) ) return ;
97- const target = memberPath ( call . arguments [ 0 ] ) ;
98123 const declarations = call . arguments [ 1 ] ;
99- if (
100- ( target === "exports" || target === "module.exports" ) &&
101- t . isObjectExpression ( declarations )
102- )
124+ if ( t . isObjectExpression ( declarations ) )
103125 collectObjectKeys ( declarations , output ) ;
104126} ;
105127
@@ -109,40 +131,50 @@ const collectObjectKeys = (
109131) : void => {
110132 for ( const property of object . properties ) {
111133 if ( ! t . isObjectProperty ( property ) && ! t . isObjectMethod ( property ) ) continue ;
112- const name = propertyName ( property . key ) ;
113- if ( name !== "" ) addExport ( output , name ) ;
134+ const name = semanticStaticPropertyKey ( property . key , property . computed ) ;
135+ // In an object literal, a non-computed `__proto__: value` sets the
136+ // prototype instead of creating an own property. Shorthand, computed,
137+ // and method forms do create own properties.
138+ if (
139+ name !== null &&
140+ ! (
141+ t . isObjectProperty ( property ) &&
142+ ! property . computed &&
143+ ! property . shorthand &&
144+ name === "__proto__"
145+ )
146+ )
147+ addExport ( output , name ) ;
114148 }
115149} ;
116150
117151const addExport = ( output : Set < string > , name : string ) : void => {
118152 output . add ( name ) ;
119153} ;
120154
121- const memberPath = ( node : t . Node | null | undefined ) : string => {
155+ const memberPath = (
156+ node : t . Node | null | undefined ,
157+ ) : readonly string [ ] | null => {
122158 const properties : string [ ] = [ ] ;
123159 let current = node ;
124160 while (
125161 current !== undefined &&
126162 current !== null &&
127163 ( t . isMemberExpression ( current ) || t . isOptionalMemberExpression ( current ) )
128164 ) {
129- properties . push ( propertyName ( current . property ) ) ;
130- current = t . isNode ( current . object ) ? current . object : undefined ;
131- }
132-
133- const root =
134- current !== undefined && t . isIdentifier ( current )
135- ? current . name
136- : current !== undefined && t . isThisExpression ( current )
137- ? "this"
138- : "" ;
139- if ( properties . length === 0 ) return root ;
140-
141- properties . reverse ( ) ;
142- const path = root === "" ? [ ] : [ root ] ;
143- for ( const property of properties ) {
144- if ( path . length === 0 && property === "" ) continue ;
145- path . push ( property ) ;
165+ const property = semanticStaticPropertyKey (
166+ current . property ,
167+ current . computed ,
168+ ) ;
169+ if ( property === null ) return null ;
170+ properties . push ( property ) ;
171+ current = current . object ;
146172 }
147- return path . join ( "." ) ;
173+ if ( current === undefined || current === null ) return null ;
174+ const root = t . isIdentifier ( current )
175+ ? current . name
176+ : t . isThisExpression ( current )
177+ ? "this"
178+ : null ;
179+ return root === null ? null : [ root , ...properties . reverse ( ) ] ;
148180} ;
0 commit comments