codemod 2.0.0-alpha.2 → 2.0.0-alpha.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/package.json +14 -16
- package/register.d.mts +2 -11
- package/register.mjs +53 -46
- package/runtime/addon.d.mts +119 -0
- package/runtime/addon.mjs +36 -40
- package/runtime/ast-grep.d.mts +20 -0
- package/runtime/ast-grep.mjs +50 -61
- package/runtime/context.d.mts +25 -0
- package/runtime/context.mjs +10 -64
- package/runtime/hooks.d.mts +2 -0
- package/runtime/hooks.mjs +4 -6
- package/runtime/host.d.mts +81 -81
- package/runtime/host.mjs +428 -326
- package/runtime/llm.d.mts +2 -0
- package/runtime/llm.mjs +1 -7
- package/runtime/modules.d.mts +3 -0
- package/runtime/modules.mjs +7 -10
- package/runtime/orchestrator-version.d.mts +2 -0
- package/runtime/orchestrator-version.mjs +3 -0
- package/runtime/orchestrator.d.mts +261 -0
- package/runtime/orchestrator.mjs +823 -608
- package/runtime/runtime.d.mts +15 -0
- package/runtime/runtime.mjs +18 -26
- package/runtime/workflow.d.mts +24 -0
- package/runtime/workflow.mjs +42 -121
- package/testing/index.d.mts +107 -155
- package/testing/index.mjs +552 -545
- package/types/index.d.ts +0 -1
- package/types/langs/package.json +3 -0
- package/types/modules/ast-grep.d.ts +22 -1
- package/types/modules/workflow.d.ts +386 -231
- package/vite.d.mts +4 -21
- package/vite.mjs +23 -17
- package/runtime/metrics.mjs +0 -45
- package/types/metrics.d.ts +0 -5
- package/types/modules/metrics.d.ts +0 -107
package/README.md
CHANGED
|
@@ -183,7 +183,6 @@ nodes:
|
|
|
183
183
|
|---------|-------------|
|
|
184
184
|
| `npx codemod workflow run -w <path>` | Run a codemod workflow on your codebase |
|
|
185
185
|
| `npx codemod workflow validate -w <path>` | Validate workflow syntax and structure |
|
|
186
|
-
| `npx codemod workflow test [tests]` | Run the whole workflow on test fixtures and compare the results |
|
|
187
186
|
| `npx codemod workflow resume -i <id>` | Resume a paused workflow |
|
|
188
187
|
| `npx codemod workflow status -i <id>` | Show workflow execution status |
|
|
189
188
|
| `npx codemod workflow list` | List recent workflow runs |
|
|
@@ -211,7 +210,7 @@ nodes:
|
|
|
211
210
|
|
|
212
211
|
A codemod package can depend on `codemod` directly: its scripts and `npx codemod` then run that
|
|
213
212
|
CLI, pinned to your `package.json`, and editors get the matching types for writing codemods and
|
|
214
|
-
workflows, the `codemod:*` modules (`codemod/workflow`, `codemod/ast-grep`, `codemod/
|
|
213
|
+
workflows, the `codemod:*` modules (`codemod/workflow`, `codemod/ast-grep`, `codemod/runtime`, ...)
|
|
215
214
|
and the per-language node types:
|
|
216
215
|
|
|
217
216
|
```bash
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codemod",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.4",
|
|
4
4
|
"description": "Codemod platform for semantic code transformations",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ast",
|
|
@@ -38,10 +38,6 @@
|
|
|
38
38
|
"types": "./types/main.d.ts",
|
|
39
39
|
"default": "./runtime/ast-grep.mjs"
|
|
40
40
|
},
|
|
41
|
-
"./metrics": {
|
|
42
|
-
"types": "./types/metrics.d.ts",
|
|
43
|
-
"default": "./runtime/metrics.mjs"
|
|
44
|
-
},
|
|
45
41
|
"./package.json": "./package.json",
|
|
46
42
|
"./register": {
|
|
47
43
|
"types": "./register.d.mts",
|
|
@@ -81,26 +77,28 @@
|
|
|
81
77
|
"vite.mjs"
|
|
82
78
|
],
|
|
83
79
|
"scripts": {
|
|
84
|
-
"
|
|
85
|
-
"
|
|
86
|
-
"
|
|
87
|
-
"
|
|
80
|
+
"build": "node scripts/build.mjs",
|
|
81
|
+
"prepack": "node scripts/build.mjs",
|
|
82
|
+
"test": "node scripts/build.mjs && node --test tests/*.test.js tests/*.test.mjs",
|
|
83
|
+
"typecheck": "node scripts/build.mjs && tsc -p ../../workflow/src/js && tsc -p types && tsc -p type-tests",
|
|
84
|
+
"pull-llrt-types": "bash scripts/pull-llrt-types.sh"
|
|
88
85
|
},
|
|
89
86
|
"dependencies": {
|
|
90
87
|
"detect-libc": "^2.0.3"
|
|
91
88
|
},
|
|
92
89
|
"devDependencies": {
|
|
93
90
|
"@codemod.com/tsconfig": "workspace:*",
|
|
91
|
+
"@types/node": "^22.16.0",
|
|
94
92
|
"typescript": "catalog:"
|
|
95
93
|
},
|
|
96
94
|
"optionalDependencies": {
|
|
97
|
-
"@codemod.com/cli-darwin-arm64": "2.0.0-alpha.
|
|
98
|
-
"@codemod.com/cli-darwin-x64": "2.0.0-alpha.
|
|
99
|
-
"@codemod.com/cli-linux-arm64-gnu": "2.0.0-alpha.
|
|
100
|
-
"@codemod.com/cli-linux-arm64-musl": "2.0.0-alpha.
|
|
101
|
-
"@codemod.com/cli-linux-x64-gnu": "2.0.0-alpha.
|
|
102
|
-
"@codemod.com/cli-linux-x64-musl": "2.0.0-alpha.
|
|
103
|
-
"@codemod.com/cli-win32-x64-msvc": "2.0.0-alpha.
|
|
95
|
+
"@codemod.com/cli-darwin-arm64": "2.0.0-alpha.4",
|
|
96
|
+
"@codemod.com/cli-darwin-x64": "2.0.0-alpha.4",
|
|
97
|
+
"@codemod.com/cli-linux-arm64-gnu": "2.0.0-alpha.4",
|
|
98
|
+
"@codemod.com/cli-linux-arm64-musl": "2.0.0-alpha.4",
|
|
99
|
+
"@codemod.com/cli-linux-x64-gnu": "2.0.0-alpha.4",
|
|
100
|
+
"@codemod.com/cli-linux-x64-musl": "2.0.0-alpha.4",
|
|
101
|
+
"@codemod.com/cli-win32-x64-msvc": "2.0.0-alpha.4"
|
|
104
102
|
},
|
|
105
103
|
"engines": {
|
|
106
104
|
"node": ">= 16.0.0"
|
package/register.d.mts
CHANGED
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* the older `codemod:workflow`, `codemod:ast-grep`, `codemod:metrics`,
|
|
4
|
-
* `codemod:runtime` and `codemod:llm` spelling: those imports resolve to this
|
|
5
|
-
* package's `codemod/*` exports. Code that imports `codemod/*` needs none of this.
|
|
6
|
-
*
|
|
7
|
-
* @module
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
/** The URL a `codemod:*` specifier resolves to, or `undefined`. */
|
|
11
|
-
export function resolveCodemodModule(specifier: string): string | undefined;
|
|
1
|
+
import { resolveCodemodModule } from "./runtime/modules.mts";
|
|
2
|
+
export { resolveCodemodModule };
|
package/register.mjs
CHANGED
|
@@ -4,57 +4,64 @@
|
|
|
4
4
|
// Code that imports `codemod/*` needs none of this.
|
|
5
5
|
//
|
|
6
6
|
// `codemod/host` and `codemod/testing` register these hooks themselves.
|
|
7
|
-
|
|
7
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
8
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
9
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
10
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return path;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* `import "codemod/register"` lets Node.js and Bun import code written with
|
|
17
|
+
* the older `codemod:workflow`, `codemod:ast-grep`, `codemod:runtime` and
|
|
18
|
+
* `codemod:llm` spelling: those imports resolve to this
|
|
19
|
+
* package's `codemod/*` exports. Code that imports `codemod/*` needs none of this.
|
|
20
|
+
*
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
8
23
|
import module from "node:module";
|
|
9
|
-
|
|
10
24
|
import { CODEMOD_MODULES, resolveCodemodModule } from "./runtime/modules.mjs";
|
|
11
|
-
|
|
12
25
|
export { resolveCodemodModule };
|
|
13
|
-
|
|
14
26
|
const REGISTERED = Symbol.for("codemod.register");
|
|
15
|
-
|
|
16
|
-
const bunModules =
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
await import(resolveCodemodModule(specifier)),
|
|
22
|
-
]),
|
|
23
|
-
)
|
|
27
|
+
const shared = globalThis;
|
|
28
|
+
const bunModules = shared.Bun !== undefined && !shared[REGISTERED]
|
|
29
|
+
? await Promise.all(CODEMOD_MODULES.map(async (specifier) => [
|
|
30
|
+
specifier,
|
|
31
|
+
await import(__rewriteRelativeImportExtension(resolveCodemodModule(specifier) ?? specifier)),
|
|
32
|
+
]))
|
|
24
33
|
: [];
|
|
25
|
-
|
|
26
34
|
function register() {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
module.registerHooks
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
module.register
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
35
|
+
if (shared[REGISTERED])
|
|
36
|
+
return;
|
|
37
|
+
shared[REGISTERED] = true;
|
|
38
|
+
if (shared.Bun !== undefined) {
|
|
39
|
+
// Bun's runtime plugins do not resolve imports of loaded modules through
|
|
40
|
+
// onResolve, but serve virtual modules: back each with the real one.
|
|
41
|
+
shared.Bun.plugin({
|
|
42
|
+
name: "codemod",
|
|
43
|
+
setup(build) {
|
|
44
|
+
for (const [specifier, exports] of bunModules) {
|
|
45
|
+
build.module(specifier, () => ({ exports, loader: "object" }));
|
|
46
|
+
}
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (typeof module.registerHooks === "function") {
|
|
52
|
+
module.registerHooks({
|
|
53
|
+
resolve(specifier, context, nextResolve) {
|
|
54
|
+
const url = resolveCodemodModule(specifier);
|
|
55
|
+
return url ? { url, shortCircuit: true } : nextResolve(specifier, context);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
if (typeof module.register === "function") {
|
|
61
|
+
// The built hooks file, next to the built form of this one.
|
|
62
|
+
module.register("./runtime/hooks.mjs", import.meta.url);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
throw new Error("this Node.js version cannot resolve codemod:* modules; use Node.js 20.6 or later");
|
|
58
66
|
}
|
|
59
|
-
|
|
60
67
|
register();
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import type { Json, Outcome, StepRecord, TestPlan } from "./orchestrator.mts";
|
|
2
|
+
/** `OpenOptions` in crates/node/src/run.rs. */
|
|
3
|
+
export interface OpenOptions {
|
|
4
|
+
workflow: string;
|
|
5
|
+
target: string;
|
|
6
|
+
params?: Record<string, Json>;
|
|
7
|
+
name?: string;
|
|
8
|
+
dryRun?: boolean;
|
|
9
|
+
/** Stop after the step with this label (a test run). */
|
|
10
|
+
until?: string;
|
|
11
|
+
/** Results for steps that should not run, by label (a test run). */
|
|
12
|
+
given?: Record<string, Json>;
|
|
13
|
+
approve?: boolean;
|
|
14
|
+
capabilities?: string[];
|
|
15
|
+
store?: string;
|
|
16
|
+
resume?: string;
|
|
17
|
+
}
|
|
18
|
+
/** An event as the engine reports it (`Event` in crates/workflow/src/events.rs). */
|
|
19
|
+
export type EngineEvent = {
|
|
20
|
+
readonly event: string;
|
|
21
|
+
} & Record<string, unknown>;
|
|
22
|
+
/** What `Run.finish` returns: `RunReport::to_json`, with stats. */
|
|
23
|
+
export interface NativeReport {
|
|
24
|
+
runId: string;
|
|
25
|
+
status: "succeeded" | "failed" | "canceled" | "awaiting-approval" | "stopped";
|
|
26
|
+
output: Json;
|
|
27
|
+
error: string | null;
|
|
28
|
+
awaiting: string | null;
|
|
29
|
+
stoppedAt: string | null;
|
|
30
|
+
steps: {
|
|
31
|
+
position: number;
|
|
32
|
+
label: string;
|
|
33
|
+
status: "succeeded" | "partial" | "failed";
|
|
34
|
+
result: Json;
|
|
35
|
+
error: string | null;
|
|
36
|
+
given: boolean;
|
|
37
|
+
}[];
|
|
38
|
+
stats: {
|
|
39
|
+
filesModified: number;
|
|
40
|
+
filesUnmodified: number;
|
|
41
|
+
filesWithErrors: number;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/** What a `forEachFile` callback did to one file, for `Run.commitFiles`. */
|
|
45
|
+
export interface FileEffects {
|
|
46
|
+
path: string;
|
|
47
|
+
/** The file's text when the step read it. */
|
|
48
|
+
original: string;
|
|
49
|
+
effects: import("./orchestrator.mts").FileEffect[];
|
|
50
|
+
}
|
|
51
|
+
/** One run in the engine: `Run` in crates/node/src/run.rs. */
|
|
52
|
+
export interface NativeRun {
|
|
53
|
+
readonly target: string;
|
|
54
|
+
readonly params: Record<string, Json>;
|
|
55
|
+
readonly dryRun: boolean;
|
|
56
|
+
/** Steps recorded by earlier executions of the run. */
|
|
57
|
+
history(): StepRecord[];
|
|
58
|
+
testPlan(): TestPlan | null;
|
|
59
|
+
/** Persist one step's outcome (a `StepRecord` as JSON). */
|
|
60
|
+
record(entry: string, durationMs: number): void;
|
|
61
|
+
/** Report a step started, replayed or retrying (a `StepEvent` as JSON). */
|
|
62
|
+
event(event: string): void;
|
|
63
|
+
/** A line the body (`null`) or the step at `position` wrote. */
|
|
64
|
+
log(position: number | null, line: string): void;
|
|
65
|
+
progress(position: number, processed: number, total: number): void;
|
|
66
|
+
canceled(): boolean;
|
|
67
|
+
/** Whether a step about to start must not, because the run was canceled. */
|
|
68
|
+
refusesSteps(): boolean;
|
|
69
|
+
cancel(): void;
|
|
70
|
+
/** The test plan's `until` step has started. */
|
|
71
|
+
stop(): void;
|
|
72
|
+
/** Evaluate a workflow.yaml `${{ }}` expression; `scope` is JSON. */
|
|
73
|
+
evaluate(kind: string, expression: string, scope: string): unknown;
|
|
74
|
+
/** Run a host task (a `StartRequest` as JSON). Rejects with `{ name, message }` as JSON in the message. */
|
|
75
|
+
runTask(request: string): Promise<Outcome>;
|
|
76
|
+
/** Collect JSSG data without writing edits; only `commit` applies them. */
|
|
77
|
+
prepareJssg(request: string): Promise<{
|
|
78
|
+
readonly data: {
|
|
79
|
+
file: string;
|
|
80
|
+
data: Json;
|
|
81
|
+
}[];
|
|
82
|
+
commit(data: string): Promise<Outcome>;
|
|
83
|
+
}>;
|
|
84
|
+
/** The files a `forEachFile` selector (as JSON) picks. */
|
|
85
|
+
selectFiles(selector: string): Promise<{
|
|
86
|
+
language: string | null;
|
|
87
|
+
files: [path: string, text: string][];
|
|
88
|
+
}>;
|
|
89
|
+
/** Commit what a step's callbacks did to its files (`FileEffects[]` as JSON). */
|
|
90
|
+
commitFiles(position: number, label: string, files: string): Promise<{
|
|
91
|
+
changes: Json;
|
|
92
|
+
failures: {
|
|
93
|
+
path: string;
|
|
94
|
+
error: string;
|
|
95
|
+
}[];
|
|
96
|
+
}>;
|
|
97
|
+
/** Events since the last call. */
|
|
98
|
+
takeEvents(): EngineEvent[];
|
|
99
|
+
/** Record how the run ended, given how its body ended (JSON, or `"null"`). */
|
|
100
|
+
finish(body: string): NativeReport;
|
|
101
|
+
}
|
|
102
|
+
/** The addon's exports. */
|
|
103
|
+
export interface Addon {
|
|
104
|
+
Run: {
|
|
105
|
+
open(options: OpenOptions): NativeRun;
|
|
106
|
+
};
|
|
107
|
+
/** Identifies the orchestrator source the addon's engine was built from. */
|
|
108
|
+
orchestratorVersion(): string;
|
|
109
|
+
/** The workflow file a path names: the file, or a package directory's workflow. */
|
|
110
|
+
resolveWorkflow(source: string, name: string | null): string;
|
|
111
|
+
/** Parse `source` as `language`; returns an `SgRoot`. */
|
|
112
|
+
parse(language: string, source: string, filename?: string, relativeFilename?: string): unknown;
|
|
113
|
+
languageFromPath(path: string): string | null;
|
|
114
|
+
kind(language: string, kindName: string): number;
|
|
115
|
+
}
|
|
116
|
+
/** The addon's exports. Throws with what to do when it cannot be found. */
|
|
117
|
+
export declare function addon(): Addon;
|
|
118
|
+
/** Where the addon is, or throws. */
|
|
119
|
+
export declare function addonPath(): string;
|
package/runtime/addon.mjs
CHANGED
|
@@ -3,57 +3,53 @@
|
|
|
3
3
|
//
|
|
4
4
|
// Looked up in order: `CODEMOD_NODE_ADDON`, the platform package next to the
|
|
5
5
|
// CLI binary, then a cargo build in a checkout of the codemod repository.
|
|
6
|
-
|
|
6
|
+
//
|
|
7
|
+
// The interfaces here describe what the addon exports (crates/node/src).
|
|
7
8
|
import { existsSync, statSync } from "node:fs";
|
|
8
9
|
import { createRequire } from "node:module";
|
|
9
10
|
import { dirname, join } from "node:path";
|
|
10
11
|
import { fileURLToPath } from "node:url";
|
|
11
|
-
|
|
12
12
|
const require = createRequire(import.meta.url);
|
|
13
13
|
const here = dirname(fileURLToPath(import.meta.url));
|
|
14
|
-
|
|
15
14
|
let loaded;
|
|
16
|
-
|
|
17
15
|
/** The addon's exports. Throws with what to do when it cannot be found. */
|
|
18
16
|
export function addon() {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
if (loaded)
|
|
18
|
+
return loaded;
|
|
19
|
+
const path = addonPath();
|
|
20
|
+
const module = { exports: {} };
|
|
21
|
+
process.dlopen(module, path);
|
|
22
|
+
loaded = module.exports;
|
|
23
|
+
return loaded;
|
|
25
24
|
}
|
|
26
|
-
|
|
27
25
|
/** Where the addon is, or throws. */
|
|
28
26
|
export function addonPath() {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
27
|
+
if (process.env.CODEMOD_NODE_ADDON)
|
|
28
|
+
return process.env.CODEMOD_NODE_ADDON;
|
|
29
|
+
const { detectPackageName } = require("../codemod");
|
|
30
|
+
const pkgName = detectPackageName();
|
|
31
|
+
if (pkgName) {
|
|
32
|
+
try {
|
|
33
|
+
const candidate = join(dirname(require.resolve(`${pkgName}/package.json`, { paths: [here] })), "codemod.node");
|
|
34
|
+
if (existsSync(candidate))
|
|
35
|
+
return candidate;
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// not installed
|
|
39
|
+
}
|
|
41
40
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
throw new Error(
|
|
57
|
-
`cannot find the codemod native addon${pkgName ? ` (expected codemod.node in the ${pkgName} package)` : " for this platform"}; reinstall codemod with optional dependencies, or set CODEMOD_NODE_ADDON`,
|
|
58
|
-
);
|
|
41
|
+
const library = process.platform === "win32"
|
|
42
|
+
? "codemod_node.dll"
|
|
43
|
+
: process.platform === "darwin"
|
|
44
|
+
? "libcodemod_node.dylib"
|
|
45
|
+
: "libcodemod_node.so";
|
|
46
|
+
// In a checkout: whichever cargo build is newest, so a stale one is not
|
|
47
|
+
// picked over the build just made.
|
|
48
|
+
const [newest] = ["release", "debug"]
|
|
49
|
+
.map((profile) => join(here, "..", "..", "..", "..", "target", profile, library))
|
|
50
|
+
.filter((candidate) => existsSync(candidate))
|
|
51
|
+
.sort((a, b) => statSync(b).mtimeMs - statSync(a).mtimeMs);
|
|
52
|
+
if (newest)
|
|
53
|
+
return newest;
|
|
54
|
+
throw new Error(`cannot find the codemod native addon${pkgName ? ` (expected codemod.node in the ${pkgName} package)` : " for this platform"}; reinstall codemod with optional dependencies, or set CODEMOD_NODE_ADDON`);
|
|
59
55
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `parse(lang, text)`, `parse(text, { language })`, or `parse(file, { language })`
|
|
3
|
+
* with a `forEachFile` file. Without a language, it comes from the file, then
|
|
4
|
+
* the `forEachFile` step's language, then the file's extension.
|
|
5
|
+
*/
|
|
6
|
+
export declare function parse(source: unknown, second?: unknown): unknown;
|
|
7
|
+
/** The same as `parse`; parsers are ready at once here. */
|
|
8
|
+
export declare function parseAsync(source: unknown, second?: unknown): unknown;
|
|
9
|
+
export declare function parseFile(language: string, path: string): unknown;
|
|
10
|
+
/** The numeric id of a named node kind, for `{ kind: id }` matchers. */
|
|
11
|
+
export declare function kind(language: string, kindName: string): number;
|
|
12
|
+
export declare function jssgTransform(): never;
|
|
13
|
+
declare const _default: {
|
|
14
|
+
parse: typeof parse;
|
|
15
|
+
parseAsync: typeof parseAsync;
|
|
16
|
+
parseFile: typeof parseFile;
|
|
17
|
+
kind: typeof kind;
|
|
18
|
+
jssgTransform: typeof jssgTransform;
|
|
19
|
+
};
|
|
20
|
+
export default _default;
|
package/runtime/ast-grep.mjs
CHANGED
|
@@ -3,91 +3,80 @@
|
|
|
3
3
|
//
|
|
4
4
|
// Roots parsed here have no semantic provider, as with `parse()` in an engine
|
|
5
5
|
// `forEachFile` callback: `definition()` returns null.
|
|
6
|
-
|
|
7
6
|
import { readFileSync, realpathSync } from "node:fs";
|
|
8
7
|
import { isAbsolute, join, relative } from "node:path";
|
|
9
|
-
|
|
10
8
|
import { addon } from "./addon.mjs";
|
|
11
9
|
import { context } from "./context.mjs";
|
|
12
|
-
|
|
10
|
+
function isRecord(value) {
|
|
11
|
+
return value !== null && typeof value === "object";
|
|
12
|
+
}
|
|
13
|
+
/** The `language` of `parse`'s options, when it is given. */
|
|
14
|
+
function languageOption(options) {
|
|
15
|
+
return isRecord(options) && typeof options.language === "string" ? options.language : undefined;
|
|
16
|
+
}
|
|
13
17
|
/**
|
|
14
18
|
* `parse(lang, text)`, `parse(text, { language })`, or `parse(file, { language })`
|
|
15
19
|
* with a `forEachFile` file. Without a language, it comes from the file, then
|
|
16
20
|
* the `forEachFile` step's language, then the file's extension.
|
|
17
21
|
*/
|
|
18
22
|
export function parse(source, second) {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
23
|
+
const native = addon();
|
|
24
|
+
const store = context.getStore();
|
|
25
|
+
const fallback = store?.step?.language;
|
|
26
|
+
if (typeof source === "string") {
|
|
27
|
+
if (typeof second === "string")
|
|
28
|
+
return native.parse(source, second);
|
|
29
|
+
const language = languageOption(second) ?? fallback;
|
|
30
|
+
if (!language) {
|
|
31
|
+
throw new Error('parse(text) needs a language: parse(text, { language: "tsx" })');
|
|
32
|
+
}
|
|
33
|
+
return native.parse(language, source);
|
|
34
|
+
}
|
|
35
|
+
if (!isRecord(source)) {
|
|
36
|
+
throw new TypeError("parse() expects source text or a file object, for example parse(file) or parse(text, { language })");
|
|
37
|
+
}
|
|
38
|
+
if (typeof source.text !== "string") {
|
|
39
|
+
throw new TypeError("parse(file) expects an object with a string `text`, such as the file a forEachFile callback receives");
|
|
40
|
+
}
|
|
41
|
+
const path = typeof source.path === "string" ? source.path : undefined;
|
|
42
|
+
const language = languageOption(second) ??
|
|
43
|
+
languageOption(source) ??
|
|
44
|
+
fallback ??
|
|
45
|
+
(path ? (native.languageFromPath(path) ?? undefined) : undefined);
|
|
28
46
|
if (!language) {
|
|
29
|
-
|
|
47
|
+
throw new Error(`parse(file): no language for '${path ?? "<file>"}'; pass one with parse(file, { language })`);
|
|
30
48
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
);
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
"parse(file) expects an object with a string `text`, such as the file a forEachFile callback receives",
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
const path = typeof source.path === "string" ? source.path : undefined;
|
|
44
|
-
const language =
|
|
45
|
-
languageOption(second) ??
|
|
46
|
-
source.language ??
|
|
47
|
-
fallback ??
|
|
48
|
-
(path ? native.languageFromPath(path) : undefined);
|
|
49
|
-
if (!language) {
|
|
50
|
-
throw new Error(
|
|
51
|
-
`parse(file): no language for '${path ?? "<file>"}'; pass one with parse(file, { language })`,
|
|
52
|
-
);
|
|
53
|
-
}
|
|
54
|
-
if (path === undefined) return native.parse(language, source.text);
|
|
55
|
-
// The file's real path, and its path inside the target: what the CLI's
|
|
56
|
-
// runtime gives `filename()` and `relativeFilename()`.
|
|
57
|
-
const target = store?.run?.target;
|
|
58
|
-
const filename = real(isAbsolute(path) || !target ? path : join(target, path));
|
|
59
|
-
const inTarget = target ? relative(real(target), filename) : path;
|
|
60
|
-
const outside = inTarget.startsWith("..") || isAbsolute(inTarget);
|
|
61
|
-
return native.parse(language, source.text, filename, outside ? undefined : inTarget);
|
|
49
|
+
if (path === undefined)
|
|
50
|
+
return native.parse(language, source.text);
|
|
51
|
+
// The file's real path, and its path inside the target: what the CLI's
|
|
52
|
+
// runtime gives `filename()` and `relativeFilename()`.
|
|
53
|
+
const target = store?.run.target;
|
|
54
|
+
const filename = real(isAbsolute(path) || !target ? path : join(target, path));
|
|
55
|
+
const inTarget = target ? relative(real(target), filename) : path;
|
|
56
|
+
const outside = inTarget.startsWith("..") || isAbsolute(inTarget);
|
|
57
|
+
return native.parse(language, source.text, filename, outside ? undefined : inTarget);
|
|
62
58
|
}
|
|
63
|
-
|
|
64
59
|
/** A path with links resolved; as given when it does not exist. */
|
|
65
60
|
function real(path) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
61
|
+
try {
|
|
62
|
+
return realpathSync(path);
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
return path;
|
|
66
|
+
}
|
|
71
67
|
}
|
|
72
|
-
|
|
73
68
|
/** The same as `parse`; parsers are ready at once here. */
|
|
74
69
|
export function parseAsync(source, second) {
|
|
75
|
-
|
|
70
|
+
return parse(source, second);
|
|
76
71
|
}
|
|
77
|
-
|
|
78
72
|
export function parseFile(language, path) {
|
|
79
|
-
|
|
73
|
+
return addon().parse(language, readFileSync(path, "utf8"), path);
|
|
80
74
|
}
|
|
81
|
-
|
|
82
75
|
/** The numeric id of a named node kind, for `{ kind: id }` matchers. */
|
|
83
76
|
export function kind(language, kindName) {
|
|
84
|
-
|
|
77
|
+
return addon().kind(language, kindName);
|
|
85
78
|
}
|
|
86
|
-
|
|
87
79
|
export function jssgTransform() {
|
|
88
|
-
|
|
89
|
-
"jssgTransform() is not supported in forEachFile callbacks yet; change each file from its own callback",
|
|
90
|
-
);
|
|
80
|
+
throw new Error("jssgTransform() is not supported in forEachFile callbacks yet; change each file from its own callback");
|
|
91
81
|
}
|
|
92
|
-
|
|
93
82
|
export default { parse, parseAsync, parseFile, kind, jssgTransform };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
import type { NativeRun } from "./addon.mts";
|
|
3
|
+
import type { Runtime } from "./orchestrator.mts";
|
|
4
|
+
/** What a step or `forEachFile` callback sees and leaves behind. */
|
|
5
|
+
export interface StepScope {
|
|
6
|
+
position: number;
|
|
7
|
+
label: string;
|
|
8
|
+
/** A failure reported with `failFile` or `failStep`. */
|
|
9
|
+
failure: string | undefined;
|
|
10
|
+
/** `parse()`'s default language. */
|
|
11
|
+
language: string | undefined;
|
|
12
|
+
}
|
|
13
|
+
/** The run the code running now belongs to, and its step when it is in one. */
|
|
14
|
+
export interface RunScope {
|
|
15
|
+
run: NativeRun;
|
|
16
|
+
runtime: Runtime;
|
|
17
|
+
/** Whether `console` output of the run's code goes to its logs. */
|
|
18
|
+
captureConsole: boolean;
|
|
19
|
+
step?: StepScope;
|
|
20
|
+
}
|
|
21
|
+
export declare const context: AsyncLocalStorage<RunScope>;
|
|
22
|
+
/** The run the calling code belongs to; throws outside a run. */
|
|
23
|
+
export declare function current(what: string): RunScope;
|
|
24
|
+
/** A fresh step scope. */
|
|
25
|
+
export declare function stepScope(position: number, label: string, language?: string): StepScope;
|