@@ -13,8 +13,11 @@ const {
13
13
const RuntimeGlobals = require ( "./RuntimeGlobals" ) ;
14
14
const ConstDependency = require ( "./dependencies/ConstDependency" ) ;
15
15
16
+ /** @typedef {import("estree").CallExpression } CallExpression */
16
17
/** @typedef {import("./Compiler") } Compiler */
18
+ /** @typedef {import("./Dependency").DependencyLocation } DependencyLocation */
17
19
/** @typedef {import("./javascript/JavascriptParser") } JavascriptParser */
20
+ /** @typedef {import("./javascript/JavascriptParser").Range } Range */
18
21
19
22
const nestedWebpackIdentifierTag = Symbol ( "nested webpack identifier" ) ;
20
23
const PLUGIN_NAME = "CompatibilityPlugin" ;
@@ -43,31 +46,41 @@ class CompatibilityPlugin {
43
46
)
44
47
return ;
45
48
46
- parser . hooks . call . for ( "require" ) . tap ( PLUGIN_NAME , expr => {
47
- // support for browserify style require delegator: "require(o, !0)"
48
- if ( expr . arguments . length !== 2 ) return ;
49
- const second = parser . evaluateExpression ( expr . arguments [ 1 ] ) ;
50
- if ( ! second . isBoolean ( ) ) return ;
51
- if ( second . asBool ( ) !== true ) return ;
52
- const dep = new ConstDependency ( "require" , expr . callee . range ) ;
53
- dep . loc = expr . loc ;
54
- if ( parser . state . current . dependencies . length > 0 ) {
55
- const last =
56
- parser . state . current . dependencies [
57
- parser . state . current . dependencies . length - 1
58
- ] ;
59
- if (
60
- last . critical &&
61
- last . options &&
62
- last . options . request === "." &&
63
- last . userRequest === "." &&
64
- last . options . recursive
65
- )
66
- parser . state . current . dependencies . pop ( ) ;
49
+ parser . hooks . call . for ( "require" ) . tap (
50
+ PLUGIN_NAME ,
51
+ /**
52
+ * @param {CallExpression } expr call expression
53
+ * @returns {boolean | void } true when need to handle
54
+ */
55
+ expr => {
56
+ // support for browserify style require delegator: "require(o, !0)"
57
+ if ( expr . arguments . length !== 2 ) return ;
58
+ const second = parser . evaluateExpression ( expr . arguments [ 1 ] ) ;
59
+ if ( ! second . isBoolean ( ) ) return ;
60
+ if ( second . asBool ( ) !== true ) return ;
61
+ const dep = new ConstDependency (
62
+ "require" ,
63
+ /** @type {Range } */ ( expr . callee . range )
64
+ ) ;
65
+ dep . loc = /** @type {DependencyLocation } */ ( expr . loc ) ;
66
+ if ( parser . state . current . dependencies . length > 0 ) {
67
+ const last =
68
+ parser . state . current . dependencies [
69
+ parser . state . current . dependencies . length - 1
70
+ ] ;
71
+ if (
72
+ last . critical &&
73
+ last . options &&
74
+ last . options . request === "." &&
75
+ last . userRequest === "." &&
76
+ last . options . recursive
77
+ )
78
+ parser . state . current . dependencies . pop ( ) ;
79
+ }
80
+ parser . state . module . addPresentationalDependency ( dep ) ;
81
+ return true ;
67
82
}
68
- parser . state . module . addPresentationalDependency ( dep ) ;
69
- return true ;
70
- } ) ;
83
+ ) ;
71
84
} ) ;
72
85
73
86
/**
@@ -82,7 +95,9 @@ class CompatibilityPlugin {
82
95
statement . id &&
83
96
statement . id . name === RuntimeGlobals . require
84
97
) {
85
- const newName = `__nested_webpack_require_${ statement . range [ 0 ] } __` ;
98
+ const newName = `__nested_webpack_require_${
99
+ /** @type {Range } */ ( statement . range ) [ 0 ]
100
+ } __`;
86
101
parser . tagVariable (
87
102
statement . id . name ,
88
103
nestedWebpackIdentifierTag ,
@@ -101,7 +116,9 @@ class CompatibilityPlugin {
101
116
parser . hooks . pattern
102
117
. for ( RuntimeGlobals . require )
103
118
. tap ( PLUGIN_NAME , pattern => {
104
- const newName = `__nested_webpack_require_${ pattern . range [ 0 ] } __` ;
119
+ const newName = `__nested_webpack_require_${
120
+ /** @type {Range } */ ( pattern . range ) [ 0 ]
121
+ } __`;
105
122
parser . tagVariable ( pattern . name , nestedWebpackIdentifierTag , {
106
123
name : newName ,
107
124
declaration : {
@@ -135,8 +152,11 @@ class CompatibilityPlugin {
135
152
parser . state . module . addPresentationalDependency ( dep ) ;
136
153
declaration . updated = true ;
137
154
}
138
- const dep = new ConstDependency ( name , expr . range ) ;
139
- dep . loc = expr . loc ;
155
+ const dep = new ConstDependency (
156
+ name ,
157
+ /** @type {Range } */ ( expr . range )
158
+ ) ;
159
+ dep . loc = /** @type {DependencyLocation } */ ( expr . loc ) ;
140
160
parser . state . module . addPresentationalDependency ( dep ) ;
141
161
return true ;
142
162
} ) ;
@@ -145,11 +165,11 @@ class CompatibilityPlugin {
145
165
parser . hooks . program . tap ( PLUGIN_NAME , ( program , comments ) => {
146
166
if ( comments . length === 0 ) return ;
147
167
const c = comments [ 0 ] ;
148
- if ( c . type === "Line" && c . range [ 0 ] === 0 ) {
168
+ if ( c . type === "Line" && /** @type { Range } */ ( c . range ) [ 0 ] === 0 ) {
149
169
if ( parser . state . source . slice ( 0 , 2 ) . toString ( ) !== "#!" ) return ;
150
170
// this is a hashbang comment
151
171
const dep = new ConstDependency ( "//" , 0 ) ;
152
- dep . loc = c . loc ;
172
+ dep . loc = /** @type { DependencyLocation } */ ( c . loc ) ;
153
173
parser . state . module . addPresentationalDependency ( dep ) ;
154
174
}
155
175
} ) ;
0 commit comments