Skip to content

Commit 6d277fb

Browse files
update from feedback
Co-Authored-By: Bruno Rodrigues <[email protected]>
1 parent 9b5788e commit 6d277fb

File tree

2 files changed

+28
-46
lines changed

2 files changed

+28
-46
lines changed

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

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ describe("package-json utilities", () => {
1414
const input = dedent`
1515
{
1616
"scripts": {
17-
"test": "echo \\"Error: no test specified\\" && exit 1"
17+
"test": "echo \"Error: no test specified\" && exit 1"
1818
}
1919
}
2020
`;
2121

2222
const result = getScriptsNode(parse('json', input));
2323

2424
assert(result);
25-
assert.strictEqual(result.children().length, 3); // curly braces + pair + curly braces
25+
assert.strictEqual(result.length, 1); // Number of children in the scripts node
2626
});
2727

2828
it("should return empty array if any scripts is present", () => {
@@ -37,23 +37,6 @@ describe("package-json utilities", () => {
3737

3838
assert.strictEqual(result.length, 0);
3939
});
40-
41-
it("should throw an error if multiple scripts nodes are found", () => {
42-
const input = dedent`
43-
{
44-
"scripts": {
45-
"test": "echo \\"Error: no test specified\\" && exit 1"
46-
},
47-
"scripts": {
48-
"start": "node index.js"
49-
}
50-
}
51-
`;
52-
53-
assert.throws(() => getScriptsNode(parse('json', input)), {
54-
message: /Multiple "scripts" fields found/
55-
});
56-
});
5740
});
5841

5942
describe("getNodeJsUsage", () => {
@@ -62,7 +45,7 @@ describe("package-json utilities", () => {
6245
{
6346
"scripts": {
6447
"start": "node script.js",
65-
"test": "echo \\"Error: no test specified\\" && exit 1"
48+
"test": "echo \"Error: no test specified\" && exit 1"
6649
}
6750
}
6851
`;
@@ -94,7 +77,7 @@ describe("package-json utilities", () => {
9477
{
9578
"scripts": {
9679
"start": "node.exe script.js",
97-
"test": "echo \\"Error: no test specified\\" && exit 1"
80+
"test": "echo \"Error: no test specified\" && exit 1"
9881
}
9982
}
10083
`;
@@ -110,7 +93,7 @@ describe("package-json utilities", () => {
11093
{
11194
"scripts": {
11295
"start": "npm run build",
113-
"test": "echo \\"Error: no test specified\\" && exit 1"
96+
"test": "echo \"Error: no test specified\" && exit 1"
11497
}
11598
}
11699
`;

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,26 @@ import type { SgRoot, Edit } from "@codemod.com/jssg-types/main";
55
* @param packageJsonRootNode The root node of the package.json AST.
66
* @returns The "scripts" node, or null if not found.
77
*/
8-
export const getScriptsNode = (packageJsonRootNode: SgRoot) => {
9-
const scriptsNodes = packageJsonRootNode
10-
.root()
11-
.findAll({
12-
rule: {
13-
kind: "pair",
14-
has: {
15-
field: "key",
16-
kind: "string",
8+
export const getScriptsNode = (packageJsonRootNode: SgRoot) =>
9+
packageJsonRootNode.root().findAll({
10+
rule: {
11+
kind: "pair",
12+
inside: {
13+
kind: "object",
14+
inside: {
15+
kind: "pair",
1716
has: {
18-
kind: "string_content",
19-
regex: "scripts"
20-
}
21-
}
22-
}
23-
});
24-
25-
if (scriptsNodes.length > 1)
26-
throw new Error(`Multiple "scripts" fields found in ${packageJsonRootNode.filename()}`);
27-
28-
return scriptsNodes[0] ?? null;
29-
};
17+
field: "key",
18+
kind: "string",
19+
has: {
20+
kind: "string_content",
21+
regex: "scripts",
22+
},
23+
},
24+
},
25+
},
26+
},
27+
});
3028

3129
/**
3230
* Get all usage of Node.js in the "scripts" node of a package.json AST.
@@ -38,8 +36,8 @@ export const getNodeJsUsage = (packageJsonRootNode: SgRoot) => {
3836

3937
if (!scriptsNode) return [];
4038

41-
return scriptsNode
42-
.findAll({
39+
return scriptsNode.flatMap((node) =>
40+
node.findAll({
4341
rule: {
4442
kind: "string_content",
4543
regex: "\\bnode(\\.exe)?\\b",
@@ -50,7 +48,8 @@ export const getNodeJsUsage = (packageJsonRootNode: SgRoot) => {
5048
}
5149
}
5250
}
53-
});
51+
})
52+
);
5453
};
5554

5655
/**

0 commit comments

Comments
 (0)