Skip to content

Commit 4bdfd29

Browse files
authored
applyRule does not require a fix field (#1139)
1 parent ae93c94 commit 4bdfd29

2 files changed

Lines changed: 45 additions & 28 deletions

File tree

.changeset/moody-zoos-own.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
`applyRule` does not require a `fix` field

packages/open-next/src/build/patch/astCodePatcher.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ export type RuleConfig = NapiConfig & { fix?: string };
2222
/**
2323
* Returns the `Edit`s and `Match`es for an ast-grep rule in yaml format
2424
*
25-
* The rule must have a `fix` to rewrite the matched node.
26-
*
27-
* Tip: use https://ast-grep.github.io/playground.html to create rules.
25+
* Notes:
26+
* - The rule must not contain a `transform` field
27+
* - The rule may contain a `fix` field, which is used to generate the edits.
28+
* If not provided, no edits will be generated and the matches will be returned as is.
29+
* - You can use https://ast-grep.github.io/playground.html to create and verify rules.
2830
*
2931
* @param rule The rule. Either a yaml string or an instance of `RuleConfig`
3032
* @param root The root node
@@ -44,36 +46,35 @@ export function applyRule(
4446
if (ruleConfig.transform) {
4547
throw new Error("transform is not supported");
4648
}
47-
if (!ruleConfig.fix) {
48-
throw new Error("no fix to apply");
49-
}
50-
51-
const fix = ruleConfig.fix;
5249

5350
const matches = once
5451
? [root.find(ruleConfig)].filter((m) => m !== null)
5552
: root.findAll(ruleConfig);
5653

5754
const edits: Edit[] = [];
5855

59-
matches.forEach((match) => {
60-
edits.push(
61-
match.replace(
62-
// Replace known placeholders by their value
63-
fix
64-
.replace(/\$\$\$([A-Z0-9_]+)/g, (_m, name) =>
65-
match
66-
.getMultipleMatches(name)
67-
.map((n) => n.text())
68-
.join(""),
69-
)
70-
.replace(
71-
/\$([A-Z0-9_]+)/g,
72-
(m, name) => match.getMatch(name)?.text() ?? m,
73-
),
74-
),
75-
);
76-
});
56+
const fix = ruleConfig.fix;
57+
58+
if (fix !== undefined) {
59+
matches.forEach((match) => {
60+
edits.push(
61+
match.replace(
62+
// Replace known placeholders by their value
63+
fix
64+
.replace(/\$\$\$([A-Z0-9_]+)/g, (_m, name) =>
65+
match
66+
.getMultipleMatches(name)
67+
.map((n) => n.text())
68+
.join(""),
69+
)
70+
.replace(
71+
/\$([A-Z0-9_]+)/g,
72+
(m, name) => match.getMatch(name)?.text() ?? m,
73+
),
74+
),
75+
);
76+
});
77+
}
7778

7879
return { edits, matches };
7980
}
@@ -83,10 +84,21 @@ export function applyRule(
8384
*
8485
* @param path The file path
8586
* @param lang The language to parse. Defaults to TypeScript.
86-
* @returns The root for the file.
87+
* @returns The root node for the file.
8788
*/
8889
export function parseFile(path: string, lang = Lang.TypeScript): SgNode {
89-
return parse(lang, readFileSync(path, { encoding: "utf-8" })).root();
90+
return parseCode(readFileSync(path, { encoding: "utf-8" }), lang);
91+
}
92+
93+
/**
94+
* Parse a code string and obtain its root.
95+
*
96+
* @param code The code string
97+
* @param lang The language to parse. Defaults to TypeScript.
98+
* @returns The root node for the code string.
99+
*/
100+
export function parseCode(code: string, lang = Lang.TypeScript): SgNode {
101+
return parse(lang, code).root();
90102
}
91103

92104
/**

0 commit comments

Comments
 (0)