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/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
- name: "codemod";
19
- enforce: "pre";
20
- resolveId(id: string): string | null;
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
- // // vitest.config.ts
7
- // import { defineConfig } from "vitest/config";
8
- // import codemod from "codemod/vite";
9
- // export default defineConfig({ plugins: [codemod()] });
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
- return {
17
- name: "codemod",
18
- enforce: "pre",
19
- resolveId(id) {
20
- const url = resolveCodemodModule(id);
21
- return url ? fileURLToPath(url) : null;
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;
@@ -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 };
@@ -1,5 +0,0 @@
1
- // oxlint-disable triple-slash-reference
2
- /// <reference path="modules/metrics.d.ts" />
3
-
4
- // `codemod/metrics`: the same module as `codemod:metrics`, its older spelling.
5
- export * from "codemod:metrics";
@@ -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
- }