Skip to content

Commit 3bcab67

Browse files
authored
feat(util): add removeBinding and refactor recipes to use it (#154)
1 parent d8fb9f2 commit 3bcab67

File tree

5 files changed

+558
-28
lines changed

5 files changed

+558
-28
lines changed

recipes/process-main-module/codemod.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
schema_version: "1.0"
22
name: "@nodejs/process-main-module"
3-
version: 1.0.0
3+
version: 1.0.1
44
description: Handle DEP0138 via transforming `process.mainModule` to `require.main`.
55
author: Bruno Rodrigues
66
license: MIT

recipes/process-main-module/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@nodejs/process-main-module",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Handle DEP0138 via transforming `process.mainModule` to `require.main`.",
55
"type": "module",
66
"scripts": {

recipes/process-main-module/src/workflow.ts

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Edit, SgRoot, Range } from "@codemod.com/jssg-types/main";
22
import { getNodeRequireCalls } from "@nodejs/codemod-utils/ast-grep/require-call";
33
import { removeLines } from "@nodejs/codemod-utils/ast-grep/remove-lines";
4+
import { removeBinding } from "@nodejs/codemod-utils/ast-grep/remove-binding";
45

56
/**
67
* Transforms `process.mainModule` usage to `require.main`. Handles direct global access
@@ -47,38 +48,21 @@ export default function transform(root: SgRoot): string | null {
4748
for (const declarationNode of [...requireDeclarations, ...destructureDeclarations]) {
4849
// Step 1: Get all requires from module nodule:process that is destructuring mainModule:
4950
if (declarationNode.text().includes("mainModule")) {
50-
const objectPattern = declarationNode.find({
51-
rule: {
52-
kind: "object_pattern",
53-
},
54-
});
55-
56-
if (!objectPattern) continue;
57-
58-
// Step2: Handle the destructuring import:
59-
const declarations = declarationNode.findAll({
60-
rule: {
61-
kind: "shorthand_property_identifier_pattern",
62-
},
63-
});
51+
// @ts-ignore - ast-grep types are not fully compatible with JSSG types
52+
const result = removeBinding(declarationNode, "mainModule");
6453

65-
if (declarations.length !== 0) {
54+
if (result) {
6655
patternsToReplace.push({
6756
pattern: "mainModule",
6857
});
69-
}
70-
71-
// When 'mainModule' is the only item imported, remove to whole thing to avoid an empty import
72-
if (declarations.length === 1) {
73-
linesToRemove.push(declarationNode.range());
74-
}
7558

76-
if (declarations.length > 1) {
77-
const restDeclarations = declarations
78-
.map((d) => d.text())
79-
.filter((d) => d !== "mainModule");
59+
if (result.edit) {
60+
edits.push(result.edit);
61+
}
8062

81-
edits.push(objectPattern.replace(`{ ${restDeclarations.join(", ")} }`));
63+
if (result.lineToRemove) {
64+
linesToRemove.push(result.lineToRemove);
65+
}
8266
}
8367
}
8468
}

0 commit comments

Comments
 (0)