@pbuilder/sdk 0.11.0 → 0.12.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/README.md +4 -2
- package/dist/core/define-dialect.d.ts +44 -0
- package/dist/core/define-dialect.js +67 -0
- package/dist/dialects/react/ast.d.ts +2 -0
- package/dist/dialects/react/ast.js +11 -3
- package/dist/dialects/react/index.d.ts +10 -9
- package/dist/dialects/react/index.js +28 -28
- package/dist/runner-manifest.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -606,8 +606,10 @@ untrusted input is the author's responsibility to sanitize.
|
|
|
606
606
|
|
|
607
607
|
### `@pbuilder/sdk/react`
|
|
608
608
|
|
|
609
|
-
Mutates `.tsx` files — `find()` requires
|
|
610
|
-
|
|
609
|
+
Mutates `.tsx` and `.jsx` files — `find()` requires one of those extensions explicitly
|
|
610
|
+
(extensionless paths are rejected, never normalized). On `.jsx`, the ops that write TypeScript
|
|
611
|
+
syntax (`addTypeImport`, `addComponentProp`, `addClassProperty`'s `scope`/`readonly`) reject,
|
|
612
|
+
leaving the file unchanged. The handle carries every
|
|
611
613
|
`@pbuilder/sdk/typescript` op plus the JSX ops below, with `.modify(fn)` as the escape hatch
|
|
612
614
|
for everything else:
|
|
613
615
|
|
|
@@ -96,3 +96,47 @@ export declare const RESERVED_HANDLE_NAMES: readonly ["then", "read", "raw", "mo
|
|
|
96
96
|
* const tsDialect = withOps(baseTsDialect, addImportPack);
|
|
97
97
|
*/
|
|
98
98
|
export declare function withOps<Ast, B extends OpPack<Ast>, P extends OpPack<Ast>[]>(base: Dialect<Ast, B>, ...packs: P): Dialect<Ast, B & UnionToIntersection<P[number]>>;
|
|
99
|
+
/**
|
|
100
|
+
* Derives a dialect whose named ops reject at call time, leaving the file unchanged, with a
|
|
101
|
+
* message naming the op, the dialect's extensions and `reason`. The ops stay on the handle and
|
|
102
|
+
* in its type, so one handle shape can serve every extension of a routed dialect
|
|
103
|
+
* (`byExtension`). Throws at composition on an op name the dialect does not have.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* const jsx = withoutOps(defineDialect({ extensions: [".jsx"], ast: jsxAst, ops: tsx.ops }), ["addTypeImport"], ".jsx files have no type syntax");
|
|
107
|
+
*/
|
|
108
|
+
export declare function withoutOps<Ast, Ops extends OpPack<Ast>>(dialect: Dialect<Ast, Ops>, names: readonly (keyof Ops & string)[], reason: string): Dialect<Ast, Ops>;
|
|
109
|
+
/**
|
|
110
|
+
* Derives a dialect whose named ops run `overrides` instead, with the same signatures. Throws at
|
|
111
|
+
* composition on an op name the dialect does not have; adding new ops is `withOps`'s job.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* const jsx = overrideOps(tsx, { addClassProperty: jsxClassProperty });
|
|
115
|
+
*/
|
|
116
|
+
export declare function overrideOps<Ast, Ops extends OpPack<Ast>>(dialect: Dialect<Ast, Ops>, overrides: Partial<Ops>): Dialect<Ast, Ops>;
|
|
117
|
+
type DialectsByExtension = {
|
|
118
|
+
readonly [extension: string]: Dialect;
|
|
119
|
+
};
|
|
120
|
+
type HandleOf<D> = D extends Dialect<infer A, infer O> ? Handle<"found", A, O> : never;
|
|
121
|
+
type MatchingKey<M, P extends string> = {
|
|
122
|
+
[K in keyof M & string]: P extends `${string}${K}` ? K : never;
|
|
123
|
+
}[keyof M & string];
|
|
124
|
+
type LongestKey<Ks extends string> = {
|
|
125
|
+
[K in Ks]: [Extract<Exclude<Ks, K>, `${string}${K}`>] extends [never] ? K : never;
|
|
126
|
+
}[Ks];
|
|
127
|
+
type RoutedHandle<M extends DialectsByExtension, P extends string> = string extends P ? HandleOf<M[keyof M]> : HandleOf<M[LongestKey<MatchingKey<M, P>>]>;
|
|
128
|
+
export interface ExtensionRouter<M extends DialectsByExtension> {
|
|
129
|
+
readonly extensions: readonly (keyof M & string)[];
|
|
130
|
+
find<P extends string>(path: P): RoutedHandle<M, P>;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Routes `find` by file extension to one dialect per extension, each with its own AST and ops —
|
|
134
|
+
* a `.ts` dialect and an `.html` dialect may parse to different AST types. The longest matching
|
|
135
|
+
* extension wins (`.component.html` over `.html`); a path matching none rejects synchronously,
|
|
136
|
+
* naming every routed extension. Throws at composition on a key its dialect does not declare.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* export const react = byExtension({ ".tsx": tsxDialect, ".jsx": jsxDialect });
|
|
140
|
+
* react.find("src/App.jsx").setJsxProp("div", "hidden");
|
|
141
|
+
*/
|
|
142
|
+
export declare function byExtension<M extends DialectsByExtension>(dialects: M): ExtensionRouter<M>;
|
|
@@ -127,3 +127,70 @@ export function withOps(base, ...packs) {
|
|
|
127
127
|
const merged = Object.assign({}, base.ops, ...packs);
|
|
128
128
|
return defineDialect({ extensions: [...base.extensions], ast: base.ast, ops: merged });
|
|
129
129
|
}
|
|
130
|
+
function assertKnownOps(ops, names, composer) {
|
|
131
|
+
for (const name of names) {
|
|
132
|
+
if (!Object.hasOwn(ops, name))
|
|
133
|
+
throw new Error(`${composer} failed: the dialect has no op "${name}".`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Derives a dialect whose named ops reject at call time, leaving the file unchanged, with a
|
|
138
|
+
* message naming the op, the dialect's extensions and `reason`. The ops stay on the handle and
|
|
139
|
+
* in its type, so one handle shape can serve every extension of a routed dialect
|
|
140
|
+
* (`byExtension`). Throws at composition on an op name the dialect does not have.
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* const jsx = withoutOps(defineDialect({ extensions: [".jsx"], ast: jsxAst, ops: tsx.ops }), ["addTypeImport"], ".jsx files have no type syntax");
|
|
144
|
+
*/
|
|
145
|
+
export function withoutOps(dialect, names, reason) {
|
|
146
|
+
assertKnownOps(dialect.ops, names, "withoutOps");
|
|
147
|
+
const files = dialect.extensions.join(", ");
|
|
148
|
+
const blocked = Object.fromEntries(names.map((name) => [
|
|
149
|
+
name,
|
|
150
|
+
() => {
|
|
151
|
+
throw dialectError(`${name} is not available for ${files} files — ${reason}. Use .modify() to make this edit directly.`);
|
|
152
|
+
},
|
|
153
|
+
]));
|
|
154
|
+
return defineDialect({ extensions: [...dialect.extensions], ast: dialect.ast, ops: { ...dialect.ops, ...blocked } });
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Derives a dialect whose named ops run `overrides` instead, with the same signatures. Throws at
|
|
158
|
+
* composition on an op name the dialect does not have; adding new ops is `withOps`'s job.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* const jsx = overrideOps(tsx, { addClassProperty: jsxClassProperty });
|
|
162
|
+
*/
|
|
163
|
+
export function overrideOps(dialect, overrides) {
|
|
164
|
+
assertKnownOps(dialect.ops, Object.keys(overrides), "overrideOps");
|
|
165
|
+
return defineDialect({ extensions: [...dialect.extensions], ast: dialect.ast, ops: { ...dialect.ops, ...overrides } });
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Routes `find` by file extension to one dialect per extension, each with its own AST and ops —
|
|
169
|
+
* a `.ts` dialect and an `.html` dialect may parse to different AST types. The longest matching
|
|
170
|
+
* extension wins (`.component.html` over `.html`); a path matching none rejects synchronously,
|
|
171
|
+
* naming every routed extension. Throws at composition on a key its dialect does not declare.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* export const react = byExtension({ ".tsx": tsxDialect, ".jsx": jsxDialect });
|
|
175
|
+
* react.find("src/App.jsx").setJsxProp("div", "hidden");
|
|
176
|
+
*/
|
|
177
|
+
export function byExtension(dialects) {
|
|
178
|
+
const extensions = Object.keys(dialects);
|
|
179
|
+
for (const extension of extensions) {
|
|
180
|
+
const declared = dialects[extension].extensions;
|
|
181
|
+
if (!declared.includes(extension)) {
|
|
182
|
+
throw new Error(`dialect routing failed: "${extension}" is not an extension its dialect declares (${declared.join(", ")}).`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const longestFirst = [...extensions].sort((a, b) => b.length - a.length);
|
|
186
|
+
return {
|
|
187
|
+
extensions,
|
|
188
|
+
find(path) {
|
|
189
|
+
const extension = longestFirst.find((candidate) => hasDeclaredExtension(path, [candidate]));
|
|
190
|
+
if (extension === undefined) {
|
|
191
|
+
throw dialectError(`find(${JSON.stringify(path)}) — the file's extension is not one this dialect declares (${extensions.join(", ")}).`);
|
|
192
|
+
}
|
|
193
|
+
return dialects[extension].find(path);
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
@@ -18,6 +18,8 @@ import { type SourceFile } from "ts-morph";
|
|
|
18
18
|
* never surfaced directly to the author.
|
|
19
19
|
*/
|
|
20
20
|
export declare function parse(source: string): SourceFile;
|
|
21
|
+
/** `parse` for `.jsx` files: `ScriptKind.JSX`, so `<div>` is always JSX, never a cast. */
|
|
22
|
+
export declare function parseJsx(source: string): SourceFile;
|
|
21
23
|
/**
|
|
22
24
|
* Prints the live AST back to a string — the ONLY serialization point for a React handle's
|
|
23
25
|
* flush. Re-prepends a BOM if the original parse observed one (see `hadBom` above) — ts-morph's
|
|
@@ -10,7 +10,8 @@ import { Project, NewLineKind, QuoteKind, IndentationText } from "ts-morph";
|
|
|
10
10
|
// `.tsx` extension is what engages `ScriptKind.Tsx` (ts-morph infers script kind from the
|
|
11
11
|
// extension, verified empirically against ts-morph@28.0.0 — no explicit scriptKind option
|
|
12
12
|
// needed, mirroring typescript/ast.ts's "the extension drives it" shape).
|
|
13
|
-
const
|
|
13
|
+
const TSX_PATH = "dialect-source.tsx";
|
|
14
|
+
const JSX_PATH = "dialect-source.jsx";
|
|
14
15
|
const BOM = "";
|
|
15
16
|
// ts-morph@28's `getFullText()` does not round-trip a leading UTF-8 BOM (same finding as
|
|
16
17
|
// typescript/ast.ts, same pinned ts-morph version) — a private per-SourceFile flag remembers
|
|
@@ -60,6 +61,13 @@ function detectQuoteKind(sourceFile) {
|
|
|
60
61
|
* never surfaced directly to the author.
|
|
61
62
|
*/
|
|
62
63
|
export function parse(source) {
|
|
64
|
+
return parseAs(source, TSX_PATH);
|
|
65
|
+
}
|
|
66
|
+
/** `parse` for `.jsx` files: `ScriptKind.JSX`, so `<div>` is always JSX, never a cast. */
|
|
67
|
+
export function parseJsx(source) {
|
|
68
|
+
return parseAs(source, JSX_PATH);
|
|
69
|
+
}
|
|
70
|
+
function parseAs(source, virtualPath) {
|
|
63
71
|
const startsWithBom = source.startsWith(BOM);
|
|
64
72
|
const body = startsWithBom ? source.slice(BOM.length) : source;
|
|
65
73
|
const project = new Project({
|
|
@@ -71,13 +79,13 @@ export function parse(source) {
|
|
|
71
79
|
usePrefixAndSuffixTextForRename: false,
|
|
72
80
|
},
|
|
73
81
|
});
|
|
74
|
-
const sourceFile = project.createSourceFile(
|
|
82
|
+
const sourceFile = project.createSourceFile(virtualPath, body);
|
|
75
83
|
project.manipulationSettings.set({ quoteKind: detectQuoteKind(sourceFile) });
|
|
76
84
|
if (startsWithBom)
|
|
77
85
|
hadBom.set(sourceFile, true);
|
|
78
86
|
const syntacticErrors = project.getProgram().getSyntacticDiagnostics(sourceFile);
|
|
79
87
|
if (syntacticErrors.length > 0) {
|
|
80
|
-
throw new Error("syntactically invalid
|
|
88
|
+
throw new Error("syntactically invalid JSX source");
|
|
81
89
|
}
|
|
82
90
|
return sourceFile;
|
|
83
91
|
}
|
|
@@ -17,15 +17,16 @@ type JsxOps = {
|
|
|
17
17
|
};
|
|
18
18
|
type ReactOps = typeof typescriptOps & JsxOps;
|
|
19
19
|
/**
|
|
20
|
-
* Opens a `.tsx` file for React-dialect-aware editing — the dialect's entry verb into
|
|
21
|
-
* that carries every `@pbuilder/sdk/typescript` op plus the JSX ops, chained on one
|
|
22
|
-
* (REQ-DG-01.2).
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* extensionless path is rejected the same way —
|
|
20
|
+
* Opens a `.tsx` or `.jsx` file for React-dialect-aware editing — the dialect's entry verb into
|
|
21
|
+
* a Session that carries every `@pbuilder/sdk/typescript` op plus the JSX ops, chained on one
|
|
22
|
+
* handle (REQ-DG-01.2). Both extensions get the same handle: on `.jsx`, which parses as plain
|
|
23
|
+
* JavaScript with JSX, `addTypeImport`, `addComponentProp` and `addClassProperty`'s `scope`/
|
|
24
|
+
* `readonly` reject at call time because they write TypeScript syntax, leaving the file
|
|
25
|
+
* unchanged. The gate below runs SYNCHRONOUSLY, before any op is chained and before any run
|
|
26
|
+
* flush, so a rejected path throws on the bare `find()` call itself. `.ts` files are TypeScript,
|
|
27
|
+
* not this dialect — use `@pbuilder/sdk/typescript`. Every other extension, dotfile basename,
|
|
28
|
+
* trailing-dot basename, and extensionless path is rejected the same way —
|
|
29
|
+
* no normalization, always an explicit `.tsx` or `.jsx`, so neither extension is ever assumed.
|
|
29
30
|
* Every gate rejection throws via `dialectError`, so its message carries the standard
|
|
30
31
|
* `dialect operation failed: ` prefix.
|
|
31
32
|
*
|
|
@@ -4,29 +4,36 @@
|
|
|
4
4
|
// no port-internal machinery imported anywhere in this tree (REQ-DG-04.1, fit-08/fit-10).
|
|
5
5
|
// The parser stays local: typescript/ast.ts parses as `.ts`, which rejects JSX syntax.
|
|
6
6
|
import { basename } from "node:path";
|
|
7
|
-
import { defineDialect, defineOpPack, withOps } from "../../core/define-dialect.js";
|
|
7
|
+
import { byExtension, defineDialect, defineOpPack, overrideOps, withOps, withoutOps } from "../../core/define-dialect.js";
|
|
8
8
|
import { dialectError } from "../../core/dialect-error.js";
|
|
9
|
+
import { addClassProperty } from "../typescript/class-property.js";
|
|
9
10
|
import { typescriptOps } from "../typescript/pack.js";
|
|
10
|
-
import { parse, print } from "./ast.js";
|
|
11
|
+
import { parse, parseJsx, print } from "./ast.js";
|
|
11
12
|
import { addComponentProp, addHookCall, addJsxChild, removeJsxProp, renameJsxElement, setJsxProp, wrapJsxElement, } from "./ops.js";
|
|
12
13
|
export * as astLibrary from "ts-morph";
|
|
13
14
|
const jsxOps = defineOpPack({ setJsxProp, removeJsxProp, addJsxChild, wrapJsxElement, addHookCall, addComponentProp, renameJsxElement });
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
const tsxDialect = withOps(defineDialect({ extensions: [".tsx"], ast: { parse, print }, ops: {} }), typescriptOps, jsxOps);
|
|
16
|
+
const jsxDialect = overrideOps(withoutOps(defineDialect({ extensions: [".jsx"], ast: { parse: parseJsx, print }, ops: tsxDialect.ops }), ["addTypeImport", "addComponentProp"], "they write TypeScript type syntax, which a .jsx file cannot hold"), {
|
|
17
|
+
addClassProperty: (ast, className, name, initializer, options) => {
|
|
18
|
+
if (options?.scope !== undefined || options?.readonly !== undefined) {
|
|
19
|
+
throw dialectError("addClassProperty's `scope` and `readonly` are TypeScript modifiers, which a .jsx file cannot hold — " +
|
|
20
|
+
"omit them, or use .modify() to make this edit directly.");
|
|
21
|
+
}
|
|
22
|
+
addClassProperty(ast, className, name, initializer);
|
|
23
|
+
},
|
|
18
24
|
});
|
|
19
|
-
const reactDialect =
|
|
25
|
+
const reactDialect = byExtension({ ".tsx": tsxDialect, ".jsx": jsxDialect });
|
|
20
26
|
/**
|
|
21
|
-
* Opens a `.tsx` file for React-dialect-aware editing — the dialect's entry verb into
|
|
22
|
-
* that carries every `@pbuilder/sdk/typescript` op plus the JSX ops, chained on one
|
|
23
|
-
* (REQ-DG-01.2).
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* extensionless path is rejected the same way —
|
|
27
|
+
* Opens a `.tsx` or `.jsx` file for React-dialect-aware editing — the dialect's entry verb into
|
|
28
|
+
* a Session that carries every `@pbuilder/sdk/typescript` op plus the JSX ops, chained on one
|
|
29
|
+
* handle (REQ-DG-01.2). Both extensions get the same handle: on `.jsx`, which parses as plain
|
|
30
|
+
* JavaScript with JSX, `addTypeImport`, `addComponentProp` and `addClassProperty`'s `scope`/
|
|
31
|
+
* `readonly` reject at call time because they write TypeScript syntax, leaving the file
|
|
32
|
+
* unchanged. The gate below runs SYNCHRONOUSLY, before any op is chained and before any run
|
|
33
|
+
* flush, so a rejected path throws on the bare `find()` call itself. `.ts` files are TypeScript,
|
|
34
|
+
* not this dialect — use `@pbuilder/sdk/typescript`. Every other extension, dotfile basename,
|
|
35
|
+
* trailing-dot basename, and extensionless path is rejected the same way —
|
|
36
|
+
* no normalization, always an explicit `.tsx` or `.jsx`, so neither extension is ever assumed.
|
|
30
37
|
* Every gate rejection throws via `dialectError`, so its message carries the standard
|
|
31
38
|
* `dialect operation failed: ` prefix.
|
|
32
39
|
*
|
|
@@ -51,23 +58,16 @@ const reactDialect = withOps(baseDialect, typescriptOps, jsxOps);
|
|
|
51
58
|
*/
|
|
52
59
|
export function find(path) {
|
|
53
60
|
const base = basename(path);
|
|
54
|
-
// A non-empty
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
// with no usable name" shape as `.babelrc` (suffix `babelrc`, not `.tsx`) — by the suffix
|
|
58
|
-
// rule REQ-RXD-02 already applies to dotfiles, `.tsx`'s suffix is `tsx`, not `.tsx`.
|
|
59
|
-
const stem = base.substring(0, base.length - ".tsx".length);
|
|
60
|
-
if (base.endsWith(".tsx") && stem.length > 0 && !stem.endsWith(".")) {
|
|
61
|
+
// A non-empty stem that does not end in a dot: `.tsx` and `a..tsx` name no file of that type.
|
|
62
|
+
const routed = reactDialect.extensions.some((extension) => base.endsWith(extension) && base.length > extension.length && !base.endsWith(`.${extension}`));
|
|
63
|
+
if (routed) {
|
|
61
64
|
return reactDialect.find(path);
|
|
62
65
|
}
|
|
63
66
|
if (!base.includes(".")) {
|
|
64
|
-
throw dialectError("find(path) — the React dialect requires an explicit .tsx extension; received a path with no extension. Append .tsx to the path.");
|
|
67
|
+
throw dialectError("find(path) — the React dialect requires an explicit .tsx or .jsx extension; received a path with no extension. Append .tsx or .jsx to the path.");
|
|
65
68
|
}
|
|
66
69
|
if (base.endsWith(".ts")) {
|
|
67
70
|
throw dialectError("find(path) — .ts is not a React file; use @pbuilder/sdk/typescript for TypeScript sources.");
|
|
68
71
|
}
|
|
69
|
-
|
|
70
|
-
throw dialectError("find(path) — .jsx is not supported in v1; tracked as a follow-up in the React dialect's op catalog.");
|
|
71
|
-
}
|
|
72
|
-
throw dialectError("find(path) — .tsx is the only extension the React dialect supports.");
|
|
72
|
+
throw dialectError("find(path) — .tsx and .jsx are the only extensions the React dialect supports.");
|
|
73
73
|
}
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifestVersion": 1,
|
|
3
3
|
"algorithm": "sha256",
|
|
4
4
|
"entry": "dist/bin/pbuilder-runner.js",
|
|
5
|
-
"packageVersion": "0.
|
|
5
|
+
"packageVersion": "0.12.0",
|
|
6
6
|
"files": [
|
|
7
7
|
{
|
|
8
8
|
"path": "dist/bin/pbuilder-runner.js",
|
|
@@ -98,7 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
{
|
|
100
100
|
"path": "package.json",
|
|
101
|
-
"sha256": "
|
|
101
|
+
"sha256": "dd84ad976cc9ec3ae2fba6d4adc7c2d5a7ce3eceeac47212a62727e7a2e96a9c"
|
|
102
102
|
}
|
|
103
103
|
]
|
|
104
104
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbuilder/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "The public TypeScript/Bun authoring layer for Project Builder schematics. Authors file mutations (create, replaceContent, remove, rename, move, copy, copyIn), lowers them to IR, and talks to the Project Builder engine over framed stdio (see docs/engine-sdk-wire-spec.md).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|