@pbuilder/sdk 0.1.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/README.md +202 -0
- package/dist/bin/pbuilder-codegen.js +506 -0
- package/dist/bin/pbuilder-runner.d.ts +1 -0
- package/dist/bin/pbuilder-runner.js +47 -0
- package/dist/commons/classify-content.d.ts +40 -0
- package/dist/commons/classify-content.js +32 -0
- package/dist/commons/index.d.ts +271 -0
- package/dist/commons/index.js +276 -0
- package/dist/conformance/index.d.ts +115 -0
- package/dist/conformance/index.js +218 -0
- package/dist/conformance/run-vehicle.d.ts +19 -0
- package/dist/conformance/run-vehicle.js +104 -0
- package/dist/core/authoring-error.d.ts +116 -0
- package/dist/core/authoring-error.js +205 -0
- package/dist/core/base-handle.d.ts +17 -0
- package/dist/core/base-handle.js +4 -0
- package/dist/core/context.d.ts +85 -0
- package/dist/core/context.js +356 -0
- package/dist/core/deep-equal.d.ts +1 -0
- package/dist/core/deep-equal.js +29 -0
- package/dist/core/define-dialect.d.ts +94 -0
- package/dist/core/define-dialect.js +121 -0
- package/dist/core/dialect-error.d.ts +8 -0
- package/dist/core/dialect-error.js +26 -0
- package/dist/core/dialect-handle.d.ts +21 -0
- package/dist/core/dialect-handle.js +382 -0
- package/dist/core/directive-factory.d.ts +47 -0
- package/dist/core/directive-factory.js +154 -0
- package/dist/core/emit-rejection.d.ts +19 -0
- package/dist/core/emit-rejection.js +31 -0
- package/dist/core/engine-client.d.ts +7 -0
- package/dist/core/engine-client.js +5 -0
- package/dist/core/fs-errors.d.ts +1 -0
- package/dist/core/fs-errors.js +5 -0
- package/dist/core/handle-state.d.ts +24 -0
- package/dist/core/handle-state.js +4 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +6 -0
- package/dist/core/jsx-name-validator.d.ts +19 -0
- package/dist/core/jsx-name-validator.js +104 -0
- package/dist/core/reject-tail.d.ts +23 -0
- package/dist/core/reject-tail.js +63 -0
- package/dist/core/schema/input-rejection.d.ts +4 -0
- package/dist/core/schema/input-rejection.js +32 -0
- package/dist/core/schema/schema-digest.d.ts +1 -0
- package/dist/core/schema/schema-digest.js +7 -0
- package/dist/core/schema/schema-discovery.d.ts +13 -0
- package/dist/core/schema/schema-discovery.js +51 -0
- package/dist/core/schema/schema-locate.d.ts +10 -0
- package/dist/core/schema/schema-locate.js +192 -0
- package/dist/core/schema/schema-model.d.ts +18 -0
- package/dist/core/schema/schema-model.js +15 -0
- package/dist/core/schema/schema-parse.d.ts +20 -0
- package/dist/core/schema/schema-parse.js +80 -0
- package/dist/core/schema/schema-sufficiency.d.ts +17 -0
- package/dist/core/schema/schema-sufficiency.js +54 -0
- package/dist/core/schema/schema-validate.d.ts +14 -0
- package/dist/core/schema/schema-validate.js +64 -0
- package/dist/core/session.d.ts +13 -0
- package/dist/core/session.js +72 -0
- package/dist/core/wire.d.ts +87 -0
- package/dist/core/wire.js +49 -0
- package/dist/dialects/react/ast.d.ts +25 -0
- package/dist/dialects/react/ast.js +80 -0
- package/dist/dialects/react/index.d.ts +39 -0
- package/dist/dialects/react/index.js +71 -0
- package/dist/dialects/react/ops.d.ts +56 -0
- package/dist/dialects/react/ops.js +186 -0
- package/dist/dialects/typescript/ast.d.ts +42 -0
- package/dist/dialects/typescript/ast.js +93 -0
- package/dist/dialects/typescript/index.d.ts +32 -0
- package/dist/dialects/typescript/index.js +40 -0
- package/dist/dialects/typescript/ops.d.ts +123 -0
- package/dist/dialects/typescript/ops.js +365 -0
- package/dist/dry-run/index.d.ts +2 -0
- package/dist/dry-run/index.js +1 -0
- package/dist/dry-run/plan.d.ts +69 -0
- package/dist/dry-run/plan.js +44 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +3 -0
- package/dist/runner-manifest.json +104 -0
- package/dist/scaffold/classify-transport.d.ts +57 -0
- package/dist/scaffold/classify-transport.js +123 -0
- package/dist/scaffold/containment.d.ts +54 -0
- package/dist/scaffold/containment.js +242 -0
- package/dist/scaffold/expander.d.ts +30 -0
- package/dist/scaffold/expander.js +161 -0
- package/dist/scaffold/filename-pipeline.d.ts +39 -0
- package/dist/scaffold/filename-pipeline.js +125 -0
- package/dist/scaffold/index.d.ts +45 -0
- package/dist/scaffold/index.js +99 -0
- package/dist/scaffold/walk.d.ts +25 -0
- package/dist/scaffold/walk.js +124 -0
- package/dist/testing/contract-fake.d.ts +17 -0
- package/dist/testing/contract-fake.js +221 -0
- package/dist/testing/index.d.ts +84 -0
- package/dist/testing/index.js +82 -0
- package/dist/testing/rejection-messages.d.ts +7 -0
- package/dist/testing/rejection-messages.js +12 -0
- package/dist/transport/bootstrap-bridge.d.ts +33 -0
- package/dist/transport/bootstrap-bridge.js +65 -0
- package/dist/transport/error-text.d.ts +8 -0
- package/dist/transport/error-text.js +44 -0
- package/dist/transport/exit-codes.d.ts +2 -0
- package/dist/transport/exit-codes.js +35 -0
- package/dist/transport/factory-pointer.d.ts +32 -0
- package/dist/transport/factory-pointer.js +63 -0
- package/dist/transport/frame-reader.d.ts +10 -0
- package/dist/transport/frame-reader.js +81 -0
- package/dist/transport/framing.d.ts +20 -0
- package/dist/transport/framing.js +98 -0
- package/dist/transport/runner.d.ts +25 -0
- package/dist/transport/runner.js +308 -0
- package/dist/transport/single-instance-probe.d.ts +23 -0
- package/dist/transport/single-instance-probe.js +119 -0
- package/dist/transport/stdio-engine-client.d.ts +27 -0
- package/dist/transport/stdio-engine-client.js +243 -0
- package/dist/transport/wire-protocol.d.ts +33 -0
- package/dist/transport/wire-protocol.js +22 -0
- package/package.json +93 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { SourceFile } from "ts-morph";
|
|
2
|
+
import { type Handle } from "../../core/define-dialect.ts";
|
|
3
|
+
type TypeScriptOps = {
|
|
4
|
+
addImport: (ast: SourceFile, name: string, from: string) => void;
|
|
5
|
+
addFunction: (ast: SourceFile, name: string, source: string, options?: {
|
|
6
|
+
export?: boolean;
|
|
7
|
+
}) => void;
|
|
8
|
+
addVariable: (ast: SourceFile, name: string, initializer: string, options?: {
|
|
9
|
+
export?: boolean;
|
|
10
|
+
kind?: "const" | "let" | "var";
|
|
11
|
+
}) => void;
|
|
12
|
+
addClass: (ast: SourceFile, name: string, source: string, options?: {
|
|
13
|
+
export?: boolean;
|
|
14
|
+
}) => void;
|
|
15
|
+
removeImport: (ast: SourceFile, name: string, from: string) => void;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Opens a TypeScript file for dialect-aware editing — the dialect's entry verb into a run
|
|
19
|
+
* (REQ-DG-01.2). Returns an awaitable, chainable `Handle` exposing the universal `.modify()`
|
|
20
|
+
* escape hatch plus the dialect's structured ops (`addImport`, `addFunction`, `addVariable`,
|
|
21
|
+
* `addClass`, `removeImport`). Reads route through `Session.read` only (REQ-MC-03); edits
|
|
22
|
+
* coalesce into a single `modify` at flush (REQ-MC-01/02).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import * as ts from "@pbuilder/sdk/typescript";
|
|
26
|
+
*
|
|
27
|
+
* export const run = async () => {
|
|
28
|
+
* await ts.find("src/index.ts").addImport("readFileSync", "node:fs");
|
|
29
|
+
* };
|
|
30
|
+
*/
|
|
31
|
+
export declare function find(path: string): Handle<"found", SourceFile, TypeScriptOps>;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// S-002: the TypeScript dialect module — `@pbuilder/sdk/typescript` (REQ-TSD-01, frozen
|
|
2
|
+
// subpath, ADR-0014 amendment). Composes the sanctioned kit surface ONLY
|
|
3
|
+
// (`defineDialect`/`defineOpPack`/`withOps` from `../../core/define-dialect.ts`) — no
|
|
4
|
+
// port-internal machinery is imported anywhere in this tree (REQ-DG-04.1, see
|
|
5
|
+
// test/fitness/fit-08-no-kit-bleed.test.ts and fit-10-engine-client-port-guard.test.ts).
|
|
6
|
+
// Namespace import surface (ADR-0003): `import * as ts from "@pbuilder/sdk/typescript"`,
|
|
7
|
+
// then `ts.find(path)`.
|
|
8
|
+
import { defineDialect, defineOpPack, withOps } from "../../core/define-dialect.js";
|
|
9
|
+
import { parse, print } from "./ast.js";
|
|
10
|
+
import { addImport, addFunction, addVariable, addClass, removeImport } from "./ops.js";
|
|
11
|
+
const opsPack = defineOpPack({
|
|
12
|
+
addImport,
|
|
13
|
+
addFunction,
|
|
14
|
+
addVariable,
|
|
15
|
+
addClass,
|
|
16
|
+
removeImport,
|
|
17
|
+
});
|
|
18
|
+
const baseDialect = defineDialect({
|
|
19
|
+
extensions: [".ts"],
|
|
20
|
+
ast: { parse, print },
|
|
21
|
+
ops: {},
|
|
22
|
+
});
|
|
23
|
+
const typescriptDialect = withOps(baseDialect, opsPack);
|
|
24
|
+
/**
|
|
25
|
+
* Opens a TypeScript file for dialect-aware editing — the dialect's entry verb into a run
|
|
26
|
+
* (REQ-DG-01.2). Returns an awaitable, chainable `Handle` exposing the universal `.modify()`
|
|
27
|
+
* escape hatch plus the dialect's structured ops (`addImport`, `addFunction`, `addVariable`,
|
|
28
|
+
* `addClass`, `removeImport`). Reads route through `Session.read` only (REQ-MC-03); edits
|
|
29
|
+
* coalesce into a single `modify` at flush (REQ-MC-01/02).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import * as ts from "@pbuilder/sdk/typescript";
|
|
33
|
+
*
|
|
34
|
+
* export const run = async () => {
|
|
35
|
+
* await ts.find("src/index.ts").addImport("readFileSync", "node:fs");
|
|
36
|
+
* };
|
|
37
|
+
*/
|
|
38
|
+
export function find(path) {
|
|
39
|
+
return typescriptDialect.find(path);
|
|
40
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { type SourceFile } from "ts-morph";
|
|
2
|
+
/**
|
|
3
|
+
* Adds `import { name } from "from";`, or merges `name` into an EXISTING NAMED-import
|
|
4
|
+
* clause from the SAME module `from` if one already exists (REQ-TSD-01). `name` is
|
|
5
|
+
* validated at the op boundary BEFORE any of the four branches below ever run (REQ-TSD-13)
|
|
6
|
+
* — see the trust-boundary note at the end of this doc.
|
|
7
|
+
*
|
|
8
|
+
* 0. Validation (REQ-TSD-13, runs FIRST, strictly before Step 1): `name` MUST be a valid
|
|
9
|
+
* plain-JS identifier, MUST NOT be a reserved or strict-mode-restricted word, and MUST
|
|
10
|
+
* NOT be a denylisted name (`assertValidImportBinding`, reused verbatim from
|
|
11
|
+
* `src/core/jsx-name-validator.ts`) — REJECTS via a PATH-LESS `dialectError` before Step
|
|
12
|
+
* 1/2/3/4 ever evaluate `name`, even when `name` would ALSO collide under Step 2
|
|
13
|
+
* (REQ-TSD-13.6, precedence pinned: validation wins, never the collision message).
|
|
14
|
+
* 1. Already-bound (idempotency, SAME module only): if an EXISTING declaration for `from`
|
|
15
|
+
* has a VALUE-BOUND specifier whose local name equals `name`, AND that specifier is a
|
|
16
|
+
* default specifier, a namespace specifier, an UNALIASED named specifier, or a
|
|
17
|
+
* SELF-ALIASED named specifier (`{ name as name }`) — this is a no-op. The self-aliased
|
|
18
|
+
* case is a DELIBERATE DEVIATION from the `react` dialect's twin predicate, which rejects
|
|
19
|
+
* it (REQ-TSD-01.15, owner-ratified). Runs strictly BEFORE Step 2 (REQ-TSD-01.24,
|
|
20
|
+
* mutant-kill ordering invariant): a same-module already-bound specifier is a no-op,
|
|
21
|
+
* NEVER a collision reject.
|
|
22
|
+
* 2. Collision: otherwise, if `name` is CLAIMED anywhere in the file — EITHER the local name
|
|
23
|
+
* of ANY import specifier, in ANY module, value-bound or type-only alike, OR a top-level
|
|
24
|
+
* value-namespace declaration (`isValueNamespaceClaimed`) — REJECT via `dialectError`
|
|
25
|
+
* BEFORE any AST mutation.
|
|
26
|
+
* 3. Merge: otherwise, if an EXISTING declaration for `from` has a non-type-only
|
|
27
|
+
* named-import clause — INCLUDING an empty one (`import {} from "from"`) — add a NEW,
|
|
28
|
+
* UNALIASED named specifier to it. Walks EVERY declaration for `from` (a module can be
|
|
29
|
+
* imported via several separate declarations) and merges into the FIRST one in source
|
|
30
|
+
* order (REQ-TSD-01.22/.25 — a known, deliberate first-match asymmetry; contrast
|
|
31
|
+
* `removeImport`, which also SEARCHES every declaration matching `from` but REMOVES the
|
|
32
|
+
* binding from only the first one it finds it in, then returns — see `removeImport`'s own
|
|
33
|
+
* JSDoc). A side-effect-only import (`import "from";`, no
|
|
34
|
+
* clause at all) is never a merge target and is never converted into a combined form
|
|
35
|
+
* (REQ-TSD-01.20, Class B) — it is left byte-unchanged, coexisting beside the fresh
|
|
36
|
+
* declaration Step 4 inserts. (Reaching this step guarantees `name` is claimed nowhere in
|
|
37
|
+
* the file, so the merge is always safe.)
|
|
38
|
+
* 4. Create: otherwise (no declaration for `from`; every one is type-only, default-only,
|
|
39
|
+
* namespace-only, or side-effect-only), INSERT a FRESH, SEPARATE
|
|
40
|
+
* `import { name } from "from";` — a type-only, default-only, or namespace-only
|
|
41
|
+
* declaration's clause structure is NEVER mutated to graft a named binding onto it. A
|
|
42
|
+
* leading directive prologue (`"use client";` and similar) is preserved as the file's
|
|
43
|
+
* first statement(s) — the fresh declaration is inserted AFTER it, never above
|
|
44
|
+
* (REQ-TSD-01.21). (Reaching this step also guarantees `name` is claimed nowhere in the
|
|
45
|
+
* file, so the fresh declaration never collides.)
|
|
46
|
+
*
|
|
47
|
+
* Idempotent: calling this twice with the same `name`+`from` produces a single import line,
|
|
48
|
+
* never a duplicate (REQ-TSD-03.10).
|
|
49
|
+
*
|
|
50
|
+
* Trust boundary (REQ-TSD-13.5): `name` and `from` are the ONLY two channels this op
|
|
51
|
+
* validates. `addFunction`/`addVariable`/`addClass`'s own `name`/`source`/`initializer`
|
|
52
|
+
* arguments remain RAW-SPLICED, author-trusted input this change does NOT validate or
|
|
53
|
+
* protect — that gap is tracked separately in `project/pending-changes`, out of this
|
|
54
|
+
* change's scope.
|
|
55
|
+
*/
|
|
56
|
+
export declare function addImport(ast: SourceFile, name: string, from: string): void;
|
|
57
|
+
type DeclarationOptions = {
|
|
58
|
+
export?: boolean;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Inserts a new top-level function declaration `{export }function {name}{source}` at the
|
|
62
|
+
* end of the file, in call order (ts-morph `addStatements`, REQ-TSD-09). Rejects with a
|
|
63
|
+
* pinned `dialectError` when a value-namespace declaration or import binding already
|
|
64
|
+
* exists under `name` (ADR-0039) — `type`/`interface` are exempt.
|
|
65
|
+
*
|
|
66
|
+
* `source` INCLUDES the function's `{ … }` braces — contrast `addClass`, whose `source`
|
|
67
|
+
* EXCLUDES them (the op supplies the braces itself).
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* // source includes braces:
|
|
71
|
+
* ts.find(path).addFunction("hi", "(): void {}"); // -> function hi(): void {}
|
|
72
|
+
* // contrast addClass, whose source EXCLUDES braces (the op adds them).
|
|
73
|
+
*/
|
|
74
|
+
export declare function addFunction(ast: SourceFile, name: string, source: string, options?: DeclarationOptions): void;
|
|
75
|
+
/**
|
|
76
|
+
* Inserts a new top-level variable declaration `{export }{kind} {name} = {initializer};` at
|
|
77
|
+
* the end of the file, in call order (ts-morph `addStatements`, REQ-TSD-10). `kind` defaults
|
|
78
|
+
* to `"const"`. Rejects with a pinned `dialectError` under the SAME collision-namespace rule
|
|
79
|
+
* as `addFunction` (ADR-0039) — `type`/`interface` are exempt.
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ts.find(path).addVariable("PI", "3.14159"); // -> const PI = 3.14159;
|
|
83
|
+
* ts.find(path).addVariable("counter", "0", { export: true, kind: "let" }); // -> export let counter = 0;
|
|
84
|
+
*/
|
|
85
|
+
export declare function addVariable(ast: SourceFile, name: string, initializer: string, options?: DeclarationOptions & {
|
|
86
|
+
kind?: "const" | "let" | "var";
|
|
87
|
+
}): void;
|
|
88
|
+
/**
|
|
89
|
+
* Inserts a new top-level class declaration `{export }class {name} {\n{source}\n}` at the
|
|
90
|
+
* end of the file, in call order (ts-morph `addStatements`, REQ-TSD-11). Rejects with a
|
|
91
|
+
* pinned `dialectError` under the SAME collision-namespace rule as `addFunction` (ADR-0039)
|
|
92
|
+
* — `type`/`interface` are exempt.
|
|
93
|
+
*
|
|
94
|
+
* `source` EXCLUDES the class's `{ … }` braces — contrast `addFunction`, whose `source`
|
|
95
|
+
* INCLUDES them (that op supplies no braces of its own; this one does).
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* // source excludes braces:
|
|
99
|
+
* ts.find(path).addClass("Point", " x = 0;"); // -> class Point {\n x = 0;\n}
|
|
100
|
+
* // contrast addFunction, whose source INCLUDES braces (the op does NOT add them itself).
|
|
101
|
+
*/
|
|
102
|
+
export declare function addClass(ast: SourceFile, name: string, source: string, options?: DeclarationOptions): void;
|
|
103
|
+
/**
|
|
104
|
+
* Removes the named binding from `from`'s import clause (REQ-TSD-08). Idempotent: an
|
|
105
|
+
* absent binding is a no-op. Removing the LAST named binding deletes the entire import
|
|
106
|
+
* statement — no dangling `import {} from "from"`. Matched by the module-EXPORTED name,
|
|
107
|
+
* not the local alias (`{ readFileSync as rf }` is matched by `"readFileSync"`). Scope:
|
|
108
|
+
* NAMED-binding imports only — default and namespace imports are untouched, and a
|
|
109
|
+
* declaration mixing a default/namespace import alongside named bindings is never
|
|
110
|
+
* whole-statement-deleted (only the matched named specifier is removed).
|
|
111
|
+
*
|
|
112
|
+
* Judgment-day round 1 (Issue 2): a module can be imported via SEVERAL separate
|
|
113
|
+
* declarations (`import { a } from "m"; import { b } from "m";`, legal TS) — this SEARCHES
|
|
114
|
+
* every declaration matching `from`, not just the first, to locate whichever one actually
|
|
115
|
+
* contains `name`. Once found, the binding is removed from that ONE declaration and the
|
|
116
|
+
* function returns — a legal file never binds the same local name twice, so this is
|
|
117
|
+
* observationally a full removal on any real input; on the illegal, hand-authored fixture
|
|
118
|
+
* where `name` is duplicated across two declarations for `from` (REQ-TSD-01.25, CORRECTED
|
|
119
|
+
* V3.2), only the FIRST one loses the binding — the search is all-declarations, the removal
|
|
120
|
+
* is first-match-only. Sibling declarations from the same module are never touched.
|
|
121
|
+
*/
|
|
122
|
+
export declare function removeImport(ast: SourceFile, name: string, from: string): void;
|
|
123
|
+
export {};
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
// S-002: the `addImport` structured op (REQ-TSD-01, frozen call signature
|
|
2
|
+
// `addImport(name: string, from: string)`, design §4.4).
|
|
3
|
+
//
|
|
4
|
+
// S-001: `addFunction` (REQ-TSD-09) + the shared `assertNoCollision` predicate it (and
|
|
5
|
+
// S-003's addVariable/addClass) reuse (design §4.5 ADR-0039).
|
|
6
|
+
//
|
|
7
|
+
// S-003: `addVariable` (REQ-TSD-10) + `addClass` (REQ-TSD-11) — same insert-mechanism and
|
|
8
|
+
// collision rule as addFunction, different declaration kinds.
|
|
9
|
+
//
|
|
10
|
+
// ts-addimport-collision/S-000: `addImport`'s original naive first-match unconditional
|
|
11
|
+
// merge is REPLACED by the V8 four-branch algorithm ported from the react leaf verbatim
|
|
12
|
+
// (`react/ops.ts:81-234`, design §4.1/§4.3) — idempotency (Step 1) -> merge (Step 3) ->
|
|
13
|
+
// create (Step 4).
|
|
14
|
+
//
|
|
15
|
+
// ts-addimport-collision/S-001: Step 2 (file-wide collision reject) lands here, strictly
|
|
16
|
+
// AFTER Step 1 (REQ-TSD-01.24, mutant-kill ordering invariant) and before Step 3/4.
|
|
17
|
+
// `isValueNamespaceClaimed` is EXTRACTED from `assertNoCollision` below (design §4.5 ADR-01)
|
|
18
|
+
// and consumed by BOTH `assertNoCollision` (behaviour-preserving, siblings' own import-scan
|
|
19
|
+
// posture UNCHANGED) and `addImport`'s new Step 2 (design §4.4 — TS house-style tail, built
|
|
20
|
+
// inline, NOT reusing `assertNoCollision`'s own message: "two bindings sharing a name" here,
|
|
21
|
+
// vs "two value declarations sharing a name" there — `addImport`'s collision surface also
|
|
22
|
+
// fires on import-vs-import and type-only claims, which are not value declarations, F10).
|
|
23
|
+
//
|
|
24
|
+
// ts-addimport-collision/S-002: REQ-TSD-13's injection-safety validation gate lands here —
|
|
25
|
+
// `assertValidImportBinding` (reused verbatim from `src/core/jsx-name-validator.ts`, ADR-02)
|
|
26
|
+
// runs INLINE as `addImport`'s first statement, strictly BEFORE Step 1/2/3/4 ever evaluate
|
|
27
|
+
// `name` (REQ-TSD-13.6 precedence pin). Its PATH-LESS reject shape is deliberately distinct
|
|
28
|
+
// from Step 2's path-ful `dialectError` tail below — see `addImport`'s own JSDoc.
|
|
29
|
+
import { SyntaxKind } from "ts-morph";
|
|
30
|
+
import { dialectError } from "../../core/dialect-error.js";
|
|
31
|
+
import { handlePathFor } from "../../core/dialect-handle.js";
|
|
32
|
+
import { elementNameEcho } from "../../core/reject-tail.js";
|
|
33
|
+
// A misnomer surviving from its JSX-only origin (`jsx-name-validator.ts`) — this dialect is
|
|
34
|
+
// its first non-JSX consumer, sharpening ARCH-2's now-realised "one consumer" debt (ADR-02).
|
|
35
|
+
import { assertValidImportBinding } from "../../core/jsx-name-validator.js";
|
|
36
|
+
// Collects every local name a single declaration binds, across all three clause shapes at
|
|
37
|
+
// once (a declaration can carry a default AND a named clause simultaneously, e.g.
|
|
38
|
+
// `import Def, { B } from "m"`). Set-key-safety clause: names are compared via
|
|
39
|
+
// `.includes()`/`===` on plain arrays, NEVER used as an object/Map key.
|
|
40
|
+
function boundNamesIn(decl) {
|
|
41
|
+
const declTypeOnly = decl.isTypeOnly();
|
|
42
|
+
const names = [];
|
|
43
|
+
const defaultImport = decl.getDefaultImport();
|
|
44
|
+
if (defaultImport !== undefined) {
|
|
45
|
+
names.push({
|
|
46
|
+
localName: defaultImport.getText(),
|
|
47
|
+
kind: "default",
|
|
48
|
+
aliased: false,
|
|
49
|
+
valueBound: !declTypeOnly,
|
|
50
|
+
selfAliased: false,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
const namespaceImport = decl.getNamespaceImport();
|
|
54
|
+
if (namespaceImport !== undefined) {
|
|
55
|
+
names.push({
|
|
56
|
+
localName: namespaceImport.getText(),
|
|
57
|
+
kind: "namespace",
|
|
58
|
+
aliased: false,
|
|
59
|
+
valueBound: !declTypeOnly,
|
|
60
|
+
selfAliased: false,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
for (const specifier of decl.getNamedImports()) {
|
|
64
|
+
const alias = specifier.getAliasNode();
|
|
65
|
+
names.push({
|
|
66
|
+
localName: alias !== undefined ? alias.getText() : specifier.getName(),
|
|
67
|
+
kind: "named",
|
|
68
|
+
aliased: alias !== undefined,
|
|
69
|
+
valueBound: !declTypeOnly && !specifier.isTypeOnly(),
|
|
70
|
+
selfAliased: alias !== undefined && alias.getText() === specifier.getName(),
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return names;
|
|
74
|
+
}
|
|
75
|
+
// Step 1's idempotency-satisfying shape: a value-bound default specifier, a value-bound
|
|
76
|
+
// namespace specifier, a value-bound UNALIASED named specifier, or a value-bound
|
|
77
|
+
// SELF-ALIASED named specifier (`{ X as X }`). An aliased-to-a-DIFFERENT-name named
|
|
78
|
+
// specifier's local name identifies a DIFFERENT export and a type-only binding at either
|
|
79
|
+
// granularity does not satisfy the value-binding promise — neither counts here.
|
|
80
|
+
//
|
|
81
|
+
// DELIBERATE DEVIATION (S-003, REQ-TSD-01.15, owner-ratified): self-alias satisfying
|
|
82
|
+
// idempotency diverges from this predicate's twin in `react/ops.ts` (which rejects it) —
|
|
83
|
+
// FIT-41 pins the divergence as a POSITIVE expected-mismatch row, never a row exclusion.
|
|
84
|
+
function satisfiesIdempotency(bound) {
|
|
85
|
+
if (!bound.valueBound)
|
|
86
|
+
return false;
|
|
87
|
+
return bound.kind !== "named" || !bound.aliased || bound.selfAliased;
|
|
88
|
+
}
|
|
89
|
+
// A valid MERGE target (Step 3) is a declaration that (a) is NOT type-only and (b) has a
|
|
90
|
+
// `NamedImports` clause node — whether or not it already carries specifiers. A default-only
|
|
91
|
+
// or namespace-only declaration has no `NamedImports` node at all: TS/JS syntax forbids
|
|
92
|
+
// grafting a named clause onto a namespace import, and grafting one onto a default-only
|
|
93
|
+
// declaration would silently widen an existing statement the author did not ask this op to
|
|
94
|
+
// touch — so neither is a merge target, matching a FRESH separate declaration instead
|
|
95
|
+
// (Step 4).
|
|
96
|
+
function isNonTypeOnlyNamedImportClause(decl) {
|
|
97
|
+
if (decl.isTypeOnly())
|
|
98
|
+
return false;
|
|
99
|
+
const namedBindings = decl.getImportClause()?.getNamedBindings();
|
|
100
|
+
return namedBindings !== undefined && namedBindings.getKind() === SyntaxKind.NamedImports;
|
|
101
|
+
}
|
|
102
|
+
// ADR-0039 collision-namespace pin (owner ruling #4), EXTRACTED (S-001, design §4.5 ADR-01)
|
|
103
|
+
// from `assertNoCollision` below so both it and `addImport`'s Step 2 share ONE predicate: a
|
|
104
|
+
// top-level VALUE-NAMESPACE declaration (function/const/let/var/class/enum/namespace) under
|
|
105
|
+
// `name` collides; `type`/`interface` are exempt (legal TS coexistence). Syntactic search
|
|
106
|
+
// only (no language service, REQ-TSD-02.3) — SourceFile-wide, not scope-aware.
|
|
107
|
+
function isValueNamespaceClaimed(ast, name) {
|
|
108
|
+
return (ast.getFunction(name) !== undefined ||
|
|
109
|
+
ast.getVariableDeclaration(name) !== undefined ||
|
|
110
|
+
ast.getClass(name) !== undefined ||
|
|
111
|
+
ast.getEnum(name) !== undefined ||
|
|
112
|
+
ast.getModule(name) !== undefined);
|
|
113
|
+
}
|
|
114
|
+
// S-003, REQ-TSD-01.21: counts the file's LEADING directive prologue — string-literal
|
|
115
|
+
// `ExpressionStatement`s starting at statement 0, stopping at the first non-directive
|
|
116
|
+
// statement (a directive is only live when first-in-scope; a string literal appearing
|
|
117
|
+
// later in the file is an ordinary expression statement, not a directive). A shebang is
|
|
118
|
+
// SourceFile leading trivia, never a statement, so it never contributes here — the Create
|
|
119
|
+
// branch below falls through to its pre-existing `addImportDeclaration` path for a shebang
|
|
120
|
+
// file, leaving REQ-TSD-01.33's fail-closed containment untouched (ADR-03).
|
|
121
|
+
function leadingDirectiveCount(ast) {
|
|
122
|
+
let count = 0;
|
|
123
|
+
for (const statement of ast.getStatements()) {
|
|
124
|
+
if (statement.getKind() !== SyntaxKind.ExpressionStatement)
|
|
125
|
+
break;
|
|
126
|
+
const expression = statement.asKindOrThrow(SyntaxKind.ExpressionStatement).getExpression();
|
|
127
|
+
if (expression.getKind() !== SyntaxKind.StringLiteral)
|
|
128
|
+
break;
|
|
129
|
+
count++;
|
|
130
|
+
}
|
|
131
|
+
return count;
|
|
132
|
+
}
|
|
133
|
+
// Judgment-day-confirmed WARNING fix: `SourceFile#insertImportDeclaration`'s `index` argument
|
|
134
|
+
// is NOT a statement index — ts-morph's `_insertChildren` resolves it against
|
|
135
|
+
// `getStatementsWithComments()`, which counts leading COMMENT TRIVIA as its own entries ahead
|
|
136
|
+
// of the statements they precede. Passing `leadingDirectiveCount` (a statement-only count, no
|
|
137
|
+
// comments) straight through as that index silently lands the import BETWEEN a leading comment
|
|
138
|
+
// and the directive it precedes (`/* lic */` + `"use client";` -> `/* lic */` + new import +
|
|
139
|
+
// `"use client";`), demoting the directive off statement position 0 and voiding it — a license
|
|
140
|
+
// header above a directive is an ordinary file shape, not an edge case. This locates the LAST
|
|
141
|
+
// directive statement's own position in the comment-inclusive list and inserts immediately
|
|
142
|
+
// after it, so a comment stays above the directive it was already above, and the directive
|
|
143
|
+
// stays the file's first real statement either way.
|
|
144
|
+
function directiveInsertIndex(ast, directiveCount) {
|
|
145
|
+
const lastDirective = ast.getStatements()[directiveCount - 1];
|
|
146
|
+
return ast.getStatementsWithComments().findIndex((statement) => statement === lastDirective) + 1;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Adds `import { name } from "from";`, or merges `name` into an EXISTING NAMED-import
|
|
150
|
+
* clause from the SAME module `from` if one already exists (REQ-TSD-01). `name` is
|
|
151
|
+
* validated at the op boundary BEFORE any of the four branches below ever run (REQ-TSD-13)
|
|
152
|
+
* — see the trust-boundary note at the end of this doc.
|
|
153
|
+
*
|
|
154
|
+
* 0. Validation (REQ-TSD-13, runs FIRST, strictly before Step 1): `name` MUST be a valid
|
|
155
|
+
* plain-JS identifier, MUST NOT be a reserved or strict-mode-restricted word, and MUST
|
|
156
|
+
* NOT be a denylisted name (`assertValidImportBinding`, reused verbatim from
|
|
157
|
+
* `src/core/jsx-name-validator.ts`) — REJECTS via a PATH-LESS `dialectError` before Step
|
|
158
|
+
* 1/2/3/4 ever evaluate `name`, even when `name` would ALSO collide under Step 2
|
|
159
|
+
* (REQ-TSD-13.6, precedence pinned: validation wins, never the collision message).
|
|
160
|
+
* 1. Already-bound (idempotency, SAME module only): if an EXISTING declaration for `from`
|
|
161
|
+
* has a VALUE-BOUND specifier whose local name equals `name`, AND that specifier is a
|
|
162
|
+
* default specifier, a namespace specifier, an UNALIASED named specifier, or a
|
|
163
|
+
* SELF-ALIASED named specifier (`{ name as name }`) — this is a no-op. The self-aliased
|
|
164
|
+
* case is a DELIBERATE DEVIATION from the `react` dialect's twin predicate, which rejects
|
|
165
|
+
* it (REQ-TSD-01.15, owner-ratified). Runs strictly BEFORE Step 2 (REQ-TSD-01.24,
|
|
166
|
+
* mutant-kill ordering invariant): a same-module already-bound specifier is a no-op,
|
|
167
|
+
* NEVER a collision reject.
|
|
168
|
+
* 2. Collision: otherwise, if `name` is CLAIMED anywhere in the file — EITHER the local name
|
|
169
|
+
* of ANY import specifier, in ANY module, value-bound or type-only alike, OR a top-level
|
|
170
|
+
* value-namespace declaration (`isValueNamespaceClaimed`) — REJECT via `dialectError`
|
|
171
|
+
* BEFORE any AST mutation.
|
|
172
|
+
* 3. Merge: otherwise, if an EXISTING declaration for `from` has a non-type-only
|
|
173
|
+
* named-import clause — INCLUDING an empty one (`import {} from "from"`) — add a NEW,
|
|
174
|
+
* UNALIASED named specifier to it. Walks EVERY declaration for `from` (a module can be
|
|
175
|
+
* imported via several separate declarations) and merges into the FIRST one in source
|
|
176
|
+
* order (REQ-TSD-01.22/.25 — a known, deliberate first-match asymmetry; contrast
|
|
177
|
+
* `removeImport`, which also SEARCHES every declaration matching `from` but REMOVES the
|
|
178
|
+
* binding from only the first one it finds it in, then returns — see `removeImport`'s own
|
|
179
|
+
* JSDoc). A side-effect-only import (`import "from";`, no
|
|
180
|
+
* clause at all) is never a merge target and is never converted into a combined form
|
|
181
|
+
* (REQ-TSD-01.20, Class B) — it is left byte-unchanged, coexisting beside the fresh
|
|
182
|
+
* declaration Step 4 inserts. (Reaching this step guarantees `name` is claimed nowhere in
|
|
183
|
+
* the file, so the merge is always safe.)
|
|
184
|
+
* 4. Create: otherwise (no declaration for `from`; every one is type-only, default-only,
|
|
185
|
+
* namespace-only, or side-effect-only), INSERT a FRESH, SEPARATE
|
|
186
|
+
* `import { name } from "from";` — a type-only, default-only, or namespace-only
|
|
187
|
+
* declaration's clause structure is NEVER mutated to graft a named binding onto it. A
|
|
188
|
+
* leading directive prologue (`"use client";` and similar) is preserved as the file's
|
|
189
|
+
* first statement(s) — the fresh declaration is inserted AFTER it, never above
|
|
190
|
+
* (REQ-TSD-01.21). (Reaching this step also guarantees `name` is claimed nowhere in the
|
|
191
|
+
* file, so the fresh declaration never collides.)
|
|
192
|
+
*
|
|
193
|
+
* Idempotent: calling this twice with the same `name`+`from` produces a single import line,
|
|
194
|
+
* never a duplicate (REQ-TSD-03.10).
|
|
195
|
+
*
|
|
196
|
+
* Trust boundary (REQ-TSD-13.5): `name` and `from` are the ONLY two channels this op
|
|
197
|
+
* validates. `addFunction`/`addVariable`/`addClass`'s own `name`/`source`/`initializer`
|
|
198
|
+
* arguments remain RAW-SPLICED, author-trusted input this change does NOT validate or
|
|
199
|
+
* protect — that gap is tracked separately in `project/pending-changes`, out of this
|
|
200
|
+
* change's scope.
|
|
201
|
+
*/
|
|
202
|
+
export function addImport(ast, name, from) {
|
|
203
|
+
// REQ-TSD-13 validation gate: a deliberately PATH-LESS reject (contrast Step 2's path-ful
|
|
204
|
+
// `dialectError` below) — runs first, so a `name` that is BOTH invalid AND claimed rejects
|
|
205
|
+
// via validation, never collision (REQ-TSD-13.6).
|
|
206
|
+
assertValidImportBinding(name);
|
|
207
|
+
// Single pass: one `getImportDeclarations()` call, one `boundNamesIn` per declaration —
|
|
208
|
+
// Step 1 and Step 2 below both read from `declarationBoundNames` instead of each re-walking
|
|
209
|
+
// the AST. Step 1 still fully evaluates (and early-returns) before Step 2 ever runs
|
|
210
|
+
// (REQ-TSD-01.24/REQ-TSD-13.6 ordering pin) — this is code motion only, not reordering.
|
|
211
|
+
const allDeclarations = ast.getImportDeclarations();
|
|
212
|
+
const declarationsForModule = allDeclarations.filter((decl) => decl.getModuleSpecifierValue() === from);
|
|
213
|
+
const declarationBoundNames = allDeclarations.map((decl) => [
|
|
214
|
+
decl,
|
|
215
|
+
boundNamesIn(decl),
|
|
216
|
+
]);
|
|
217
|
+
const alreadyBound = declarationBoundNames.some(([decl, bound]) => decl.getModuleSpecifierValue() === from &&
|
|
218
|
+
bound.some((b) => b.localName === name && satisfiesIdempotency(b)));
|
|
219
|
+
if (alreadyBound)
|
|
220
|
+
return;
|
|
221
|
+
const claimed = declarationBoundNames.some(([, bound]) => bound.some((b) => b.localName === name)) ||
|
|
222
|
+
isValueNamespaceClaimed(ast, name);
|
|
223
|
+
if (claimed) {
|
|
224
|
+
// `name` is POST-VALIDATION here (Step 0 already ran) but its grammar has no length
|
|
225
|
+
// ceiling — echoed through `elementNameEcho` (the wide, marked 100-char bound shared with
|
|
226
|
+
// `setJsxProp`'s reject tails), never left raw, so a pathological-length name can't blow up
|
|
227
|
+
// this message (REQ-TSD-01.32 pins realistic names, e.g. 29 chars, staying UNCUT).
|
|
228
|
+
const echoedName = elementNameEcho(name);
|
|
229
|
+
throw dialectError(`addImport("${echoedName}") on "${handlePathFor(ast)}" — a value or import binding named "${echoedName}" already exists; ` +
|
|
230
|
+
`TypeScript forbids two bindings sharing a name. Rename or remove the existing one, or edit it with .modify().`);
|
|
231
|
+
}
|
|
232
|
+
const mergeTarget = declarationsForModule.find(isNonTypeOnlyNamedImportClause);
|
|
233
|
+
if (mergeTarget !== undefined) {
|
|
234
|
+
mergeTarget.addNamedImport(name);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
const directiveCount = leadingDirectiveCount(ast);
|
|
238
|
+
if (directiveCount === 0) {
|
|
239
|
+
ast.addImportDeclaration({ moduleSpecifier: from, namedImports: [name] });
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
ast.insertImportDeclaration(directiveInsertIndex(ast, directiveCount), {
|
|
243
|
+
moduleSpecifier: from,
|
|
244
|
+
namedImports: [name],
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
function exportPrefixFor(options) {
|
|
249
|
+
return options?.export === true ? "export " : "";
|
|
250
|
+
}
|
|
251
|
+
// ADR-0039 collision-namespace pin (owner ruling #4): a VALUE-namespace declaration
|
|
252
|
+
// (`isValueNamespaceClaimed`, S-001-extracted, shared with `addImport`'s Step 2 above) OR a
|
|
253
|
+
// named-import binding under `name` collides, cross-kind; `type`/`interface` are exempt
|
|
254
|
+
// (legal TS coexistence). Syntactic search only (no language service, per REQ-TSD-02.3's own
|
|
255
|
+
// no-LS commitment) — SourceFile-wide, not scope-aware (matches the "no language service
|
|
256
|
+
// needed" owner framing; every signed scenario seeds top-level declarations only).
|
|
257
|
+
//
|
|
258
|
+
// Behaviour-preservation (S-001, ops-declarations.test.ts guard): the import-binding half of
|
|
259
|
+
// this predicate stays NAMED-imports-only, UNCHANGED by the extraction — `addFunction`/
|
|
260
|
+
// `addVariable`/`addClass` stay blind to a default/namespace import collision, same as
|
|
261
|
+
// before (design §4.8's registered "sibling collision-scan asymmetry" followup, not closed
|
|
262
|
+
// by this change).
|
|
263
|
+
//
|
|
264
|
+
// `handlePathFor(ast)` (final-verify fix, dialect-handle.ts) is the ONLY channel through
|
|
265
|
+
// which an op function can recover the author-facing path for its own dialectError()-branded
|
|
266
|
+
// message (design §4.4's pinned "on {path}" clause) — position-independent (keyed by the
|
|
267
|
+
// live AST instance, not a runtime argument position), so it can never collide with `name`/
|
|
268
|
+
// `options` regardless of which trailing args the author omitted.
|
|
269
|
+
function assertNoCollision(ast, name, opName) {
|
|
270
|
+
const collides = isValueNamespaceClaimed(ast, name) ||
|
|
271
|
+
ast
|
|
272
|
+
.getImportDeclarations()
|
|
273
|
+
.some((decl) => decl.getNamedImports().some((named) => (named.getAliasNode()?.getText() ?? named.getName()) === name));
|
|
274
|
+
if (!collides)
|
|
275
|
+
return;
|
|
276
|
+
throw dialectError(`${opName}("${name}") on "${handlePathFor(ast)}" — a value or import binding named "${name}" already exists; ` +
|
|
277
|
+
`TypeScript forbids two value declarations sharing a name. Rename or remove the existing one, or edit it with .modify().`);
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* Inserts a new top-level function declaration `{export }function {name}{source}` at the
|
|
281
|
+
* end of the file, in call order (ts-morph `addStatements`, REQ-TSD-09). Rejects with a
|
|
282
|
+
* pinned `dialectError` when a value-namespace declaration or import binding already
|
|
283
|
+
* exists under `name` (ADR-0039) — `type`/`interface` are exempt.
|
|
284
|
+
*
|
|
285
|
+
* `source` INCLUDES the function's `{ … }` braces — contrast `addClass`, whose `source`
|
|
286
|
+
* EXCLUDES them (the op supplies the braces itself).
|
|
287
|
+
*
|
|
288
|
+
* @example
|
|
289
|
+
* // source includes braces:
|
|
290
|
+
* ts.find(path).addFunction("hi", "(): void {}"); // -> function hi(): void {}
|
|
291
|
+
* // contrast addClass, whose source EXCLUDES braces (the op adds them).
|
|
292
|
+
*/
|
|
293
|
+
export function addFunction(ast, name, source, options) {
|
|
294
|
+
assertNoCollision(ast, name, "addFunction");
|
|
295
|
+
ast.addStatements(`${exportPrefixFor(options)}function ${name}${source}`);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Inserts a new top-level variable declaration `{export }{kind} {name} = {initializer};` at
|
|
299
|
+
* the end of the file, in call order (ts-morph `addStatements`, REQ-TSD-10). `kind` defaults
|
|
300
|
+
* to `"const"`. Rejects with a pinned `dialectError` under the SAME collision-namespace rule
|
|
301
|
+
* as `addFunction` (ADR-0039) — `type`/`interface` are exempt.
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* ts.find(path).addVariable("PI", "3.14159"); // -> const PI = 3.14159;
|
|
305
|
+
* ts.find(path).addVariable("counter", "0", { export: true, kind: "let" }); // -> export let counter = 0;
|
|
306
|
+
*/
|
|
307
|
+
export function addVariable(ast, name, initializer, options) {
|
|
308
|
+
assertNoCollision(ast, name, "addVariable");
|
|
309
|
+
const kind = options?.kind ?? "const";
|
|
310
|
+
ast.addStatements(`${exportPrefixFor(options)}${kind} ${name} = ${initializer};`);
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Inserts a new top-level class declaration `{export }class {name} {\n{source}\n}` at the
|
|
314
|
+
* end of the file, in call order (ts-morph `addStatements`, REQ-TSD-11). Rejects with a
|
|
315
|
+
* pinned `dialectError` under the SAME collision-namespace rule as `addFunction` (ADR-0039)
|
|
316
|
+
* — `type`/`interface` are exempt.
|
|
317
|
+
*
|
|
318
|
+
* `source` EXCLUDES the class's `{ … }` braces — contrast `addFunction`, whose `source`
|
|
319
|
+
* INCLUDES them (that op supplies no braces of its own; this one does).
|
|
320
|
+
*
|
|
321
|
+
* @example
|
|
322
|
+
* // source excludes braces:
|
|
323
|
+
* ts.find(path).addClass("Point", " x = 0;"); // -> class Point {\n x = 0;\n}
|
|
324
|
+
* // contrast addFunction, whose source INCLUDES braces (the op does NOT add them itself).
|
|
325
|
+
*/
|
|
326
|
+
export function addClass(ast, name, source, options) {
|
|
327
|
+
assertNoCollision(ast, name, "addClass");
|
|
328
|
+
ast.addStatements(`${exportPrefixFor(options)}class ${name} {\n${source}\n}`);
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* Removes the named binding from `from`'s import clause (REQ-TSD-08). Idempotent: an
|
|
332
|
+
* absent binding is a no-op. Removing the LAST named binding deletes the entire import
|
|
333
|
+
* statement — no dangling `import {} from "from"`. Matched by the module-EXPORTED name,
|
|
334
|
+
* not the local alias (`{ readFileSync as rf }` is matched by `"readFileSync"`). Scope:
|
|
335
|
+
* NAMED-binding imports only — default and namespace imports are untouched, and a
|
|
336
|
+
* declaration mixing a default/namespace import alongside named bindings is never
|
|
337
|
+
* whole-statement-deleted (only the matched named specifier is removed).
|
|
338
|
+
*
|
|
339
|
+
* Judgment-day round 1 (Issue 2): a module can be imported via SEVERAL separate
|
|
340
|
+
* declarations (`import { a } from "m"; import { b } from "m";`, legal TS) — this SEARCHES
|
|
341
|
+
* every declaration matching `from`, not just the first, to locate whichever one actually
|
|
342
|
+
* contains `name`. Once found, the binding is removed from that ONE declaration and the
|
|
343
|
+
* function returns — a legal file never binds the same local name twice, so this is
|
|
344
|
+
* observationally a full removal on any real input; on the illegal, hand-authored fixture
|
|
345
|
+
* where `name` is duplicated across two declarations for `from` (REQ-TSD-01.25, CORRECTED
|
|
346
|
+
* V3.2), only the FIRST one loses the binding — the search is all-declarations, the removal
|
|
347
|
+
* is first-match-only. Sibling declarations from the same module are never touched.
|
|
348
|
+
*/
|
|
349
|
+
export function removeImport(ast, name, from) {
|
|
350
|
+
const decls = ast.getImportDeclarations().filter((d) => d.getModuleSpecifierValue() === from);
|
|
351
|
+
for (const decl of decls) {
|
|
352
|
+
const namedImports = decl.getNamedImports();
|
|
353
|
+
const specifier = namedImports.find((named) => named.getName() === name);
|
|
354
|
+
if (specifier === undefined)
|
|
355
|
+
continue;
|
|
356
|
+
const isLastNamedBindingOnly = namedImports.length === 1 && decl.getDefaultImport() === undefined && decl.getNamespaceImport() === undefined;
|
|
357
|
+
if (isLastNamedBindingOnly) {
|
|
358
|
+
decl.remove();
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
specifier.remove();
|
|
362
|
+
}
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { dryRunPlan } from "./plan.js";
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { Directive } from "../core/wire.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Author-facing verb vocabulary for a dry-run plan entry.
|
|
4
|
+
*
|
|
5
|
+
* Frozen at exactly these seven members — adding a member is a MAJOR event (ADR-0025).
|
|
6
|
+
* `schematic-local-files` S-003 (ADR-0025 amendment): grew from six to seven — `copyIn`
|
|
7
|
+
* added, 1:1 with the `copyIn` wire op (ADR-0043).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* function label(verb: DryRunVerb): string {
|
|
11
|
+
* switch (verb) {
|
|
12
|
+
* case "create": return "creates";
|
|
13
|
+
* case "modify": return "modifies";
|
|
14
|
+
* case "remove": return "removes";
|
|
15
|
+
* case "rename": return "renames";
|
|
16
|
+
* case "move": return "moves";
|
|
17
|
+
* case "copy": return "copies";
|
|
18
|
+
* case "copyIn": return "copies in";
|
|
19
|
+
* default: {
|
|
20
|
+
* const exhaustive: never = verb;
|
|
21
|
+
* return exhaustive;
|
|
22
|
+
* }
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
*/
|
|
26
|
+
export type DryRunVerb = "create" | "modify" | "remove" | "rename" | "move" | "copy" | "copyIn";
|
|
27
|
+
/**
|
|
28
|
+
* One entry of a rendered dry-run plan: the author verb and the primary path the
|
|
29
|
+
* operation targets. Carries no content or byte preview.
|
|
30
|
+
*
|
|
31
|
+
* `kind` is present ONLY on the package-local-READ verbs whose transport is CLASSIFIED
|
|
32
|
+
* (REQ-DRE-05, additive field — the existing `{verb, path}` shape is unchanged):
|
|
33
|
+
* `"rendered"` for `create` (every `create` IS a render request, whether the template
|
|
34
|
+
* came from an inline string or a `templateFile` — indistinguishable in kind,
|
|
35
|
+
* REQ-DRE-05.3), `"copied"` for a `copyIn` entry (REQ-DRE-05.2) — including a `scaffold`
|
|
36
|
+
* entry that classified by-reference and surfaces under author verb `"copyIn"` even
|
|
37
|
+
* though the author never called `copyIn` directly (REQ-DRE-06). Derived purely from the
|
|
38
|
+
* wire op — total, deterministic — and ABSENT for `modify`/`remove`/`rename`/`move`/
|
|
39
|
+
* `copy`: the tree→tree `copy` verb predates this axis and materializes content too, but
|
|
40
|
+
* it is never package-local-read/classified, so it carries no `kind` (tagging a plain
|
|
41
|
+
* `remove` "rendered" would be a lie, but `copy`'s absence is a scope boundary, not a lie
|
|
42
|
+
* about its own behavior).
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* const plan = dryRun(); // from "@pbuilder/sdk/commons"
|
|
46
|
+
* for (const entry of plan) {
|
|
47
|
+
* console.log(entry.verb, entry.path, entry.kind);
|
|
48
|
+
* }
|
|
49
|
+
*/
|
|
50
|
+
export interface DryRunEntry {
|
|
51
|
+
verb: DryRunVerb;
|
|
52
|
+
path: string;
|
|
53
|
+
kind?: "rendered" | "copied";
|
|
54
|
+
}
|
|
55
|
+
export declare const WIRE_TO_AUTHOR_VERB: Readonly<Record<Directive["op"], DryRunVerb>>;
|
|
56
|
+
/**
|
|
57
|
+
* Renders a directive snapshot as an author-vocabulary plan.
|
|
58
|
+
*
|
|
59
|
+
* Each entry carries the author verb (per the frozen `WIRE_TO_AUTHOR_VERB` table above)
|
|
60
|
+
* and the primary source path of the operation:
|
|
61
|
+
* create → create.pathTemplate
|
|
62
|
+
* modify → modify.path
|
|
63
|
+
* delete → delete.path (rendered as author verb "remove")
|
|
64
|
+
* rename → rename.path
|
|
65
|
+
* move → move.path
|
|
66
|
+
* copy → copy.from (primary source path)
|
|
67
|
+
* copyIn → copyIn.from (primary source path, REQ-BRC-07 package-relative)
|
|
68
|
+
*/
|
|
69
|
+
export declare function dryRunPlan(snapshot: readonly Directive[]): DryRunEntry[];
|