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/vite.d.mts
CHANGED
|
@@ -1,24 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* A Vite (and Vitest) plugin that resolves the older `codemod:*` spelling of
|
|
3
|
-
* the codemod modules (`codemod:workflow`) to the `codemod/*` exports. Code
|
|
4
|
-
* that imports `codemod/*` needs no plugin.
|
|
5
|
-
*
|
|
6
|
-
* ```ts
|
|
7
|
-
* // vitest.config.ts
|
|
8
|
-
* import { defineConfig } from "vitest/config";
|
|
9
|
-
* import codemod from "codemod/vite";
|
|
10
|
-
*
|
|
11
|
-
* export default defineConfig({ plugins: [codemod()] });
|
|
12
|
-
* ```
|
|
13
|
-
*
|
|
14
|
-
* @module
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
1
|
export interface CodemodVitePlugin {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
2
|
+
name: "codemod";
|
|
3
|
+
enforce: "pre";
|
|
4
|
+
resolveId(id: string): string | null;
|
|
21
5
|
}
|
|
22
|
-
|
|
23
|
-
export function codemod(): CodemodVitePlugin;
|
|
6
|
+
export declare function codemod(): CodemodVitePlugin;
|
|
24
7
|
export default codemod;
|
package/vite.mjs
CHANGED
|
@@ -2,25 +2,31 @@
|
|
|
2
2
|
// `codemod:workflow`, `codemod:ast-grep`, ... spelling: it resolves those to
|
|
3
3
|
// this package's `codemod/*` exports. Code that imports `codemod/*` needs no
|
|
4
4
|
// plugin.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
/**
|
|
6
|
+
* A Vite (and Vitest) plugin that resolves the older `codemod:*` spelling of
|
|
7
|
+
* the codemod modules (`codemod:workflow`) to the `codemod/*` exports. Code
|
|
8
|
+
* that imports `codemod/*` needs no plugin.
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* // vitest.config.ts
|
|
12
|
+
* import { defineConfig } from "vitest/config";
|
|
13
|
+
* import codemod from "codemod/vite";
|
|
14
|
+
*
|
|
15
|
+
* export default defineConfig({ plugins: [codemod()] });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
11
20
|
import { fileURLToPath } from "node:url";
|
|
12
|
-
|
|
13
21
|
import { resolveCodemodModule } from "./runtime/modules.mjs";
|
|
14
|
-
|
|
15
22
|
export function codemod() {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
return {
|
|
24
|
+
name: "codemod",
|
|
25
|
+
enforce: "pre",
|
|
26
|
+
resolveId(id) {
|
|
27
|
+
const url = resolveCodemodModule(id);
|
|
28
|
+
return url ? fileURLToPath(url) : null;
|
|
29
|
+
},
|
|
30
|
+
};
|
|
24
31
|
}
|
|
25
|
-
|
|
26
32
|
export default codemod;
|
package/runtime/metrics.mjs
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
// `codemod/metrics` in Node.js and Bun. Metrics belong to the step that
|
|
2
|
-
// records them: they go into a `forEachFile` step's result and the run's
|
|
3
|
-
// report. The workflow body cannot record them, as in the engine.
|
|
4
|
-
|
|
5
|
-
import { context } from "./context.mjs";
|
|
6
|
-
|
|
7
|
-
class MetricAtom {
|
|
8
|
-
constructor(name) {
|
|
9
|
-
this.name = name;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
increment(cardinality = {}, amount = 1) {
|
|
13
|
-
const scope = context.getStore()?.step;
|
|
14
|
-
if (!scope) {
|
|
15
|
-
throw new Error(
|
|
16
|
-
`metric '${this.name}': metrics can only be recorded inside a step or forEachFile callback`,
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
const clean = {};
|
|
20
|
-
for (const key of Object.keys(cardinality ?? {}).sort()) {
|
|
21
|
-
const value = cardinality[key];
|
|
22
|
-
if (value !== undefined && value !== null) clean[key] = String(value);
|
|
23
|
-
}
|
|
24
|
-
// A whole amount of at least 0; anything that is not a number counts 1.
|
|
25
|
-
amount =
|
|
26
|
-
typeof amount === "number" && Number.isFinite(amount) ? Math.max(0, Math.trunc(amount)) : 1;
|
|
27
|
-
const entryKey = JSON.stringify(clean);
|
|
28
|
-
let entries = scope.metrics.get(this.name);
|
|
29
|
-
if (!entries) scope.metrics.set(this.name, (entries = new Map()));
|
|
30
|
-
const entry = entries.get(entryKey);
|
|
31
|
-
if (entry) entry.count += amount;
|
|
32
|
-
else entries.set(entryKey, { cardinality: clean, count: amount });
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
getEntries() {
|
|
36
|
-
const entries = context.getStore()?.step?.metrics.get(this.name);
|
|
37
|
-
return entries ? [...entries.values()].map((entry) => ({ ...entry })) : [];
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function useMetricAtom(name) {
|
|
42
|
-
return new MetricAtom(name);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export { MetricAtom };
|
package/types/metrics.d.ts
DELETED
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
declare module "codemod:metrics" {
|
|
2
|
-
/**
|
|
3
|
-
* Cardinality dimensions for a metric entry.
|
|
4
|
-
* Keys and values must be strings. Undefined/null values are omitted.
|
|
5
|
-
*/
|
|
6
|
-
export type Cardinality = Record<string, string | undefined | null>;
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* A metric entry with its cardinality dimensions and count.
|
|
10
|
-
*/
|
|
11
|
-
export interface MetricEntry {
|
|
12
|
-
cardinality: Record<string, string>;
|
|
13
|
-
count: number;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* A metric atom for tracking counts with cardinality dimensions.
|
|
18
|
-
*
|
|
19
|
-
* MetricAtom provides a way to increment and retrieve counts for different
|
|
20
|
-
* cardinality combinations within a named metric. This is useful for tracking
|
|
21
|
-
* things like prop usage counts with multiple dimensions (e.g., propName + propValue).
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* import { useMetricAtom } from "codemod/metrics";
|
|
26
|
-
*
|
|
27
|
-
* const propUsage = useMetricAtom("prop-usage");
|
|
28
|
-
*
|
|
29
|
-
* export function transform(root) {
|
|
30
|
-
* root.findAll({ rule: { pattern: "$PROP={$_}" } }).forEach((match) => {
|
|
31
|
-
* propUsage.increment({
|
|
32
|
-
* propName: match.getMatch("PROP")?.text(),
|
|
33
|
-
* });
|
|
34
|
-
* });
|
|
35
|
-
* return null;
|
|
36
|
-
* }
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export class MetricAtom {
|
|
40
|
-
/**
|
|
41
|
-
* The name of this metric.
|
|
42
|
-
*/
|
|
43
|
-
readonly name: string;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Increment the count for a given cardinality combination.
|
|
47
|
-
*
|
|
48
|
-
* @param cardinality - Object with dimension key-value pairs. Undefined/null values are omitted.
|
|
49
|
-
* @param amount - The amount to increment by (default: 1)
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```ts
|
|
53
|
-
* // Simple increment with no cardinality
|
|
54
|
-
* metric.increment();
|
|
55
|
-
*
|
|
56
|
-
* // Single dimension
|
|
57
|
-
* propUsage.increment({ propName: "title" });
|
|
58
|
-
*
|
|
59
|
-
* // Multiple dimensions
|
|
60
|
-
* propUsage.increment({ propName: "className", propValue: "container" });
|
|
61
|
-
*
|
|
62
|
-
* // With amount
|
|
63
|
-
* propUsage.increment({ propName: "title" }, 5);
|
|
64
|
-
*
|
|
65
|
-
* // Undefined values are omitted
|
|
66
|
-
* propUsage.increment({ propName: "title", propValue: undefined });
|
|
67
|
-
* // Results in cardinality: { propName: "title" }
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
increment(cardinality?: Cardinality, amount?: number): void;
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Get the current entries for this metric.
|
|
74
|
-
*
|
|
75
|
-
* @returns An array of entries with their cardinality and count
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```ts
|
|
79
|
-
* const entries = propUsage.getEntries();
|
|
80
|
-
* // [
|
|
81
|
-
* // { cardinality: { propName: "title" }, count: 10 },
|
|
82
|
-
* // { cardinality: { propName: "className", propValue: "container" }, count: 5 }
|
|
83
|
-
* // ]
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
getEntries(): MetricEntry[];
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Create or retrieve a metric atom with the given name.
|
|
91
|
-
*
|
|
92
|
-
* Multiple calls with the same name will return atoms that share the same
|
|
93
|
-
* underlying data, allowing metrics to be collected across multiple files.
|
|
94
|
-
*
|
|
95
|
-
* @param name - The name of the metric (e.g., "prop-usage", "component-count")
|
|
96
|
-
* @returns A MetricAtom instance for tracking counts with cardinality
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* ```ts
|
|
100
|
-
* import { useMetricAtom } from "codemod/metrics";
|
|
101
|
-
*
|
|
102
|
-
* const propUsage = useMetricAtom("prop-usage");
|
|
103
|
-
* const componentCount = useMetricAtom("component-count");
|
|
104
|
-
* ```
|
|
105
|
-
*/
|
|
106
|
-
export function useMetricAtom(name: string): MetricAtom;
|
|
107
|
-
}
|