Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix ast-grep runner when there are special characters #1495

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/pretty-monkeys-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"codemod": patch
---

Make ast-grep runner unicode-aware
1 change: 0 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@
"@ast-grep/cli": "catalog:",
"@ast-grep/napi": "catalog:",
"@codemod.com/workflow": "workspace:*",
"@nodejs-loaders/alias": "^1.1.1",
"@octokit/rest": "catalog:",
"blessed": "catalog:",
"esbuild": "^0.23.0",
Expand Down
34 changes: 27 additions & 7 deletions packages/runner/src/engines/ast-grep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,45 @@ export const runAstGrepCodemod = async (
? `powershell -Command "${astCommandBase}"`
: astCommandBase;

const { stdout } = await execPromise(astCommand);
const { stdout, stderr } = await execPromise(astCommand);
const matches = JSON.parse(stdout.trim()) as AstGrepCompactOutput[];
// Sort in reverse order to not mess up replacement offsets
matches.sort((a, b) => b.range.byteOffset.start - a.range.byteOffset.start);

let newData = oldData;
const encoder = new TextEncoder();
const decoder = new TextDecoder();
const oldDataBytes = encoder.encode(oldData);

let newDataBytes = oldDataBytes;

for (const result of matches) {
const { replacementOffsets, replacement } = result;
if (!replacementOffsets) {
continue;
}
const replacementBytes = encoder.encode(replacement || "");

const startOffset = replacementOffsets.start;
const endOffset = replacementOffsets.end;
const newSize =
newDataBytes.length - (endOffset - startOffset) + replacementBytes.length;
const updatedBytes = new Uint8Array(newSize);

updatedBytes.set(newDataBytes.subarray(0, startOffset));

newData =
newData.slice(0, replacementOffsets.start) +
replacement +
newData.slice(replacementOffsets.end);
updatedBytes.set(replacementBytes, startOffset);

updatedBytes.set(
newDataBytes.subarray(endOffset),
startOffset + replacementBytes.length,
);

newDataBytes = updatedBytes;
}

if (typeof newData !== "string" || oldData === newData) {
const newData = decoder.decode(newDataBytes);

if (oldData === newData) {
return commands;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/workflow/src/astGrep/astGrep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const fileExtensionToLang: Record<string, Lang> = {
cpp: Lang.Cpp,
hpp: Lang.Cpp,
cs: Lang.CSharp,
dart: Lang.Dart,
ex: Lang.Elixir,
exs: Lang.Elixir,
go: Lang.Go,
Expand Down Expand Up @@ -153,6 +152,7 @@ const runExternalAstGrepRule = async (rule: any, filepath: string) => {
path.dirname(require.resolve("@ast-grep/cli/package.json")),
binaries.sg,
);
console.log(binaryPath);
await execFile(binaryPath, [
"scan",
"--update-all",
Expand Down
Loading
Loading