Skip to content

Commit 00f4de6

Browse files
WIP
1 parent fad76b2 commit 00f4de6

File tree

2 files changed

+43
-27
lines changed

2 files changed

+43
-27
lines changed

utils/src/ast-grep/package-json.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ describe("package-json utilities", () => {
9999

100100
assert.strictEqual(result.length, 0);
101101
});
102+
103+
it("should not catch node in the key", () => {
104+
const input = dedent`
105+
{
106+
"scripts": {
107+
"node": "echo \"foo\""
108+
}
109+
}
110+
`;
111+
112+
const result = getNodeJsUsage(parse('json', input) as SgRoot);
113+
114+
assert.strictEqual(result.length, 0);
115+
});
102116
});
103117

104118
describe("replaceNodeJsArgs", () => {

utils/src/ast-grep/package-json.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,28 @@ export const getScriptsNode = (packageJsonRootNode: SgRoot) =>
3636
*/
3737
export const getNodeJsUsage = (packageJsonRootNode: SgRoot) =>
3838
getScriptsNode(packageJsonRootNode)
39-
.flatMap((node) =>
40-
node.findAll({
41-
rule: {
42-
kind: "string_content",
43-
// we can use bash sub-parsing but it's will be overkill
44-
regex: "\\bnode(\\.exe)?\\b",
45-
inside: {
39+
.flatMap((node) =>
40+
node.findAll({
41+
rule: {
4642
kind: "string",
43+
regex: "\\bnode(\\.exe)?\\b",
4744
inside: {
45+
field: "value",
4846
kind: "pair",
49-
}
47+
},
48+
},
49+
}).map((n) => {
50+
const raw = n.text();
51+
let unquoted = raw;
52+
if (unquoted.startsWith('"') && unquoted.endsWith('"')) {
53+
unquoted = unquoted.slice(1, -1);
5054
}
51-
}
52-
})
53-
);
55+
return {
56+
node: n,
57+
text: () => unquoted,
58+
};
59+
})
60+
);
5461

5562
/**
5663
* Replace Node.js arguments in the "scripts" node of a package.json AST.
@@ -59,30 +66,25 @@ export const getNodeJsUsage = (packageJsonRootNode: SgRoot) =>
5966
* @param edits An array to collect the edits made.
6067
*/
6168
export const replaceNodeJsArgs = (packageJsonRootNode: SgRoot, argsToValues: Record<string, string>, edits: Edit[]) => {
62-
for (const nodeJsUsageNode of getNodeJsUsage(packageJsonRootNode)) {
63-
const text = nodeJsUsageNode.text();
69+
for (const usage of getNodeJsUsage(packageJsonRootNode)) {
70+
const text = usage.text();
6471
const bashAST = astGrep.parse("bash", text).root();
65-
66-
const command = bashAST.findAll({
67-
rule: { kind: "command" }
68-
});
69-
72+
const command = bashAST.findAll({ rule: { kind: "command" } });
7073
for (const cmd of command) {
7174
const args = cmd.findAll({
7275
rule: {
7376
kind: "word",
7477
not: {
75-
inside: {
76-
kind: "command_name"
77-
}
78-
}
79-
}
78+
inside: { kind: "command_name" },
79+
},
80+
},
8081
});
81-
8282
for (const arg of args) {
83-
const newValue = argsToValues[arg.text()];
84-
85-
if (newValue) edits.push(arg.replace(newValue));
83+
const oldArg = arg.text();
84+
const newValue = argsToValues[oldArg];
85+
if (newValue) {
86+
edits.push(arg.replace(newValue));
87+
}
8688
}
8789
}
8890
}

0 commit comments

Comments
 (0)