@pbuilder/sdk 0.16.0 → 0.18.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 +75 -1
- package/dist/bin/pbuilder-conformance.d.ts +2 -0
- package/dist/bin/pbuilder-conformance.js +26 -0
- package/dist/bounded-contexts/authoring/application/usecases/run-factory/context.d.ts +7 -1
- package/dist/bounded-contexts/authoring/application/usecases/run-factory/context.js +2 -1
- package/dist/bounded-contexts/authoring/application/usecases/scaffold/expander.js +4 -4
- package/dist/bounded-contexts/authoring/domain/aggregates/session/session.d.ts +1 -0
- package/dist/bounded-contexts/authoring/domain/aggregates/session/session.js +54 -4
- package/dist/bounded-contexts/authoring/domain/entities/dialect-handle.d.ts +2 -1
- package/dist/bounded-contexts/authoring/domain/entities/dialect-handle.js +108 -30
- package/dist/bounded-contexts/authoring/domain/services/define-dialect.d.ts +48 -10
- package/dist/bounded-contexts/authoring/domain/services/define-dialect.js +37 -9
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/angular/index.js +2 -2
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/aspnet/index.d.ts +51 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/aspnet/index.js +49 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/aspnet/ops.d.ts +11 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/aspnet/ops.js +139 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/ast.d.ts +29 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/ast.js +41 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/index.d.ts +44 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/index.js +45 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/names.d.ts +7 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/names.js +72 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/ops.d.ts +19 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/ops.js +255 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/pack.d.ts +9 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/pack.js +2 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/syntax.d.ts +26 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/syntax.js +111 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/tree-sitter-c_sharp.LICENSE +23 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/csharp/tree-sitter-c_sharp.wasm +0 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/ast.d.ts +22 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/ast.js +17 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/index.d.ts +55 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/index.js +54 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/ops.d.ts +7 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/ops.js +162 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/xml.d.ts +20 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/msbuild/xml.js +50 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/nestjs/index.js +2 -2
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/ast.d.ts +2 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/ast.js +4 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/react/index.js +3 -3
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/ast.d.ts +2 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/ast.js +4 -0
- package/dist/bounded-contexts/authoring/infrastructure/out/dialects/typescript/index.js +2 -2
- package/dist/bounded-contexts/authoring/testing/conformance/index.d.ts +10 -12
- package/dist/bounded-contexts/authoring/testing/conformance/index.js +17 -2
- package/dist/bounded-contexts/authoring/testing/conformance/run-package.d.ts +20 -0
- package/dist/bounded-contexts/authoring/testing/conformance/run-package.js +129 -0
- package/dist/bounded-contexts/authoring/testing/harness/index.d.ts +3 -0
- package/dist/bounded-contexts/authoring/testing/harness/index.js +5 -1
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/bootstrap-bridge.d.ts +3 -0
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/bootstrap-bridge.js +2 -0
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/runner.js +42 -3
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/single-instance-probe.d.ts +13 -0
- package/dist/bounded-contexts/runner-hosting/infrastructure/in/single-instance-probe.js +25 -1
- package/dist/conformance/index.d.ts +1 -0
- package/dist/conformance/index.js +1 -0
- package/dist/dialect/index.d.ts +2 -0
- package/dist/dialect/index.js +3 -0
- package/dist/dialects/aspnet/index.d.ts +1 -0
- package/dist/dialects/aspnet/index.js +1 -0
- package/dist/dialects/csharp/index.d.ts +1 -0
- package/dist/dialects/csharp/index.js +1 -0
- package/dist/dialects/msbuild/index.d.ts +1 -0
- package/dist/dialects/msbuild/index.js +1 -0
- package/dist/runner-manifest.json +10 -6
- package/package.json +27 -3
|
@@ -6,12 +6,24 @@ export type Query<Ast = any, A extends unknown[] = any[], R = unknown> = ((ast:
|
|
|
6
6
|
readonly [QUERY]: true;
|
|
7
7
|
};
|
|
8
8
|
export type OpPack<Ast = any> = Record<string, Op<Ast>>;
|
|
9
|
+
/**
|
|
10
|
+
* How a dialect reads and writes its files. `retire`, when given, is called on a tree a handle
|
|
11
|
+
* re-parsed away from (A-8): it must make every node of that tree fail when used, so a node kept
|
|
12
|
+
* from before the re-parse never edits a tree no file reflects.
|
|
13
|
+
*/
|
|
14
|
+
export interface DialectAst<Ast = any> {
|
|
15
|
+
parse(source: string): Ast;
|
|
16
|
+
print(ast: Ast): string;
|
|
17
|
+
retire?(ast: Ast): void;
|
|
18
|
+
}
|
|
9
19
|
export interface DialectDescriptor<Ast = any, Ops extends OpPack<Ast> = OpPack<Ast>> {
|
|
20
|
+
/**
|
|
21
|
+
* The name of the package that ships an author-defined dialect, the name its Dialect
|
|
22
|
+
* declaration uses (A-16). The SDK's dialects carry none and are never declared.
|
|
23
|
+
*/
|
|
24
|
+
name?: string;
|
|
10
25
|
extensions: string[];
|
|
11
|
-
ast:
|
|
12
|
-
parse(source: string): Ast;
|
|
13
|
-
print(ast: Ast): string;
|
|
14
|
-
};
|
|
26
|
+
ast: DialectAst<Ast>;
|
|
15
27
|
ops: Ops;
|
|
16
28
|
}
|
|
17
29
|
type Bound<Ast, F> = F extends (ast: Ast, ...rest: infer R) => void ? (...rest: R) => void : never;
|
|
@@ -33,15 +45,24 @@ type DialectWriteOps<Ast, Ops extends OpPack<Ast>> = {
|
|
|
33
45
|
};
|
|
34
46
|
export type Handle<State extends "found" | "writable" = "found", Ast = any, Ops extends OpPack<Ast> = OpPack<Ast>> = ReadOps & DialectWriteOps<Ast, Ops> & {
|
|
35
47
|
modify(fn: (ast: Ast) => void): Edited<Ast, Ops>;
|
|
48
|
+
/**
|
|
49
|
+
* The read escape hatch, as `.modify()` is the write one (A-128). Hands `fn` the live AST,
|
|
50
|
+
* after every op chained before it, and resolves to what `fn` returns; it ends the chain.
|
|
51
|
+
* The AST is printed before and after `fn`, and a difference fails the factory with
|
|
52
|
+
* `inspect() must not change "<path>"`. Return plain data: a node kept after the next edit
|
|
53
|
+
* is stale (A-8).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const count = await typescript.find("src/a.ts").addImport("x", "y").inspect((ast) => ast.getImportDeclarations().length);
|
|
57
|
+
*/
|
|
58
|
+
inspect<R>(fn: (ast: Ast) => R): Promise<Awaited<R>>;
|
|
36
59
|
} & OpMethods<Ast, Ops> & (State extends "found" ? {
|
|
37
60
|
remove(): void;
|
|
38
61
|
} : {}) & PromiseLike<void>;
|
|
39
62
|
export interface Dialect<Ast = any, Ops extends OpPack<Ast> = OpPack<Ast>> {
|
|
63
|
+
readonly name?: string;
|
|
40
64
|
readonly extensions: readonly string[];
|
|
41
|
-
readonly ast:
|
|
42
|
-
parse(source: string): Ast;
|
|
43
|
-
print(ast: Ast): string;
|
|
44
|
-
};
|
|
65
|
+
readonly ast: DialectAst<Ast>;
|
|
45
66
|
readonly ops: Ops;
|
|
46
67
|
find(path: string): Handle<"found", Ast, Ops>;
|
|
47
68
|
}
|
|
@@ -59,7 +80,8 @@ export declare function defineOpPack<Ast, Ops extends OpPack<Ast>>(ops: Ops): Op
|
|
|
59
80
|
/**
|
|
60
81
|
* Assembles a `Dialect` from its frozen descriptor (extensions, an AST parse/print pair,
|
|
61
82
|
* and an op-pack). `find` is the dialect's ONLY entry verb into a Session (REQ-DG-01.2) — it
|
|
62
|
-
* returns an open, coalescing `Handle`.
|
|
83
|
+
* returns an open, coalescing `Handle`. Throws when an op in `ops` takes the name of a handle
|
|
84
|
+
* verb (`RESERVED_HANDLE_NAMES`).
|
|
63
85
|
*
|
|
64
86
|
* @example
|
|
65
87
|
* const myDialect = defineDialect({
|
|
@@ -69,6 +91,22 @@ export declare function defineOpPack<Ast, Ops extends OpPack<Ast>>(ops: Ops): Op
|
|
|
69
91
|
* });
|
|
70
92
|
*/
|
|
71
93
|
export declare function defineDialect<Ast, Ops extends OpPack<Ast>>(d: DialectDescriptor<Ast, Ops>): Dialect<Ast, Ops>;
|
|
94
|
+
/**
|
|
95
|
+
* Defines an author-defined dialect, named by the package that ships it (A-16): a factory opens
|
|
96
|
+
* its handles only when the schematic's library declares that name in its Manifest, and the
|
|
97
|
+
* conformance fixture rejects a name that is not the package's.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* export const ini = defineDialect({
|
|
101
|
+
* name: "@company/ini-dialect",
|
|
102
|
+
* extensions: [".ini"],
|
|
103
|
+
* ast: { parse: (text) => parseIni(text), print: (tree) => printIni(tree) },
|
|
104
|
+
* ops: {},
|
|
105
|
+
* });
|
|
106
|
+
*/
|
|
107
|
+
export declare function defineAuthorDialect<Ast, Ops extends OpPack<Ast>>(d: DialectDescriptor<Ast, Ops> & {
|
|
108
|
+
name: string;
|
|
109
|
+
}): Dialect<Ast, Ops>;
|
|
72
110
|
type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
|
|
73
111
|
/**
|
|
74
112
|
* The base handle's own vocabulary (REQ-DG-02, ADR-0010 amendment) — an op-pack op sharing
|
|
@@ -82,7 +120,7 @@ type UnionToIntersection<U> = (U extends unknown ? (k: U) => void : never) exten
|
|
|
82
120
|
* import it instead of hand-typing a second literal) — NOT a supported public API member, not
|
|
83
121
|
* semver-covered beyond the FIT-04 baseline pin it already sits inside (ORCHESTRATOR RULING R2).
|
|
84
122
|
*/
|
|
85
|
-
export declare const RESERVED_HANDLE_NAMES: readonly ["then", "read", "raw", "modify", "replaceContent", "rename", "move", "copy", "remove"];
|
|
123
|
+
export declare const RESERVED_HANDLE_NAMES: readonly ["then", "read", "raw", "modify", "inspect", "replaceContent", "rename", "move", "copy", "remove"];
|
|
86
124
|
/**
|
|
87
125
|
* Composes additional op-packs onto a base dialect. The resulting handle type is the
|
|
88
126
|
* INTERSECTION of the base's ops and every attached pack's ops (ADR-0010) — an op from an
|
|
@@ -40,7 +40,8 @@ function hasDeclaredExtension(path, extensions) {
|
|
|
40
40
|
/**
|
|
41
41
|
* Assembles a `Dialect` from its frozen descriptor (extensions, an AST parse/print pair,
|
|
42
42
|
* and an op-pack). `find` is the dialect's ONLY entry verb into a Session (REQ-DG-01.2) — it
|
|
43
|
-
* returns an open, coalescing `Handle`.
|
|
43
|
+
* returns an open, coalescing `Handle`. Throws when an op in `ops` takes the name of a handle
|
|
44
|
+
* verb (`RESERVED_HANDLE_NAMES`).
|
|
44
45
|
*
|
|
45
46
|
* @example
|
|
46
47
|
* const myDialect = defineDialect({
|
|
@@ -50,7 +51,11 @@ function hasDeclaredExtension(path, extensions) {
|
|
|
50
51
|
* });
|
|
51
52
|
*/
|
|
52
53
|
export function defineDialect(d) {
|
|
54
|
+
// The handle spreads its ops over its verbs, so an op named after a verb would replace it.
|
|
55
|
+
for (const name of Object.keys(d.ops))
|
|
56
|
+
assertNotReserved(name);
|
|
53
57
|
return {
|
|
58
|
+
name: d.name,
|
|
54
59
|
extensions: d.extensions,
|
|
55
60
|
ast: d.ast,
|
|
56
61
|
ops: d.ops,
|
|
@@ -58,10 +63,29 @@ export function defineDialect(d) {
|
|
|
58
63
|
if (!hasDeclaredExtension(path, d.extensions)) {
|
|
59
64
|
throw dialectError(`find(${JSON.stringify(path)}) — the file's extension is not one this dialect declares (${d.extensions.join(", ")}).`);
|
|
60
65
|
}
|
|
61
|
-
return createDialectHandle(d.ast, d.ops, path);
|
|
66
|
+
return createDialectHandle(d.ast, d.ops, path, d.name);
|
|
62
67
|
},
|
|
63
68
|
};
|
|
64
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Defines an author-defined dialect, named by the package that ships it (A-16): a factory opens
|
|
72
|
+
* its handles only when the schematic's library declares that name in its Manifest, and the
|
|
73
|
+
* conformance fixture rejects a name that is not the package's.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* export const ini = defineDialect({
|
|
77
|
+
* name: "@company/ini-dialect",
|
|
78
|
+
* extensions: [".ini"],
|
|
79
|
+
* ast: { parse: (text) => parseIni(text), print: (tree) => printIni(tree) },
|
|
80
|
+
* ops: {},
|
|
81
|
+
* });
|
|
82
|
+
*/
|
|
83
|
+
export function defineAuthorDialect(d) {
|
|
84
|
+
if (typeof d.name !== "string" || d.name.length === 0) {
|
|
85
|
+
throw new Error('defineDialect failed: "name" is required, the name of the package that ships the dialect (A-16).');
|
|
86
|
+
}
|
|
87
|
+
return defineDialect(d);
|
|
88
|
+
}
|
|
65
89
|
/**
|
|
66
90
|
* The base handle's own vocabulary (REQ-DG-02, ADR-0010 amendment) — an op-pack op sharing
|
|
67
91
|
* any of these names would silently shadow a base verb; `then` in particular would break the
|
|
@@ -79,6 +103,7 @@ export const RESERVED_HANDLE_NAMES = [
|
|
|
79
103
|
"read",
|
|
80
104
|
"raw",
|
|
81
105
|
"modify",
|
|
106
|
+
"inspect",
|
|
82
107
|
"replaceContent",
|
|
83
108
|
"rename",
|
|
84
109
|
"move",
|
|
@@ -95,14 +120,17 @@ void _reservedCoversSurface;
|
|
|
95
120
|
* `Error`s, NOT minted via `dialectError()` — they are not run-op contained rejects and must
|
|
96
121
|
* never be recognised by `isContained` as passthrough (design §4.5's own rationale).
|
|
97
122
|
*/
|
|
123
|
+
function assertNotReserved(name) {
|
|
124
|
+
if (!RESERVED_SET.has(name))
|
|
125
|
+
return;
|
|
126
|
+
const hint = name === "raw" ? " — the live AST-fn escape hatch is .modify(fn)" : "";
|
|
127
|
+
throw new Error(`op-pack composition failed: op "${name}" collides with a reserved handle verb${hint}`);
|
|
128
|
+
}
|
|
98
129
|
function assertNoCompositionCollision(baseOpNames, packs) {
|
|
99
130
|
const claimed = new Set(baseOpNames);
|
|
100
131
|
for (const pack of packs) {
|
|
101
132
|
for (const name of Object.keys(pack)) {
|
|
102
|
-
|
|
103
|
-
const hint = name === "raw" ? " — the live AST-fn escape hatch is .modify(fn)" : "";
|
|
104
|
-
throw new Error(`op-pack composition failed: op "${name}" collides with a reserved handle verb${hint}`);
|
|
105
|
-
}
|
|
133
|
+
assertNotReserved(name);
|
|
106
134
|
if (claimed.has(name)) {
|
|
107
135
|
throw new Error(`op-pack composition failed: duplicate op "${name}" declared by more than one pack.`);
|
|
108
136
|
}
|
|
@@ -125,7 +153,7 @@ function assertNoCompositionCollision(baseOpNames, packs) {
|
|
|
125
153
|
export function withOps(base, ...packs) {
|
|
126
154
|
assertNoCompositionCollision(Object.keys(base.ops), packs);
|
|
127
155
|
const merged = Object.assign({}, base.ops, ...packs);
|
|
128
|
-
return defineDialect({ extensions: [...base.extensions], ast: base.ast, ops: merged });
|
|
156
|
+
return defineDialect({ name: base.name, extensions: [...base.extensions], ast: base.ast, ops: merged });
|
|
129
157
|
}
|
|
130
158
|
function assertKnownOps(ops, names, composer) {
|
|
131
159
|
for (const name of names) {
|
|
@@ -151,7 +179,7 @@ export function withoutOps(dialect, names, reason) {
|
|
|
151
179
|
throw dialectError(`${name} is not available for ${files} files — ${reason}. Use .modify() to make this edit directly.`);
|
|
152
180
|
},
|
|
153
181
|
]));
|
|
154
|
-
return defineDialect({ extensions: [...dialect.extensions], ast: dialect.ast, ops: { ...dialect.ops, ...blocked } });
|
|
182
|
+
return defineDialect({ name: dialect.name, extensions: [...dialect.extensions], ast: dialect.ast, ops: { ...dialect.ops, ...blocked } });
|
|
155
183
|
}
|
|
156
184
|
/**
|
|
157
185
|
* Derives a dialect whose named ops run `overrides` instead, with the same signatures. Throws at
|
|
@@ -162,7 +190,7 @@ export function withoutOps(dialect, names, reason) {
|
|
|
162
190
|
*/
|
|
163
191
|
export function overrideOps(dialect, overrides) {
|
|
164
192
|
assertKnownOps(dialect.ops, Object.keys(overrides), "overrideOps");
|
|
165
|
-
return defineDialect({ extensions: [...dialect.extensions], ast: dialect.ast, ops: { ...dialect.ops, ...overrides } });
|
|
193
|
+
return defineDialect({ name: dialect.name, extensions: [...dialect.extensions], ast: dialect.ast, ops: { ...dialect.ops, ...overrides } });
|
|
166
194
|
}
|
|
167
195
|
/**
|
|
168
196
|
* Routes `find` by file extension to one dialect per extension, each with its own AST and ops —
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { defineDialect, defineOpPack, withOps } from "../../../../domain/services/define-dialect.js";
|
|
2
|
-
import { parse, print } from "../typescript/ast.js";
|
|
2
|
+
import { parse, print, retire } from "../typescript/ast.js";
|
|
3
3
|
import { typescriptOps } from "../typescript/pack.js";
|
|
4
4
|
import { addBootstrap, addComponentImport, addComponentProvider, addComputed, addDeclaration, addInjection, addInput, addLazyRoute, addModel, addNgModuleExport, addNgModuleImport, addNgModuleProvider, addOutput, addRootProvider, addRoute, addSignal, } from "./ops.js";
|
|
5
5
|
export * as astLibrary from "ts-morph";
|
|
@@ -23,7 +23,7 @@ const angularOps = defineOpPack({
|
|
|
23
23
|
});
|
|
24
24
|
const baseDialect = defineDialect({
|
|
25
25
|
extensions: [".ts"],
|
|
26
|
-
ast: { parse, print },
|
|
26
|
+
ast: { parse, print, retire },
|
|
27
27
|
ops: {},
|
|
28
28
|
});
|
|
29
29
|
const angularDialect = withOps(baseDialect, typescriptOps, angularOps);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type Handle } from "../../../../domain/services/define-dialect.ts";
|
|
2
|
+
import { type CsharpDocument } from "../csharp/ast.ts";
|
|
3
|
+
import { type CsharpOps } from "../csharp/pack.ts";
|
|
4
|
+
import { type Lifetime, type ServiceOptions } from "./ops.ts";
|
|
5
|
+
export * as astLibrary from "web-tree-sitter";
|
|
6
|
+
export { language } from "../csharp/ast.ts";
|
|
7
|
+
export type { CsharpDocument, FromOptions, UsingOptions } from "../csharp/index.ts";
|
|
8
|
+
export type { Lifetime, ServiceOptions };
|
|
9
|
+
type ServiceOps = {
|
|
10
|
+
addService: (ast: CsharpDocument, lifetime: Lifetime, service: string, implementation?: string, options?: ServiceOptions) => void;
|
|
11
|
+
};
|
|
12
|
+
type AspnetOps = CsharpOps & ServiceOps;
|
|
13
|
+
/**
|
|
14
|
+
* Opens a `.cs` file of an ASP.NET Core application. The handle carries every
|
|
15
|
+
* `@pbuilder/sdk/csharp` op plus `addService`, chained on one handle and written once.
|
|
16
|
+
*
|
|
17
|
+
* `addService(lifetime, service, implementation?, { serviceFrom?, implementationFrom?, method? })`
|
|
18
|
+
* writes `<receiver>.Add<lifetime><service, implementation>();` once, `lifetime` being
|
|
19
|
+
* `"Singleton"`, `"Scoped"` or `"Transient"`; when `implementation` is left out or equals
|
|
20
|
+
* `service`, it writes `<receiver>.Add<lifetime><service>();`. Without `method` it edits the top-level statements
|
|
21
|
+
* of `Program.cs`: the receiver is `<builder>.Services` of the one variable initialized by
|
|
22
|
+
* `WebApplication.CreateBuilder` (or `CreateSlimBuilder`, `CreateEmptyBuilder`), and the line
|
|
23
|
+
* goes just before the statement that calls `<builder>.Build()`. With `method` it edits the one
|
|
24
|
+
* static method of that name whose first parameter is `this IServiceCollection`, before its
|
|
25
|
+
* `return`. `serviceFrom` and `implementationFrom` add the `using` of each type unless the file
|
|
26
|
+
* already sees it. The same registration already there, called with no arguments, changes
|
|
27
|
+
* nothing (`Add<L><S>()` and `Add<L><S, S>()` are the same registration); the same pair with
|
|
28
|
+
* another lifetime or with arguments such as a factory, or a file with no such place, rejects and
|
|
29
|
+
* leaves the file as it is.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* import * as aspnet from "@pbuilder/sdk/aspnet";
|
|
33
|
+
*
|
|
34
|
+
* // Program.cs before:
|
|
35
|
+
* // var builder = WebApplication.CreateBuilder(args);
|
|
36
|
+
* //
|
|
37
|
+
* // var app = builder.Build();
|
|
38
|
+
* export const run = async () => {
|
|
39
|
+
* await aspnet
|
|
40
|
+
* .find("Program.cs")
|
|
41
|
+
* .addService("Scoped", "IOrders", "SqlOrders", { serviceFrom: "Shop.Application.Ports", implementationFrom: "Shop.Infrastructure" });
|
|
42
|
+
* };
|
|
43
|
+
* // -> using Shop.Application.Ports;
|
|
44
|
+
* // -> using Shop.Infrastructure;
|
|
45
|
+
* // ->
|
|
46
|
+
* // -> var builder = WebApplication.CreateBuilder(args);
|
|
47
|
+
* // -> builder.Services.AddScoped<IOrders, SqlOrders>();
|
|
48
|
+
* // ->
|
|
49
|
+
* // -> var app = builder.Build();
|
|
50
|
+
*/
|
|
51
|
+
export declare function find(path: string): Handle<"found", CsharpDocument, AspnetOps>;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { defineDialect, defineOpPack, withOps } from "../../../../domain/services/define-dialect.js";
|
|
2
|
+
import { parse, print } from "../csharp/ast.js";
|
|
3
|
+
import { csharpOps } from "../csharp/pack.js";
|
|
4
|
+
import { addService } from "./ops.js";
|
|
5
|
+
export * as astLibrary from "web-tree-sitter";
|
|
6
|
+
export { language } from "../csharp/ast.js";
|
|
7
|
+
const serviceOps = defineOpPack({ addService });
|
|
8
|
+
const aspnetDialect = withOps(defineDialect({ extensions: [".cs"], ast: { parse, print }, ops: {} }), csharpOps, serviceOps);
|
|
9
|
+
/**
|
|
10
|
+
* Opens a `.cs` file of an ASP.NET Core application. The handle carries every
|
|
11
|
+
* `@pbuilder/sdk/csharp` op plus `addService`, chained on one handle and written once.
|
|
12
|
+
*
|
|
13
|
+
* `addService(lifetime, service, implementation?, { serviceFrom?, implementationFrom?, method? })`
|
|
14
|
+
* writes `<receiver>.Add<lifetime><service, implementation>();` once, `lifetime` being
|
|
15
|
+
* `"Singleton"`, `"Scoped"` or `"Transient"`; when `implementation` is left out or equals
|
|
16
|
+
* `service`, it writes `<receiver>.Add<lifetime><service>();`. Without `method` it edits the top-level statements
|
|
17
|
+
* of `Program.cs`: the receiver is `<builder>.Services` of the one variable initialized by
|
|
18
|
+
* `WebApplication.CreateBuilder` (or `CreateSlimBuilder`, `CreateEmptyBuilder`), and the line
|
|
19
|
+
* goes just before the statement that calls `<builder>.Build()`. With `method` it edits the one
|
|
20
|
+
* static method of that name whose first parameter is `this IServiceCollection`, before its
|
|
21
|
+
* `return`. `serviceFrom` and `implementationFrom` add the `using` of each type unless the file
|
|
22
|
+
* already sees it. The same registration already there, called with no arguments, changes
|
|
23
|
+
* nothing (`Add<L><S>()` and `Add<L><S, S>()` are the same registration); the same pair with
|
|
24
|
+
* another lifetime or with arguments such as a factory, or a file with no such place, rejects and
|
|
25
|
+
* leaves the file as it is.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* import * as aspnet from "@pbuilder/sdk/aspnet";
|
|
29
|
+
*
|
|
30
|
+
* // Program.cs before:
|
|
31
|
+
* // var builder = WebApplication.CreateBuilder(args);
|
|
32
|
+
* //
|
|
33
|
+
* // var app = builder.Build();
|
|
34
|
+
* export const run = async () => {
|
|
35
|
+
* await aspnet
|
|
36
|
+
* .find("Program.cs")
|
|
37
|
+
* .addService("Scoped", "IOrders", "SqlOrders", { serviceFrom: "Shop.Application.Ports", implementationFrom: "Shop.Infrastructure" });
|
|
38
|
+
* };
|
|
39
|
+
* // -> using Shop.Application.Ports;
|
|
40
|
+
* // -> using Shop.Infrastructure;
|
|
41
|
+
* // ->
|
|
42
|
+
* // -> var builder = WebApplication.CreateBuilder(args);
|
|
43
|
+
* // -> builder.Services.AddScoped<IOrders, SqlOrders>();
|
|
44
|
+
* // ->
|
|
45
|
+
* // -> var app = builder.Build();
|
|
46
|
+
*/
|
|
47
|
+
export function find(path) {
|
|
48
|
+
return aspnetDialect.find(path);
|
|
49
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type CsharpDocument } from "../csharp/ast.ts";
|
|
2
|
+
export type Lifetime = "Singleton" | "Scoped" | "Transient";
|
|
3
|
+
export interface ServiceOptions {
|
|
4
|
+
/** The namespace that declares the service type; its `using` is added unless the file already sees it. */
|
|
5
|
+
serviceFrom?: string;
|
|
6
|
+
/** The namespace that declares the implementation type, imported like `serviceFrom`. */
|
|
7
|
+
implementationFrom?: string;
|
|
8
|
+
/** Edits the one `static` method of this name whose first parameter is `this IServiceCollection`. */
|
|
9
|
+
method?: string;
|
|
10
|
+
}
|
|
11
|
+
export declare function addService(doc: CsharpDocument, lifetime: Lifetime, service: string, implementation?: string, options?: ServiceOptions): void;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { dialectError } from "../../../../domain/valueobjects/dialect-error.js";
|
|
2
|
+
import { withTree } from "../csharp/ast.js";
|
|
3
|
+
import { assertIdentifier, assertNamespace, assertOptions, assertType, compact } from "../csharp/names.js";
|
|
4
|
+
import { importNamespace } from "../csharp/ops.js";
|
|
5
|
+
import { endOfNode, indentAt, indentUnit, isTrivia, lineEnding, splice } from "../csharp/syntax.js";
|
|
6
|
+
const LIFETIMES = ["Singleton", "Scoped", "Transient"];
|
|
7
|
+
const BUILDER_FACTORIES = new Set(["CreateBuilder", "CreateSlimBuilder", "CreateEmptyBuilder"]);
|
|
8
|
+
/** `<receiver>.Add<Lifetime><Service>(...)` or `<receiver>.Add<Lifetime><Service, Implementation>(...)` as a registration, or `undefined`. */
|
|
9
|
+
function registration(statement, receiver) {
|
|
10
|
+
if (statement.type !== "expression_statement")
|
|
11
|
+
return undefined;
|
|
12
|
+
const call = statement.namedChildren[0];
|
|
13
|
+
const callee = call?.type === "invocation_expression" ? call.childForFieldName("function") : null;
|
|
14
|
+
if (callee?.type !== "member_access_expression" || compact(callee.childForFieldName("expression").text) !== receiver)
|
|
15
|
+
return undefined;
|
|
16
|
+
const generic = callee.childForFieldName("name");
|
|
17
|
+
const lifetime = /^Add(Singleton|Scoped|Transient)$/.exec(generic.namedChildren[0]?.text ?? "")?.[1];
|
|
18
|
+
const types = generic.namedChildren[1]?.namedChildren ?? [];
|
|
19
|
+
if (generic.type !== "generic_name" || lifetime === undefined || (types.length !== 1 && types.length !== 2))
|
|
20
|
+
return undefined;
|
|
21
|
+
const service = compact(types[0].text);
|
|
22
|
+
return {
|
|
23
|
+
lifetime,
|
|
24
|
+
service,
|
|
25
|
+
implementation: types.length === 2 ? compact(types[1].text) : service,
|
|
26
|
+
passesArguments: (call.childForFieldName("arguments")?.namedChildren.length ?? 0) > 0,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
function isBuilderCall(value) {
|
|
30
|
+
const callee = value?.type === "invocation_expression" ? value.childForFieldName("function") : null;
|
|
31
|
+
return (callee?.type === "member_access_expression" &&
|
|
32
|
+
callee.childForFieldName("expression").text === "WebApplication" &&
|
|
33
|
+
BUILDER_FACTORIES.has(callee.childForFieldName("name").text));
|
|
34
|
+
}
|
|
35
|
+
function callsBuild(statement, builder) {
|
|
36
|
+
return statement.descendantsOfType("invocation_expression").some((call) => {
|
|
37
|
+
const callee = call.childForFieldName("function");
|
|
38
|
+
return (callee?.type === "member_access_expression" &&
|
|
39
|
+
callee.childForFieldName("expression").text === builder &&
|
|
40
|
+
callee.childForFieldName("name").text === "Build");
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/** The top-level statements: the builder, and the line after the last statement before the one calling `Build()`. */
|
|
44
|
+
function programPlace(op, root) {
|
|
45
|
+
const statements = root.namedChildren.filter((child) => child.type === "global_statement").map((child) => child.namedChildren[0]);
|
|
46
|
+
const builders = statements.flatMap((statement) => statement.type === "local_declaration_statement"
|
|
47
|
+
? statement.descendantsOfType("variable_declarator").filter((d) => isBuilderCall(d.namedChildren.at(-1) ?? null))
|
|
48
|
+
: []);
|
|
49
|
+
if (builders.length !== 1) {
|
|
50
|
+
throw dialectError(`${op} — the top-level statements need exactly one variable initialized by WebApplication.CreateBuilder, CreateSlimBuilder or CreateEmptyBuilder; found ${builders.length}. Pass \`method\` to edit a service collection extension method instead`);
|
|
51
|
+
}
|
|
52
|
+
const builder = builders[0].childForFieldName("name").text;
|
|
53
|
+
const build = statements.findIndex((statement) => callsBuild(statement, builder));
|
|
54
|
+
if (build < 1)
|
|
55
|
+
throw dialectError(`${op} — no top-level statement after the builder calls \`${builder}.Build()\``);
|
|
56
|
+
const before = statements[build - 1];
|
|
57
|
+
return {
|
|
58
|
+
receiver: `${builder}.Services`,
|
|
59
|
+
statements,
|
|
60
|
+
insert: (source, line) => {
|
|
61
|
+
const at = endOfNode(source, before);
|
|
62
|
+
return splice(source, at, at, lineEnding(source) + indentAt(source, before.startIndex) + line);
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function isServiceCollectionExtension(method) {
|
|
67
|
+
if (!method.children.some((child) => child.type === "modifier" && child.text === "static"))
|
|
68
|
+
return false;
|
|
69
|
+
const first = method.childForFieldName("parameters")?.namedChildren.find((child) => child.type === "parameter");
|
|
70
|
+
const isThis = first?.children.some((child) => child.type === "modifier" && child.text === "this") === true;
|
|
71
|
+
return isThis && /(^|\.)IServiceCollection$/.test(compact(first.childForFieldName("type")?.text ?? ""));
|
|
72
|
+
}
|
|
73
|
+
/** The one `static` method `name(this IServiceCollection ...)`: before its last statement when that is a `return`, else after it. */
|
|
74
|
+
function methodPlace(op, root, name) {
|
|
75
|
+
const methods = root
|
|
76
|
+
.descendantsOfType("method_declaration")
|
|
77
|
+
.filter((method) => method.childForFieldName("name").text === name && isServiceCollectionExtension(method));
|
|
78
|
+
if (methods.length !== 1) {
|
|
79
|
+
throw dialectError(`${op} — the file needs exactly one static method \`${name}\` whose first parameter is \`this IServiceCollection\`; found ${methods.length}`);
|
|
80
|
+
}
|
|
81
|
+
const method = methods[0];
|
|
82
|
+
const body = method.childForFieldName("body");
|
|
83
|
+
if (body?.type !== "block")
|
|
84
|
+
throw dialectError(`${op} — \`${name}\` has an expression body; edit it with .modify()`);
|
|
85
|
+
const receiver = method.childForFieldName("parameters").namedChildren.find((child) => child.type === "parameter").childForFieldName("name").text;
|
|
86
|
+
const statements = body.namedChildren.filter((child) => !isTrivia(child));
|
|
87
|
+
const last = statements.at(-1);
|
|
88
|
+
const insert = (source, line) => {
|
|
89
|
+
const eol = lineEnding(source);
|
|
90
|
+
if (last === undefined) {
|
|
91
|
+
const open = body.children[0];
|
|
92
|
+
return splice(source, open.endIndex, open.endIndex, eol + indentAt(source, method.startIndex) + indentUnit(source, root) + line);
|
|
93
|
+
}
|
|
94
|
+
const indent = indentAt(source, last.startIndex);
|
|
95
|
+
if (last.type === "return_statement")
|
|
96
|
+
return splice(source, last.startIndex, last.startIndex, line + eol + indent);
|
|
97
|
+
const at = endOfNode(source, last);
|
|
98
|
+
return splice(source, at, at, eol + indent + line);
|
|
99
|
+
};
|
|
100
|
+
return { receiver, statements, insert };
|
|
101
|
+
}
|
|
102
|
+
export function addService(doc, lifetime, service, implementation, options) {
|
|
103
|
+
const op = "addService";
|
|
104
|
+
if (typeof lifetime !== "string" || !LIFETIMES.includes(lifetime)) {
|
|
105
|
+
throw dialectError(`${op} — \`lifetime\` ${JSON.stringify(lifetime)} is not a lifetime; use "Singleton", "Scoped" or "Transient"`);
|
|
106
|
+
}
|
|
107
|
+
assertType(op, "service", service);
|
|
108
|
+
if (implementation !== undefined)
|
|
109
|
+
assertType(op, "implementation", implementation);
|
|
110
|
+
const implementationType = implementation ?? service;
|
|
111
|
+
const singleType = compact(implementationType) === compact(service);
|
|
112
|
+
const opts = assertOptions(op, options, ["serviceFrom", "implementationFrom", "method"]);
|
|
113
|
+
if (opts.serviceFrom !== undefined)
|
|
114
|
+
assertNamespace(op, "serviceFrom", opts.serviceFrom);
|
|
115
|
+
if (opts.implementationFrom !== undefined)
|
|
116
|
+
assertNamespace(op, "implementationFrom", opts.implementationFrom);
|
|
117
|
+
if (opts.method !== undefined)
|
|
118
|
+
assertIdentifier(op, "method", opts.method);
|
|
119
|
+
const edited = withTree(doc.text, (root) => {
|
|
120
|
+
const place = opts.method === undefined ? programPlace(op, root) : methodPlace(op, root, opts.method);
|
|
121
|
+
const receiver = compact(place.receiver);
|
|
122
|
+
const existing = place.statements
|
|
123
|
+
.map((statement) => registration(statement, receiver))
|
|
124
|
+
.find((r) => r !== undefined && r.service === compact(service) && r.implementation === compact(implementationType));
|
|
125
|
+
if (existing?.passesArguments) {
|
|
126
|
+
throw dialectError(`${op}(${JSON.stringify(service)}) — \`${service}\` is already registered with \`${implementationType}\` by a call that passes arguments; edit it with .modify()`);
|
|
127
|
+
}
|
|
128
|
+
if (existing?.lifetime === lifetime)
|
|
129
|
+
return undefined;
|
|
130
|
+
if (existing !== undefined) {
|
|
131
|
+
throw dialectError(`${op}(${JSON.stringify(service)}) — \`${service}\` is already registered with \`${implementationType}\` as ${existing.lifetime}, not ${lifetime}`);
|
|
132
|
+
}
|
|
133
|
+
const types = singleType ? service.trim() : `${service.trim()}, ${implementationType.trim()}`;
|
|
134
|
+
return place.insert(doc.text, `${place.receiver}.Add${lifetime}<${types}>();`);
|
|
135
|
+
});
|
|
136
|
+
if (edited === undefined)
|
|
137
|
+
return;
|
|
138
|
+
doc.text = importNamespace(op, importNamespace(op, edited, opts.serviceFrom), opts.implementationFrom);
|
|
139
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Language, type Node } from "web-tree-sitter";
|
|
2
|
+
/** The tree-sitter C# grammar (tree-sitter-c-sharp 0.24.0) the dialect parses with. */
|
|
3
|
+
export declare const language: Language;
|
|
4
|
+
/**
|
|
5
|
+
* The live document a C# handle edits. `text` is the current source without its UTF-8 BOM
|
|
6
|
+
* (re-added on print). tree-sitter trees are read-only views of a text, so named ops replace
|
|
7
|
+
* `text` with spliced edits; a `.modify(fn)` callback may do the same, parsing `text` with
|
|
8
|
+
* `astLibrary` (`web-tree-sitter`) and `language`.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* import * as csharp from "@pbuilder/sdk/csharp";
|
|
12
|
+
*
|
|
13
|
+
* await csharp.find("Orders.cs").modify((doc) => {
|
|
14
|
+
* doc.text = doc.text.replace("internal class", "public class");
|
|
15
|
+
* });
|
|
16
|
+
*/
|
|
17
|
+
export interface CsharpDocument {
|
|
18
|
+
text: string;
|
|
19
|
+
}
|
|
20
|
+
/** Runs `fn` over a fresh tree of `text`, freeing the tree's WASM memory afterwards. */
|
|
21
|
+
export declare function withTree<T>(text: string, fn: (root: Node) => T): T;
|
|
22
|
+
/** True when `text` parses as C# with no ERROR or MISSING node. */
|
|
23
|
+
export declare function isValidCsharp(text: string): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* Wraps `source` in a `CsharpDocument`, throwing when it is not valid C# syntax. A compile
|
|
26
|
+
* error that is not a syntax error, such as an unknown type, parses.
|
|
27
|
+
*/
|
|
28
|
+
export declare function parse(source: string): CsharpDocument;
|
|
29
|
+
export declare function print(doc: CsharpDocument): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { fileURLToPath } from "node:url";
|
|
2
|
+
import { Language, Parser } from "web-tree-sitter";
|
|
3
|
+
const BOM = "";
|
|
4
|
+
// `ast.parse` is synchronous and loading WASM is not, so the module loads it once at import.
|
|
5
|
+
await Parser.init();
|
|
6
|
+
/** The tree-sitter C# grammar (tree-sitter-c-sharp 0.24.0) the dialect parses with. */
|
|
7
|
+
export const language = await Language.load(fileURLToPath(new URL("./tree-sitter-c_sharp.wasm", import.meta.url)));
|
|
8
|
+
const parser = new Parser();
|
|
9
|
+
parser.setLanguage(language);
|
|
10
|
+
const hadBom = new WeakSet();
|
|
11
|
+
/** Runs `fn` over a fresh tree of `text`, freeing the tree's WASM memory afterwards. */
|
|
12
|
+
export function withTree(text, fn) {
|
|
13
|
+
const tree = parser.parse(text);
|
|
14
|
+
if (tree === null)
|
|
15
|
+
throw new Error("invalid C#");
|
|
16
|
+
try {
|
|
17
|
+
return fn(tree.rootNode);
|
|
18
|
+
}
|
|
19
|
+
finally {
|
|
20
|
+
tree.delete();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** True when `text` parses as C# with no ERROR or MISSING node. */
|
|
24
|
+
export function isValidCsharp(text) {
|
|
25
|
+
return withTree(text, (root) => !root.hasError);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Wraps `source` in a `CsharpDocument`, throwing when it is not valid C# syntax. A compile
|
|
29
|
+
* error that is not a syntax error, such as an unknown type, parses.
|
|
30
|
+
*/
|
|
31
|
+
export function parse(source) {
|
|
32
|
+
const doc = { text: source.startsWith(BOM) ? source.slice(BOM.length) : source };
|
|
33
|
+
if (!isValidCsharp(doc.text))
|
|
34
|
+
throw new Error("invalid C#");
|
|
35
|
+
if (source.startsWith(BOM))
|
|
36
|
+
hadBom.add(doc);
|
|
37
|
+
return doc;
|
|
38
|
+
}
|
|
39
|
+
export function print(doc) {
|
|
40
|
+
return hadBom.has(doc) ? BOM + doc.text : doc.text;
|
|
41
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type Handle } from "../../../../domain/services/define-dialect.ts";
|
|
2
|
+
import { type CsharpDocument } from "./ast.ts";
|
|
3
|
+
import type { FromOptions, UsingOptions } from "./ops.ts";
|
|
4
|
+
import { type CsharpOps } from "./pack.ts";
|
|
5
|
+
export * as astLibrary from "web-tree-sitter";
|
|
6
|
+
export { language } from "./ast.ts";
|
|
7
|
+
export type { CsharpDocument, FromOptions, UsingOptions };
|
|
8
|
+
/**
|
|
9
|
+
* Opens a `.cs` file for editing (`.csx` excluded). A file that is not valid C# syntax fails the
|
|
10
|
+
* factory; a compile error such as an unknown type does not, since the dialect has no compiler.
|
|
11
|
+
* Every op edits only the bytes it must, and inserted code follows the file's line ending, BOM
|
|
12
|
+
* and member indentation (four spaces when it has none); `.editorconfig` is never read.
|
|
13
|
+
*
|
|
14
|
+
* - `addUsing(namespace, { static?, global?, alias? })` adds a using directive once.
|
|
15
|
+
* - `addBaseType(typeName, base, { from? })` appends a base type to the base list once.
|
|
16
|
+
* - `addMember(typeName, source)` appends one member; an identical member changes nothing.
|
|
17
|
+
* - `addConstructorParameter(typeName, name, type, { from? })` appends a primary constructor
|
|
18
|
+
* parameter; a type with an explicit constructor, or an interface, rejects.
|
|
19
|
+
*
|
|
20
|
+
* `typeName` names the one class, record, struct or interface of that name at the top level or
|
|
21
|
+
* in a namespace. `from` names the namespace that declares the type and adds its `using`
|
|
22
|
+
* unless the file already sees it. `.modify(fn)` hands `fn` the live `CsharpDocument`; parse its
|
|
23
|
+
* `text` with `astLibrary` (`web-tree-sitter`) and `language` for anything the ops do not cover.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* import * as csharp from "@pbuilder/sdk/csharp";
|
|
27
|
+
*
|
|
28
|
+
* // Orders.cs before: namespace Shop.Infrastructure;\n\npublic class Orders\n{\n}\n
|
|
29
|
+
* export const run = async () => {
|
|
30
|
+
* await csharp
|
|
31
|
+
* .find("Orders.cs")
|
|
32
|
+
* .addBaseType("Orders", "IOrders", { from: "Shop.Application.Ports" })
|
|
33
|
+
* .addConstructorParameter("Orders", "db", "ShopDb", { from: "Shop.Data" });
|
|
34
|
+
* };
|
|
35
|
+
* // -> using Shop.Application.Ports;
|
|
36
|
+
* // -> using Shop.Data;
|
|
37
|
+
* // ->
|
|
38
|
+
* // -> namespace Shop.Infrastructure;
|
|
39
|
+
* // ->
|
|
40
|
+
* // -> public class Orders(ShopDb db) : IOrders
|
|
41
|
+
* // -> {
|
|
42
|
+
* // -> }
|
|
43
|
+
*/
|
|
44
|
+
export declare function find(path: string): Handle<"found", CsharpDocument, CsharpOps>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { defineDialect, withOps } from "../../../../domain/services/define-dialect.js";
|
|
2
|
+
import { parse, print } from "./ast.js";
|
|
3
|
+
import { csharpOps } from "./pack.js";
|
|
4
|
+
export * as astLibrary from "web-tree-sitter";
|
|
5
|
+
export { language } from "./ast.js";
|
|
6
|
+
const csharpDialect = withOps(defineDialect({ extensions: [".cs"], ast: { parse, print }, ops: {} }), csharpOps);
|
|
7
|
+
/**
|
|
8
|
+
* Opens a `.cs` file for editing (`.csx` excluded). A file that is not valid C# syntax fails the
|
|
9
|
+
* factory; a compile error such as an unknown type does not, since the dialect has no compiler.
|
|
10
|
+
* Every op edits only the bytes it must, and inserted code follows the file's line ending, BOM
|
|
11
|
+
* and member indentation (four spaces when it has none); `.editorconfig` is never read.
|
|
12
|
+
*
|
|
13
|
+
* - `addUsing(namespace, { static?, global?, alias? })` adds a using directive once.
|
|
14
|
+
* - `addBaseType(typeName, base, { from? })` appends a base type to the base list once.
|
|
15
|
+
* - `addMember(typeName, source)` appends one member; an identical member changes nothing.
|
|
16
|
+
* - `addConstructorParameter(typeName, name, type, { from? })` appends a primary constructor
|
|
17
|
+
* parameter; a type with an explicit constructor, or an interface, rejects.
|
|
18
|
+
*
|
|
19
|
+
* `typeName` names the one class, record, struct or interface of that name at the top level or
|
|
20
|
+
* in a namespace. `from` names the namespace that declares the type and adds its `using`
|
|
21
|
+
* unless the file already sees it. `.modify(fn)` hands `fn` the live `CsharpDocument`; parse its
|
|
22
|
+
* `text` with `astLibrary` (`web-tree-sitter`) and `language` for anything the ops do not cover.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import * as csharp from "@pbuilder/sdk/csharp";
|
|
26
|
+
*
|
|
27
|
+
* // Orders.cs before: namespace Shop.Infrastructure;\n\npublic class Orders\n{\n}\n
|
|
28
|
+
* export const run = async () => {
|
|
29
|
+
* await csharp
|
|
30
|
+
* .find("Orders.cs")
|
|
31
|
+
* .addBaseType("Orders", "IOrders", { from: "Shop.Application.Ports" })
|
|
32
|
+
* .addConstructorParameter("Orders", "db", "ShopDb", { from: "Shop.Data" });
|
|
33
|
+
* };
|
|
34
|
+
* // -> using Shop.Application.Ports;
|
|
35
|
+
* // -> using Shop.Data;
|
|
36
|
+
* // ->
|
|
37
|
+
* // -> namespace Shop.Infrastructure;
|
|
38
|
+
* // ->
|
|
39
|
+
* // -> public class Orders(ShopDb db) : IOrders
|
|
40
|
+
* // -> {
|
|
41
|
+
* // -> }
|
|
42
|
+
*/
|
|
43
|
+
export function find(path) {
|
|
44
|
+
return csharpDialect.find(path);
|
|
45
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function assertIdentifier(op: string, argument: string, value: unknown): asserts value is string;
|
|
2
|
+
export declare function assertNamespace(op: string, argument: string, value: unknown): asserts value is string;
|
|
3
|
+
/** A type must parse as exactly one C# type: a name, qualified, generic, nullable, array or tuple type. */
|
|
4
|
+
export declare function assertType(op: string, argument: string, value: unknown): asserts value is string;
|
|
5
|
+
export declare function assertOptions(op: string, value: unknown, allowed: readonly string[]): Record<string, unknown>;
|
|
6
|
+
/** Text with every whitespace removed, to compare code as written ignoring whitespace. */
|
|
7
|
+
export declare function compact(text: string): string;
|