@@ -7,6 +7,7 @@ const newImportFunction = 'createSecureContext'
7
7
const newImportModule = 'node:tls'
8
8
const oldFunctionName = 'createCredentials' ;
9
9
const oldImportModule = 'node:crypto'
10
+ const newNamespace = 'tls' ;
10
11
11
12
function handleNamespaceImport (
12
13
rootNode : SgRoot ,
@@ -15,7 +16,6 @@ function handleNamespaceImport(
15
16
importType : 'require' | 'static' | 'dynamic-await'
16
17
) : Edit [ ] {
17
18
const allEdits : Edit [ ] = [ ] ;
18
- const newNamespace = 'tls' ;
19
19
20
20
const usages = rootNode . root ( ) . findAll ( {
21
21
rule : {
@@ -75,7 +75,8 @@ function handleDestructuredImport(
75
75
) ;
76
76
77
77
for ( const spec of relevantSpecifiers ) {
78
- let keyNode , aliasNode ;
78
+ let keyNode : SgNode < TypesMap , Kinds < TypesMap > > | null = null ;
79
+ let aliasNode : SgNode < TypesMap , Kinds < TypesMap > > | null = null ;
79
80
80
81
if ( spec . kind ( ) === 'import_specifier' ) {
81
82
keyNode = spec . field ( 'name' ) ;
@@ -220,20 +221,18 @@ function handleRequire(
220
221
221
222
return [ ] ;
222
223
}
224
+
223
225
function handleStaticImport (
224
226
statement : SgNode < TypesMap , Kinds < TypesMap > > ,
225
227
rootNode : SgRoot ,
226
228
) : Edit [ ] {
227
229
228
- const modulePathNode = statement . field ( 'source' ) ;
229
230
const importClause = statement . child ( 1 ) ;
230
-
231
- if ( importClause ?. kind ( ) !== 'import_clause' || ! modulePathNode ) {
231
+ if ( importClause ?. kind ( ) !== 'import_clause' ) {
232
232
return [ ] ;
233
233
}
234
-
234
+ // Detects: import * as crypto from '...'
235
235
const clauseContent = importClause . child ( 0 ) ;
236
-
237
236
if ( ! clauseContent ) {
238
237
return [ ] ;
239
238
}
@@ -258,7 +257,6 @@ function handleStaticImport(
258
257
} ) ;
259
258
260
259
if ( usages . length > 0 ) {
261
- const newNamespace = 'tls' ;
262
260
263
261
for ( const usage of usages ) {
264
262
const func = usage . field ( 'function' ) ;
@@ -338,6 +336,7 @@ function handleDynamicImport(
338
336
const idNode = statement . child ( 0 ) ;
339
337
const declaration = statement . parent ( ) ;
340
338
339
+ // Detects: const x = (await import(...)).then(...)
341
340
if ( valueNode ?. kind ( ) === 'call_expression' && idNode ?. kind ( ) === 'identifier' ) {
342
341
const functionNode = valueNode . field ( 'function' ) ;
343
342
const isThenCall = functionNode ?. kind ( ) === 'member_expression' && functionNode . field ( 'property' ) ?. text ( ) === 'then' ;
@@ -377,6 +376,7 @@ function handleDynamicImport(
377
376
return [ ] ;
378
377
}
379
378
379
+ // Detects: const crypto = await import(...)
380
380
if ( idNode ?. kind ( ) === 'identifier' ) {
381
381
const localNamespace = idNode . text ( ) ;
382
382
const allEdits : Edit [ ] = [ ] ;
@@ -396,8 +396,6 @@ function handleDynamicImport(
396
396
} ) ;
397
397
398
398
if ( usages . length > 0 ) {
399
- const newNamespace = 'tls' ;
400
-
401
399
for ( const usage of usages ) {
402
400
const func = usage . field ( 'function' ) ;
403
401
if ( func ) {
@@ -412,6 +410,7 @@ function handleDynamicImport(
412
410
}
413
411
}
414
412
413
+ // Detects: const { ... } = await import(...)
415
414
if ( idNode ?. kind ( ) === 'object_pattern' ) {
416
415
let localFunctionName : string | null = null ;
417
416
let targetSpecifierNode : SgNode | null = null ;
0 commit comments