@codegraft/codemod 0.1.0-beta.0
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/LICENSE +21 -0
- package/dist/codemod.d.ts +37 -0
- package/dist/codemod.d.ts.map +1 -0
- package/dist/codemod.js +28 -0
- package/dist/codemod.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026-present Joël Charles
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { Collection, GrammarId, Transformer, ZoneSplitter } from '@codegraft/core';
|
|
2
|
+
/** A codemod body: receives the file's {@link Collection} and the run context, records edits.
|
|
3
|
+
* `G` types the node-type/field vocabulary; annotate `root` (`Collection<'tsx'>`) to narrow it. */
|
|
4
|
+
export type CodemodFn<Ctx extends Record<string, unknown>, G extends GrammarId = GrammarId> = (root: Collection<G>, context: Ctx) => void;
|
|
5
|
+
/** Options for a codemod. */
|
|
6
|
+
export interface CodemodConfig {
|
|
7
|
+
/**
|
|
8
|
+
* The build-time global marker the codemod keys off (e.g. `$$`). Enables the source-scan
|
|
9
|
+
* optimisation — files that never mention it are returned untouched without being parsed.
|
|
10
|
+
*/
|
|
11
|
+
namespace?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Authoring handle for a codemod. `forTarget` builds a transformer for one target; `codegraft run`
|
|
15
|
+
* and `@codegraft/unplugin` call it to apply the codemod live (no build step).
|
|
16
|
+
*
|
|
17
|
+
* `G` is the grammar the body was authored against (inferred from a `root: Collection<'tsx'>`
|
|
18
|
+
* annotation, else every built-in grammar); it ties `forTarget` to a matching bare-grammar target.
|
|
19
|
+
*/
|
|
20
|
+
export declare class Codemod<Ctx extends Record<string, unknown> = Record<string, unknown>, G extends GrammarId = GrammarId> {
|
|
21
|
+
readonly fn: CodemodFn<Ctx, G>;
|
|
22
|
+
readonly namespace: string | undefined;
|
|
23
|
+
constructor(fn: CodemodFn<Ctx, G>, namespace?: string);
|
|
24
|
+
/** Interpreted mode: build a ready transformer for `target`. A grammar-annotated codemod only
|
|
25
|
+
* accepts its own bare grammar; a {@link ZoneSplitter} is always allowed (its grammars aren't
|
|
26
|
+
* statically known). */
|
|
27
|
+
forTarget(target: G | ZoneSplitter): Promise<Transformer<Ctx>>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Entry point for authoring a codemod. An optional {@link CodemodConfig} may precede the body.
|
|
31
|
+
* `Ctx` types the run context threaded into the body (and the resulting transformer's
|
|
32
|
+
* `transform(src, ctx)`); defaults to an open record. `G` is inferred from the body's `root`
|
|
33
|
+
* annotation (defaulting to every built-in grammar).
|
|
34
|
+
*/
|
|
35
|
+
export declare function defineCodemod<Ctx extends Record<string, unknown> = Record<string, unknown>, G extends GrammarId = GrammarId>(fn: CodemodFn<Ctx, G>): Codemod<Ctx, G>;
|
|
36
|
+
export declare function defineCodemod<Ctx extends Record<string, unknown> = Record<string, unknown>, G extends GrammarId = GrammarId>(config: CodemodConfig, fn: CodemodFn<Ctx, G>): Codemod<Ctx, G>;
|
|
37
|
+
//# sourceMappingURL=codemod.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codemod.d.ts","sourceRoot":"","sources":["../src/codemod.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,WAAW,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAGvF;oGACoG;AACpG,MAAM,MAAM,SAAS,CAAC,GAAG,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,SAAS,GAAG,SAAS,IAAI,CAC5F,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,EACnB,OAAO,EAAE,GAAG,KACT,IAAI,CAAA;AAET,6BAA6B;AAC7B,MAAM,WAAW,aAAa;IAC5B;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;GAMG;AACH,qBAAa,OAAO,CAClB,GAAG,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7D,CAAC,SAAS,SAAS,GAAG,SAAS;IAE/B,QAAQ,CAAC,EAAE,EAAE,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,GAAG,SAAS,CAAA;gBAE1B,EAAE,EAAE,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,MAAM;IAKrD;;6BAEyB;IACzB,SAAS,CAAC,MAAM,EAAE,CAAC,GAAG,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,GAAG,CAAC,CAAC;CAG/D;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,SAAS,GAAG,SAAS,EAC1H,EAAE,EAAE,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,GACpB,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;AAClB,wBAAgB,aAAa,CAAC,GAAG,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,SAAS,SAAS,GAAG,SAAS,EAC1H,MAAM,EAAE,aAAa,EACrB,EAAE,EAAE,SAAS,CAAC,GAAG,EAAE,CAAC,CAAC,GACpB,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA"}
|
package/dist/codemod.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { createCodemodTransformer } from '@codegraft/core';
|
|
2
|
+
/**
|
|
3
|
+
* Authoring handle for a codemod. `forTarget` builds a transformer for one target; `codegraft run`
|
|
4
|
+
* and `@codegraft/unplugin` call it to apply the codemod live (no build step).
|
|
5
|
+
*
|
|
6
|
+
* `G` is the grammar the body was authored against (inferred from a `root: Collection<'tsx'>`
|
|
7
|
+
* annotation, else every built-in grammar); it ties `forTarget` to a matching bare-grammar target.
|
|
8
|
+
*/
|
|
9
|
+
export class Codemod {
|
|
10
|
+
fn;
|
|
11
|
+
namespace;
|
|
12
|
+
constructor(fn, namespace) {
|
|
13
|
+
this.fn = fn;
|
|
14
|
+
this.namespace = namespace;
|
|
15
|
+
}
|
|
16
|
+
/** Interpreted mode: build a ready transformer for `target`. A grammar-annotated codemod only
|
|
17
|
+
* accepts its own bare grammar; a {@link ZoneSplitter} is always allowed (its grammars aren't
|
|
18
|
+
* statically known). */
|
|
19
|
+
forTarget(target) {
|
|
20
|
+
return createCodemodTransformer(target, this.fn, { namespace: this.namespace }).init();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export function defineCodemod(configOrFn, maybeFn) {
|
|
24
|
+
const fn = maybeFn ?? configOrFn;
|
|
25
|
+
const namespace = maybeFn ? configOrFn.namespace : undefined;
|
|
26
|
+
return new Codemod(fn, namespace);
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=codemod.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codemod.js","sourceRoot":"","sources":["../src/codemod.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,MAAM,iBAAiB,CAAA;AAkB1D;;;;;;GAMG;AACH,MAAM,OAAO,OAAO;IAIT,EAAE,CAAmB;IACrB,SAAS,CAAoB;IAEtC,YAAY,EAAqB,EAAE,SAAkB;QACnD,IAAI,CAAC,EAAE,GAAG,EAAE,CAAA;QACZ,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;IAC5B,CAAC;IAED;;6BAEyB;IACzB,SAAS,CAAC,MAAwB;QAChC,OAAO,wBAAwB,CAAS,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,EAAE,SAAS,EAAE,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,IAAI,EAAE,CAAA;IAChG,CAAC;CACF;AAeD,MAAM,UAAU,aAAa,CAC3B,UAA6C,EAC7C,OAA2B;IAE3B,MAAM,EAAE,GAAG,OAAO,IAAK,UAAgC,CAAA;IACvD,MAAM,SAAS,GAAG,OAAO,CAAC,CAAC,CAAE,UAA4B,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,CAAA;IAC/E,OAAO,IAAI,OAAO,CAAS,EAAE,EAAE,SAAS,CAAC,CAAA;AAC3C,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AACrD,YAAY,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA"}
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@codegraft/codemod",
|
|
3
|
+
"version": "0.1.0-beta.0",
|
|
4
|
+
"description": "Authoring API for Codegraft codemods: defineCodemod and a jscodeshift-style collection over the CST.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/magne4000/codegraft.git",
|
|
11
|
+
"directory": "packages/codemod"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/magne4000/codegraft#readme",
|
|
14
|
+
"bugs": "https://github.com/magne4000/codegraft/issues",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"import": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=22.13"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@codegraft/core": "0.1.0-beta.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@codegraft/vue": "0.1.0-beta.0"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -b"
|
|
35
|
+
}
|
|
36
|
+
}
|