Skip to content

Commit 31b0f9b

Browse files
committed
change getRuleEdits to applyRule and return matches
1 parent c2d8219 commit 31b0f9b

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

packages/cloudflare/src/cli/build/bundle-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ async function updateWorkerBundledCode(workerOutputFile: string, buildOpts: Buil
176176

177177
const bundle = parse(Lang.TypeScript, patchedCode).root();
178178

179-
const edits = patchOptionalDependencies(bundle);
179+
const { edits } = patchOptionalDependencies(bundle);
180180

181181
await writeFile(workerOutputFile, bundle.commitEdits(edits));
182182
}

packages/cloudflare/src/cli/build/patches/ast/optional-deps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type SgNode } from "@ast-grep/napi";
22

3-
import { getRuleEdits } from "./util.js";
3+
import { applyRule } from "./util.js";
44

55
/**
66
* Handle optional dependencies.
@@ -31,5 +31,5 @@ fix: |-
3131
`;
3232

3333
export function patchOptionalDependencies(root: SgNode) {
34-
return getRuleEdits(optionalDepRule, root);
34+
return applyRule(optionalDepRule, root);
3535
}

packages/cloudflare/src/cli/build/patches/ast/util.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import yaml from "yaml";
88
export type RuleConfig = NapiConfig & { fix?: string };
99

1010
/**
11-
* Returns the `Edit`s for an ast-grep rule in yaml format
11+
* Returns the `Edit`s and `Match`es for an ast-grep rule in yaml format
1212
*
1313
* The rule must have a `fix` to rewrite the matched node.
1414
*
@@ -17,9 +17,9 @@ export type RuleConfig = NapiConfig & { fix?: string };
1717
* @param rule The rule. Either a yaml string or an instance of `RuleConfig`
1818
* @param root The root node
1919
* @param once only apply once
20-
* @returns A list of edits.
20+
* @returns A list of edits and a list of matches.
2121
*/
22-
export function getRuleEdits(rule: string | RuleConfig, root: SgNode, { once = false } = {}) {
22+
export function applyRule(rule: string | RuleConfig, root: SgNode, { once = false } = {}) {
2323
const ruleConfig: RuleConfig = typeof rule === "string" ? yaml.parse(rule) : rule;
2424
if (ruleConfig.transform) {
2525
throw new Error("transform is not supported");
@@ -50,7 +50,7 @@ export function getRuleEdits(rule: string | RuleConfig, root: SgNode, { once = f
5050
);
5151
});
5252

53-
return edits;
53+
return { edits, matches };
5454
}
5555

5656
/**
@@ -71,6 +71,6 @@ export function patchCode(
7171
{ lang = Lang.TypeScript, once = false } = {}
7272
): string {
7373
const node = parse(lang, code).root();
74-
const edits = getRuleEdits(rule, node, { once });
74+
const { edits } = applyRule(rule, node, { once });
7575
return node.commitEdits(edits);
7676
}

0 commit comments

Comments
 (0)