Skip to content

Commit 152e90f

Browse files
committed
fix
1 parent 015cd21 commit 152e90f

File tree

1 file changed

+9
-10
lines changed
  • recipes/createCredentials-to-createSecureContext/src

1 file changed

+9
-10
lines changed

recipes/createCredentials-to-createSecureContext/src/workflow.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const newImportFunction = 'createSecureContext'
77
const newImportModule = 'node:tls'
88
const oldFunctionName = 'createCredentials';
99
const oldImportModule = 'node:crypto'
10+
const newNamespace = 'tls';
1011

1112
function handleNamespaceImport(
1213
rootNode: SgRoot,
@@ -15,7 +16,6 @@ function handleNamespaceImport(
1516
importType: 'require' | 'static' | 'dynamic-await'
1617
): Edit[] {
1718
const allEdits: Edit[] = [];
18-
const newNamespace = 'tls';
1919

2020
const usages = rootNode.root().findAll({
2121
rule: {
@@ -75,7 +75,8 @@ function handleDestructuredImport(
7575
);
7676

7777
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;
7980

8081
if (spec.kind() === 'import_specifier') {
8182
keyNode = spec.field('name');
@@ -220,20 +221,18 @@ function handleRequire(
220221

221222
return [];
222223
}
224+
223225
function handleStaticImport(
224226
statement: SgNode<TypesMap, Kinds<TypesMap>>,
225227
rootNode: SgRoot,
226228
): Edit[] {
227229

228-
const modulePathNode = statement.field('source');
229230
const importClause = statement.child(1);
230-
231-
if (importClause?.kind() !== 'import_clause' || !modulePathNode) {
231+
if (importClause?.kind() !== 'import_clause') {
232232
return [];
233233
}
234-
234+
// Detects: import * as crypto from '...'
235235
const clauseContent = importClause.child(0);
236-
237236
if (!clauseContent) {
238237
return [];
239238
}
@@ -258,7 +257,6 @@ function handleStaticImport(
258257
});
259258

260259
if (usages.length > 0) {
261-
const newNamespace = 'tls';
262260

263261
for (const usage of usages) {
264262
const func = usage.field('function');
@@ -338,6 +336,7 @@ function handleDynamicImport(
338336
const idNode = statement.child(0);
339337
const declaration = statement.parent();
340338

339+
// Detects: const x = (await import(...)).then(...)
341340
if (valueNode?.kind() === 'call_expression' && idNode?.kind() === 'identifier') {
342341
const functionNode = valueNode.field('function');
343342
const isThenCall = functionNode?.kind() === 'member_expression' && functionNode.field('property')?.text() === 'then';
@@ -377,6 +376,7 @@ function handleDynamicImport(
377376
return [];
378377
}
379378

379+
// Detects: const crypto = await import(...)
380380
if (idNode?.kind() === 'identifier') {
381381
const localNamespace = idNode.text();
382382
const allEdits: Edit[] = [];
@@ -396,8 +396,6 @@ function handleDynamicImport(
396396
});
397397

398398
if (usages.length > 0) {
399-
const newNamespace = 'tls';
400-
401399
for (const usage of usages) {
402400
const func = usage.field('function');
403401
if (func) {
@@ -412,6 +410,7 @@ function handleDynamicImport(
412410
}
413411
}
414412

413+
// Detects: const { ... } = await import(...)
415414
if (idNode?.kind() === 'object_pattern') {
416415
let localFunctionName: string | null = null;
417416
let targetSpecifierNode: SgNode | null = null;

0 commit comments

Comments
 (0)