Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/open-next/src/build/patch/astCodePatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
import yaml from "yaml";
import type { PatchCodeFn } from "./codePatcher";

export type * from "@ast-grep/napi";

/**
* fix has the same meaning as in yaml rules
* see https://ast-grep.github.io/guide/rewrite-code.html#using-fix-in-yaml-rule
Expand All @@ -33,7 +35,10 @@ export function applyRule(
rule: string | RuleConfig,
root: SgNode,
{ once = false } = {},
) {
): {
edits: Edit[];
matches: SgNode[];
} {
const ruleConfig: RuleConfig =
typeof rule === "string" ? yaml.parse(rule) : rule;
if (ruleConfig.transform) {
Expand Down Expand Up @@ -80,7 +85,7 @@ export function applyRule(
* @param lang The language to parse. Defaults to TypeScript.
* @returns The root for the file.
*/
export function parseFile(path: string, lang = Lang.TypeScript) {
export function parseFile(path: string, lang = Lang.TypeScript): SgNode {
return parse(lang, readFileSync(path, { encoding: "utf-8" })).root();
}

Expand All @@ -106,6 +111,11 @@ export function patchCode(
return node.commitEdits(edits);
}

/**
* @param rule
* @param lang
* @returns a callback applying the the rule.
*/
export function createPatchCode(
rule: string | RuleConfig,
lang = Lang.TypeScript,
Expand Down