@pbuilder/sdk 0.10.0 → 0.10.1
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/dist/dialects/react/ast.d.ts +2 -1
- package/dist/dialects/react/ast.js +14 -2
- package/dist/dialects/react/ops.js +20 -2
- package/dist/dialects/typescript/ast.d.ts +3 -2
- package/dist/dialects/typescript/ast.js +15 -3
- package/dist/dialects/typescript/ops.js +22 -7
- package/dist/runner-manifest.json +2 -2
- package/package.json +1 -1
|
@@ -3,7 +3,8 @@ import { type SourceFile } from "ts-morph";
|
|
|
3
3
|
* Parses TSX source into a live ts-morph `SourceFile` — the React dialect's `Ast` (design
|
|
4
4
|
* §4.4). Constructs a FRESH `Project` per call (never a module-level singleton) whose
|
|
5
5
|
* `manipulationSettings.newLineKind` is derived from THIS source's own content
|
|
6
|
-
* (`detectNewLineKind`)
|
|
6
|
+
* (`detectNewLineKind`), and whose quote kind follows its module specifiers (`detectQuoteKind`).
|
|
7
|
+
* Indentation is a frozen constant.
|
|
7
8
|
*
|
|
8
9
|
* Failure posture (REQ-RXD-03.5 / REQ-DG-05.2): ts-morph's parser is fault-tolerant — it
|
|
9
10
|
* records diagnostics rather than throwing natively. This function surfaces a genuine parse
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// S-000: ts-morph parse/print pair for the React dialect (REQ-RXD-03, design §4.4, ADR-01).
|
|
2
2
|
// DUPLICATES (does not import) typescript/ast.ts's four fidelity primitives — BOM
|
|
3
|
-
// re-prepend, content-derived newline detection, a frozen per-parse ManipulationSettings,
|
|
3
|
+
// re-prepend, content-derived newline and quote detection, a frozen per-parse ManipulationSettings,
|
|
4
4
|
// and the syntactic-diagnostic throw — so `typescript/ast.ts` stays untouched and ts-morph
|
|
5
5
|
// stays leaf-isolated to one importer per dialect (fit-38 guards this repo-wide). A fresh
|
|
6
6
|
// `Project` per parse, never a module-level singleton, so the newLineKind setting can vary
|
|
@@ -31,11 +31,22 @@ function detectNewLineKind(source) {
|
|
|
31
31
|
function countOccurrences(source, needle) {
|
|
32
32
|
return source.split(needle).length - 1;
|
|
33
33
|
}
|
|
34
|
+
// The quote kind of the file's own module specifiers; single wins only on a strict majority,
|
|
35
|
+
// so a file with no specifiers (or a tie) keeps the double-quote default.
|
|
36
|
+
function detectQuoteKind(sourceFile) {
|
|
37
|
+
const specifiers = [...sourceFile.getImportDeclarations(), ...sourceFile.getExportDeclarations()].flatMap((decl) => {
|
|
38
|
+
const specifier = decl.getModuleSpecifier();
|
|
39
|
+
return specifier === undefined ? [] : [specifier.getQuoteKind()];
|
|
40
|
+
});
|
|
41
|
+
const single = specifiers.filter((kind) => kind === QuoteKind.Single).length;
|
|
42
|
+
return single > specifiers.length - single ? QuoteKind.Single : QuoteKind.Double;
|
|
43
|
+
}
|
|
34
44
|
/**
|
|
35
45
|
* Parses TSX source into a live ts-morph `SourceFile` — the React dialect's `Ast` (design
|
|
36
46
|
* §4.4). Constructs a FRESH `Project` per call (never a module-level singleton) whose
|
|
37
47
|
* `manipulationSettings.newLineKind` is derived from THIS source's own content
|
|
38
|
-
* (`detectNewLineKind`)
|
|
48
|
+
* (`detectNewLineKind`), and whose quote kind follows its module specifiers (`detectQuoteKind`).
|
|
49
|
+
* Indentation is a frozen constant.
|
|
39
50
|
*
|
|
40
51
|
* Failure posture (REQ-RXD-03.5 / REQ-DG-05.2): ts-morph's parser is fault-tolerant — it
|
|
41
52
|
* records diagnostics rather than throwing natively. This function surfaces a genuine parse
|
|
@@ -61,6 +72,7 @@ export function parse(source) {
|
|
|
61
72
|
},
|
|
62
73
|
});
|
|
63
74
|
const sourceFile = project.createSourceFile(VIRTUAL_PATH, body);
|
|
75
|
+
project.manipulationSettings.set({ quoteKind: detectQuoteKind(sourceFile) });
|
|
64
76
|
if (startsWithBom)
|
|
65
77
|
hadBom.set(sourceFile, true);
|
|
66
78
|
const syntacticErrors = project.getProgram().getSyntacticDiagnostics(sourceFile);
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
// validation + `from`'s pinned-escaping trust, design §4.4/ADR-05). Written FRESH — NOT
|
|
9
9
|
// copied from the TypeScript dialect's `addImport` (src/dialects/typescript/ops.ts), which
|
|
10
10
|
// validates nothing; that laxity is a confirmed vulnerability class this op does not repeat.
|
|
11
|
-
import { SyntaxKind, } from "ts-morph";
|
|
11
|
+
import { Node, SyntaxKind, } from "ts-morph";
|
|
12
12
|
import { dialectError } from "../../core/dialect-error.js";
|
|
13
13
|
import { assertValidAttributeName, assertValidElementName, assertValidImportBinding, validatedOp, } from "../../core/jsx-name-validator.js";
|
|
14
14
|
import { claimedNameTail, multiMatchTail, zeroMatchTail } from "../../core/reject-tail.js";
|
|
@@ -182,5 +182,23 @@ export const addImport = validatedOp(([name]) => {
|
|
|
182
182
|
mergeTarget.addNamedImport(name);
|
|
183
183
|
return;
|
|
184
184
|
}
|
|
185
|
-
|
|
185
|
+
const dropSemicolon = omitsSemicolons(ast);
|
|
186
|
+
const inserted = ast.addImportDeclaration({ moduleSpecifier: from, namedImports: [name] });
|
|
187
|
+
if (dropSemicolon)
|
|
188
|
+
inserted.replaceWithText(inserted.getText().slice(0, -1));
|
|
186
189
|
});
|
|
190
|
+
// Duplicated from typescript/ops.ts per ADR-01. A fresh import keeps the file's statement-terminating semicolon style: ts-morph always
|
|
191
|
+
// prints one, so it is dropped only when the file's terminable top-level statements mostly
|
|
192
|
+
// omit it (a file with no such statements, or a tie, keeps it).
|
|
193
|
+
function omitsSemicolons(ast) {
|
|
194
|
+
const terminable = ast
|
|
195
|
+
.getStatements()
|
|
196
|
+
.filter((statement) => Node.isImportDeclaration(statement) ||
|
|
197
|
+
Node.isExportDeclaration(statement) ||
|
|
198
|
+
Node.isExportAssignment(statement) ||
|
|
199
|
+
Node.isVariableStatement(statement) ||
|
|
200
|
+
Node.isExpressionStatement(statement) ||
|
|
201
|
+
Node.isTypeAliasDeclaration(statement));
|
|
202
|
+
const terminated = terminable.filter((statement) => statement.getText().endsWith(";")).length;
|
|
203
|
+
return terminable.length - terminated > terminated;
|
|
204
|
+
}
|
|
@@ -15,8 +15,9 @@ export declare function detectNewLineKind(source: string): NewLineKind;
|
|
|
15
15
|
* Parses TypeScript source into a live ts-morph `SourceFile` — the dialect's `Ast` (design
|
|
16
16
|
* §4.4). Constructs a FRESH `Project` per call (never a module-level singleton) whose
|
|
17
17
|
* `manipulationSettings.newLineKind` is derived from THIS source's own content
|
|
18
|
-
* (`detectNewLineKind`) — the frozen determinism pin (REQ-TSD-02).
|
|
19
|
-
*
|
|
18
|
+
* (`detectNewLineKind`) — the frozen determinism pin (REQ-TSD-02). The quote kind is likewise
|
|
19
|
+
* derived from the source's own module specifiers (`detectQuoteKind`, double when the source
|
|
20
|
+
* shows no preference); indentation is a frozen constant. Neither reads the host.
|
|
20
21
|
*
|
|
21
22
|
* Failure posture (REQ-TSD-04.1 / REQ-DG-05.2): ts-morph's parser is deliberately
|
|
22
23
|
* FAULT-TOLERANT — it does not natively throw on malformed syntax, it records diagnostics.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// S-002: ts-morph parse/print pair for the TypeScript dialect (REQ-TSD-02, design §4.4).
|
|
2
|
-
// FROZEN ManipulationSettings + an explicit, CONTENT-DERIVED newLineKind (never host-OS) —
|
|
2
|
+
// FROZEN ManipulationSettings + an explicit, CONTENT-DERIVED newLineKind and quoteKind (never host-OS) —
|
|
3
3
|
// a fresh `Project` per parse so the setting can vary per source, never a module-level
|
|
4
4
|
// singleton. No language-service formatter is ever invoked (REQ-TSD-02.3): this module calls
|
|
5
5
|
// no `.formatText()`/language-service API anywhere.
|
|
@@ -39,12 +39,23 @@ export function detectNewLineKind(source) {
|
|
|
39
39
|
function countOccurrences(source, needle) {
|
|
40
40
|
return source.split(needle).length - 1;
|
|
41
41
|
}
|
|
42
|
+
// The quote kind of the file's own module specifiers; single wins only on a strict majority,
|
|
43
|
+
// so a file with no specifiers (or a tie) keeps the double-quote default.
|
|
44
|
+
function detectQuoteKind(sourceFile) {
|
|
45
|
+
const specifiers = [...sourceFile.getImportDeclarations(), ...sourceFile.getExportDeclarations()].flatMap((decl) => {
|
|
46
|
+
const specifier = decl.getModuleSpecifier();
|
|
47
|
+
return specifier === undefined ? [] : [specifier.getQuoteKind()];
|
|
48
|
+
});
|
|
49
|
+
const single = specifiers.filter((kind) => kind === QuoteKind.Single).length;
|
|
50
|
+
return single > specifiers.length - single ? QuoteKind.Single : QuoteKind.Double;
|
|
51
|
+
}
|
|
42
52
|
/**
|
|
43
53
|
* Parses TypeScript source into a live ts-morph `SourceFile` — the dialect's `Ast` (design
|
|
44
54
|
* §4.4). Constructs a FRESH `Project` per call (never a module-level singleton) whose
|
|
45
55
|
* `manipulationSettings.newLineKind` is derived from THIS source's own content
|
|
46
|
-
* (`detectNewLineKind`) — the frozen determinism pin (REQ-TSD-02).
|
|
47
|
-
*
|
|
56
|
+
* (`detectNewLineKind`) — the frozen determinism pin (REQ-TSD-02). The quote kind is likewise
|
|
57
|
+
* derived from the source's own module specifiers (`detectQuoteKind`, double when the source
|
|
58
|
+
* shows no preference); indentation is a frozen constant. Neither reads the host.
|
|
48
59
|
*
|
|
49
60
|
* Failure posture (REQ-TSD-04.1 / REQ-DG-05.2): ts-morph's parser is deliberately
|
|
50
61
|
* FAULT-TOLERANT — it does not natively throw on malformed syntax, it records diagnostics.
|
|
@@ -73,6 +84,7 @@ export function parse(source) {
|
|
|
73
84
|
},
|
|
74
85
|
});
|
|
75
86
|
const sourceFile = project.createSourceFile(VIRTUAL_PATH, body);
|
|
87
|
+
project.manipulationSettings.set({ quoteKind: detectQuoteKind(sourceFile) });
|
|
76
88
|
if (startsWithBom)
|
|
77
89
|
hadBom.set(sourceFile, true);
|
|
78
90
|
const syntacticErrors = project.getProgram().getSyntacticDiagnostics(sourceFile);
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
// runs INLINE as `addImport`'s first statement, strictly BEFORE Step 1/2/3/4 ever evaluate
|
|
27
27
|
// `name` (REQ-TSD-13.6 precedence pin). Its PATH-LESS reject shape is deliberately distinct
|
|
28
28
|
// from Step 2's path-ful `dialectError` tail below — see `addImport`'s own JSDoc.
|
|
29
|
-
import { SyntaxKind, } from "ts-morph";
|
|
29
|
+
import { Node, SyntaxKind, } from "ts-morph";
|
|
30
30
|
import { dialectError } from "../../core/dialect-error.js";
|
|
31
31
|
import { handlePathFor } from "../../core/dialect-handle.js";
|
|
32
32
|
import { elementNameEcho } from "../../core/reject-tail.js";
|
|
@@ -245,13 +245,28 @@ function claimedBindingError(ast, opName, name) {
|
|
|
245
245
|
// A fresh import declaration goes after a leading directive prologue, never above it
|
|
246
246
|
// (REQ-TSD-01.21).
|
|
247
247
|
function insertImport(ast, structure) {
|
|
248
|
+
const dropSemicolon = omitsSemicolons(ast);
|
|
248
249
|
const directiveCount = leadingDirectiveCount(ast);
|
|
249
|
-
|
|
250
|
-
ast.addImportDeclaration(structure)
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
250
|
+
const inserted = directiveCount === 0
|
|
251
|
+
? ast.addImportDeclaration(structure)
|
|
252
|
+
: ast.insertImportDeclaration(directiveInsertIndex(ast, directiveCount), structure);
|
|
253
|
+
if (dropSemicolon)
|
|
254
|
+
inserted.replaceWithText(inserted.getText().slice(0, -1));
|
|
255
|
+
}
|
|
256
|
+
// A fresh import keeps the file's statement-terminating semicolon style: ts-morph always
|
|
257
|
+
// prints one, so it is dropped only when the file's terminable top-level statements mostly
|
|
258
|
+
// omit it (a file with no such statements, or a tie, keeps it).
|
|
259
|
+
function omitsSemicolons(ast) {
|
|
260
|
+
const terminable = ast
|
|
261
|
+
.getStatements()
|
|
262
|
+
.filter((statement) => Node.isImportDeclaration(statement) ||
|
|
263
|
+
Node.isExportDeclaration(statement) ||
|
|
264
|
+
Node.isExportAssignment(statement) ||
|
|
265
|
+
Node.isVariableStatement(statement) ||
|
|
266
|
+
Node.isExpressionStatement(statement) ||
|
|
267
|
+
Node.isTypeAliasDeclaration(statement));
|
|
268
|
+
const terminated = terminable.filter((statement) => statement.getText().endsWith(";")).length;
|
|
269
|
+
return terminable.length - terminated > terminated;
|
|
255
270
|
}
|
|
256
271
|
// The value-bound (not whole-declaration type-only) declarations importing `from`.
|
|
257
272
|
function valueImportsOf(ast, from) {
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"manifestVersion": 1,
|
|
3
3
|
"algorithm": "sha256",
|
|
4
4
|
"entry": "dist/bin/pbuilder-runner.js",
|
|
5
|
-
"packageVersion": "0.10.
|
|
5
|
+
"packageVersion": "0.10.1",
|
|
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": "a1394881932ffe36d70d2e8e942ae21056a7bd69c5a6a868b825b181d966deda"
|
|
102
102
|
}
|
|
103
103
|
]
|
|
104
104
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbuilder/sdk",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.1",
|
|
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",
|