@@ -127,14 +127,7 @@ function getErrorCode(
127
127
}
128
128
129
129
function transform ( code : string , relativeFilePath : string ) {
130
- // If the code doesn't seem to contain anything invariant-related, we
131
- // can skip parsing and transforming it.
132
- if ( ! / i n v a r i a n t / i. test ( code ) ) {
133
- return code ;
134
- }
135
-
136
130
const ast = reparse ( code ) ;
137
- let addedDEV = false ;
138
131
139
132
recast . visit ( ast , {
140
133
visitCallExpression ( path ) {
@@ -198,62 +191,31 @@ function transform(code: string, relativeFilePath: string) {
198
191
if ( isDEVLogicalAnd ( path . parent . node ) ) {
199
192
return newNode ;
200
193
}
201
- addedDEV = true ;
202
194
return b . logicalExpression ( '&&' , makeDEVExpr ( ) , newNode ) ;
203
195
}
204
196
} ,
205
197
} ) ;
206
198
207
- if ( addedDEV ) {
208
- // Make sure there's an import { __DEV__ } from "../utilities/globals" or
209
- // similar declaration in any module where we injected __DEV__.
210
- let foundExistingImportDecl = false ;
211
-
199
+ if ( ! [ 'utilities/globals/index.js' , 'config/jest/setup.js' ] . includes ( relativeFilePath ) )
212
200
recast . visit ( ast , {
213
- visitImportDeclaration ( path ) {
201
+ visitIdentifier ( path ) {
214
202
this . traverse ( path ) ;
215
203
const node = path . node ;
216
- const importedModuleId = node . source . value ;
217
-
218
- // Normalize node.source.value relative to the current file.
219
- if (
220
- typeof importedModuleId === 'string' &&
221
- importedModuleId . startsWith ( '.' )
222
- ) {
223
- const normalized = posix . normalize (
224
- posix . join ( posix . dirname ( relativeFilePath ) , importedModuleId )
204
+ if ( isDEVExpr ( node ) ) {
205
+ return b . binaryExpression (
206
+ '!==' ,
207
+ b . memberExpression (
208
+ b . identifier ( 'globalThis' ) ,
209
+ b . identifier ( '__DEV__' )
210
+ ) ,
211
+ b . literal ( false )
225
212
) ;
226
- if ( normalized === 'utilities/globals' ) {
227
- foundExistingImportDecl = true ;
228
- if (
229
- node . specifiers ?. some ( ( s ) =>
230
- isIdWithName ( s . local || s . id , '__DEV__' )
231
- )
232
- ) {
233
- return false ;
234
- }
235
- if ( ! node . specifiers ) node . specifiers = [ ] ;
236
- node . specifiers . push ( b . importSpecifier ( b . identifier ( '__DEV__' ) ) ) ;
237
- return false ;
238
- }
239
213
}
214
+
215
+ return node ;
240
216
} ,
241
217
} ) ;
242
218
243
- if ( ! foundExistingImportDecl ) {
244
- // We could modify the AST to include a new import declaration, but since
245
- // this code is running at build time, we can simplify things by throwing
246
- // here, because we expect invariant and InvariantError to be imported
247
- // from the utilities/globals subpackage.
248
- throw new Error (
249
- `Missing import from "${ posix . relative (
250
- posix . dirname ( relativeFilePath ) ,
251
- 'utilities/globals'
252
- ) } in ${ relativeFilePath } `
253
- ) ;
254
- }
255
- }
256
-
257
219
return reprint ( ast ) ;
258
220
}
259
221
0 commit comments