@@ -11,6 +11,21 @@ export const CALL = Symbol("call")
11
11
export const CONSTRUCT = Symbol ( "construct" )
12
12
export const ESM = Symbol ( "esm" )
13
13
14
+ const requireCall = { require : { [ CALL ] : true } }
15
+
16
+ /**
17
+ * Check whether a given variable is modified or not.
18
+ * @param {Variable } variable The variable to check.
19
+ * @returns {boolean } `true` if the variable is modified.
20
+ */
21
+ function isModifiedGlobal ( variable ) {
22
+ return (
23
+ variable == null ||
24
+ variable . defs . length !== 0 ||
25
+ variable . references . some ( r => r . isWrite ( ) )
26
+ )
27
+ }
28
+
14
29
/**
15
30
* The reference tracker.
16
31
*/
@@ -46,7 +61,7 @@ export class ReferenceTracker {
46
61
const path = [ key ]
47
62
const variable = this . globalScope . set . get ( key )
48
63
49
- if ( variable == null || variable . defs . length !== 0 ) {
64
+ if ( isModifiedGlobal ( variable ) ) {
50
65
continue
51
66
}
52
67
@@ -62,7 +77,7 @@ export class ReferenceTracker {
62
77
const path = [ ]
63
78
const variable = this . globalScope . set . get ( key )
64
79
65
- if ( variable == null || variable . defs . length !== 0 ) {
80
+ if ( isModifiedGlobal ( variable ) ) {
66
81
continue
67
82
}
68
83
@@ -81,40 +96,24 @@ export class ReferenceTracker {
81
96
* @returns {IterableIterator<{node:Node,path:string[],type:symbol,info:any}> } The iterator to iterate references.
82
97
*/
83
98
* iterateCjsReferences ( traceMap ) {
84
- const variable = this . globalScope . set . get ( "require" )
85
-
86
- if ( variable == null || variable . defs . length !== 0 ) {
87
- return
88
- }
89
-
90
- for ( const reference of variable . references ) {
91
- const reqNode = reference . identifier
92
- const callNode = reqNode . parent
93
-
94
- if (
95
- ! reference . isRead ( ) ||
96
- callNode . type !== "CallExpression" ||
97
- callNode . callee !== reqNode
98
- ) {
99
- continue
100
- }
101
- const key = getStringIfConstant ( callNode . arguments [ 0 ] )
102
-
99
+ for ( const { node } of this . iterateGlobalReferences ( requireCall ) ) {
100
+ const key = getStringIfConstant ( node . arguments [ 0 ] )
103
101
if ( key == null || ! has ( traceMap , key ) ) {
104
102
continue
105
103
}
104
+
106
105
const nextTraceMap = traceMap [ key ]
107
106
const path = [ key ]
108
107
109
108
if ( nextTraceMap [ READ ] ) {
110
109
yield {
111
- node : callNode ,
110
+ node,
112
111
path,
113
112
type : READ ,
114
113
info : nextTraceMap [ READ ] ,
115
114
}
116
115
}
117
- yield * this . _iteratePropertyReferences ( callNode , path , nextTraceMap )
116
+ yield * this . _iteratePropertyReferences ( node , path , nextTraceMap )
118
117
}
119
118
}
120
119
0 commit comments