@webpieces/code-rules 0.4.979 → 0.4.980
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/package.json +8 -7
- package/src/code-policy-registry.js +5 -0
- package/src/code-policy-registry.js.map +1 -1
- package/src/configs/ipc-configs.d.ts +158 -0
- package/src/configs/ipc-configs.js +206 -0
- package/src/configs/ipc-configs.js.map +1 -0
- package/src/index.d.ts +7 -0
- package/src/index.js +20 -1
- package/src/index.js.map +1 -1
- package/src/ipc/ipc-boundary-analyzer.d.ts +92 -0
- package/src/ipc/ipc-boundary-analyzer.js +371 -0
- package/src/ipc/ipc-boundary-analyzer.js.map +1 -0
- package/src/ipc/ipc-contract-inventory.d.ts +83 -0
- package/src/ipc/ipc-contract-inventory.js +215 -0
- package/src/ipc/ipc-contract-inventory.js.map +1 -0
- package/src/ipc/ipc-contract-shape-analyzer.d.ts +54 -0
- package/src/ipc/ipc-contract-shape-analyzer.js +186 -0
- package/src/ipc/ipc-contract-shape-analyzer.js.map +1 -0
- package/src/ipc/ipc-controller-resolver.d.ts +43 -0
- package/src/ipc/ipc-controller-resolver.js +187 -0
- package/src/ipc/ipc-controller-resolver.js.map +1 -0
- package/src/ipc/ipc-pairing-analyzer.d.ts +43 -0
- package/src/ipc/ipc-pairing-analyzer.js +247 -0
- package/src/ipc/ipc-pairing-analyzer.js.map +1 -0
- package/src/ipc/ipc-protocol-fingerprint.d.ts +50 -0
- package/src/ipc/ipc-protocol-fingerprint.js +215 -0
- package/src/ipc/ipc-protocol-fingerprint.js.map +1 -0
- package/src/ipc/ipc-protocol-recorder.d.ts +25 -0
- package/src/ipc/ipc-protocol-recorder.js +98 -0
- package/src/ipc/ipc-protocol-recorder.js.map +1 -0
- package/src/ipc/ipc-rule-support.d.ts +27 -0
- package/src/ipc/ipc-rule-support.js +76 -0
- package/src/ipc/ipc-rule-support.js.map +1 -0
- package/src/ipc/ipc-source.d.ts +96 -0
- package/src/ipc/ipc-source.js +239 -0
- package/src/ipc/ipc-source.js.map +1 -0
- package/src/ipc-record-protocol.d.ts +2 -0
- package/src/ipc-record-protocol.js +34 -0
- package/src/ipc-record-protocol.js.map +1 -0
- package/src/rule-help.js +4 -0
- package/src/rule-help.js.map +1 -1
- package/src/rule-settings.js +119 -1
- package/src/rule-settings.js.map +1 -1
- package/src/validate-ipc-boundary.d.ts +20 -0
- package/src/validate-ipc-boundary.js +96 -0
- package/src/validate-ipc-boundary.js.map +1 -0
- package/src/validate-ipc-contract-shape.d.ts +20 -0
- package/src/validate-ipc-contract-shape.js +105 -0
- package/src/validate-ipc-contract-shape.js.map +1 -0
- package/src/validate-ipc-pairing.d.ts +16 -0
- package/src/validate-ipc-pairing.js +71 -0
- package/src/validate-ipc-pairing.js.map +1 -0
- package/src/validate-ipc-protocol-fingerprint.d.ts +23 -0
- package/src/validate-ipc-protocol-fingerprint.js +79 -0
- package/src/validate-ipc-protocol-fingerprint.js.map +1 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The type-checked read of the IPC contract library (#1389), shared by `ipc-pairing` and
|
|
3
|
+
* `ipc-protocol-fingerprint`. Its `ts.Program` is rooted at the configured contract files ALONE (plus the
|
|
4
|
+
* wire table file), so the program holds the contract library and what it imports — never the endpoint
|
|
5
|
+
* apps, never an unrelated project. `types: []` keeps every ambient `@types/*` package out as well.
|
|
6
|
+
*
|
|
7
|
+
* The checker is what makes a wire table row like `{ wire: AUDIO_PLAY, direction: WireDirectionDto.NATIVE }`
|
|
8
|
+
* readable: both are resolved to their literal values, exactly as the replaced script read them.
|
|
9
|
+
*/
|
|
10
|
+
import * as ts from 'typescript';
|
|
11
|
+
/** A literal value read from the contract through the checker. */
|
|
12
|
+
export type IpcLiteral = string | number | undefined;
|
|
13
|
+
/** One method of a directional contract class. Data-only. */
|
|
14
|
+
export declare class IpcContractMethod {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
readonly id: IpcLiteral;
|
|
17
|
+
/** `request` / `notification`, or undefined when the decorator has a non-literal kind. */
|
|
18
|
+
readonly kind: string | undefined;
|
|
19
|
+
readonly hasEndpoint: boolean;
|
|
20
|
+
readonly declaration: ts.MethodDeclaration;
|
|
21
|
+
constructor(name: string, id: IpcLiteral,
|
|
22
|
+
/** `request` / `notification`, or undefined when the decorator has a non-literal kind. */
|
|
23
|
+
kind: string | undefined, hasEndpoint: boolean, declaration: ts.MethodDeclaration);
|
|
24
|
+
}
|
|
25
|
+
/** One directional contract class of the contract library (an `…ToNativeApi`). Data-only. */
|
|
26
|
+
export declare class IpcDirectionalContract {
|
|
27
|
+
readonly name: string;
|
|
28
|
+
/** The `@WpInternal` id, or undefined when absent / not a literal. */
|
|
29
|
+
readonly id: IpcLiteral;
|
|
30
|
+
readonly relFile: string;
|
|
31
|
+
readonly declaration: ts.ClassDeclaration;
|
|
32
|
+
readonly methods: readonly IpcContractMethod[];
|
|
33
|
+
constructor(name: string,
|
|
34
|
+
/** The `@WpInternal` id, or undefined when absent / not a literal. */
|
|
35
|
+
id: IpcLiteral, relFile: string, declaration: ts.ClassDeclaration, methods: readonly IpcContractMethod[]);
|
|
36
|
+
}
|
|
37
|
+
/** One row of the configured wire table, each field resolved to its literal value. Data-only. */
|
|
38
|
+
export declare class IpcWireRow {
|
|
39
|
+
readonly api: IpcLiteral;
|
|
40
|
+
readonly method: IpcLiteral;
|
|
41
|
+
readonly wire: IpcLiteral;
|
|
42
|
+
readonly direction: IpcLiteral;
|
|
43
|
+
readonly kind: IpcLiteral;
|
|
44
|
+
readonly since: IpcLiteral;
|
|
45
|
+
constructor(api: IpcLiteral, method: IpcLiteral, wire: IpcLiteral, direction: IpcLiteral, kind: IpcLiteral, since: IpcLiteral);
|
|
46
|
+
}
|
|
47
|
+
/** Where the literal wire table lives, when the contract has one. Data-only. */
|
|
48
|
+
export declare class IpcWireTableRef {
|
|
49
|
+
readonly relFile: string;
|
|
50
|
+
readonly exportName: string;
|
|
51
|
+
constructor(relFile: string, exportName: string);
|
|
52
|
+
}
|
|
53
|
+
/** The wire table a config names, or undefined when it names none; one of the two null is a config error. */
|
|
54
|
+
export declare class IpcWireTableConfig {
|
|
55
|
+
of(ruleName: string, file: string | null, exportName: string | null): IpcWireTableRef | undefined;
|
|
56
|
+
}
|
|
57
|
+
/** Everything the two project-scoped rules read from the contract library. */
|
|
58
|
+
export declare class IpcContractInventory {
|
|
59
|
+
readonly program: ts.Program;
|
|
60
|
+
readonly contracts: ReadonlyMap<string, IpcDirectionalContract>;
|
|
61
|
+
/** Undefined when no wire table is configured. */
|
|
62
|
+
readonly rows: readonly IpcWireRow[] | undefined;
|
|
63
|
+
readonly errors: readonly string[];
|
|
64
|
+
constructor(program: ts.Program, contracts: ReadonlyMap<string, IpcDirectionalContract>,
|
|
65
|
+
/** Undefined when no wire table is configured. */
|
|
66
|
+
rows: readonly IpcWireRow[] | undefined, errors: readonly string[]);
|
|
67
|
+
get checker(): ts.TypeChecker;
|
|
68
|
+
}
|
|
69
|
+
export declare class IpcContractInventoryReader {
|
|
70
|
+
private readonly syntax;
|
|
71
|
+
/**
|
|
72
|
+
* Builds the contract-rooted program and reads its directional contracts and wire table.
|
|
73
|
+
* `contractFiles` are workspace-relative; `wireTable` must name an existing file when given.
|
|
74
|
+
*/
|
|
75
|
+
read(workspaceRoot: string, options: ts.CompilerOptions, contractFiles: readonly string[], apiSuffixes: readonly string[], wireTable: IpcWireTableRef | undefined): IpcContractInventory;
|
|
76
|
+
private readContract;
|
|
77
|
+
/** The endpoint `kind`, resolved through the checker (a const or enum member counts). */
|
|
78
|
+
private endpointKind;
|
|
79
|
+
private readRows;
|
|
80
|
+
private readRow;
|
|
81
|
+
private findConst;
|
|
82
|
+
private literal;
|
|
83
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* The type-checked read of the IPC contract library (#1389), shared by `ipc-pairing` and
|
|
4
|
+
* `ipc-protocol-fingerprint`. Its `ts.Program` is rooted at the configured contract files ALONE (plus the
|
|
5
|
+
* wire table file), so the program holds the contract library and what it imports — never the endpoint
|
|
6
|
+
* apps, never an unrelated project. `types: []` keeps every ambient `@types/*` package out as well.
|
|
7
|
+
*
|
|
8
|
+
* The checker is what makes a wire table row like `{ wire: AUDIO_PLAY, direction: WireDirectionDto.NATIVE }`
|
|
9
|
+
* readable: both are resolved to their literal values, exactly as the replaced script read them.
|
|
10
|
+
*/
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.IpcContractInventoryReader = exports.IpcContractInventory = exports.IpcWireTableConfig = exports.IpcWireTableRef = exports.IpcWireRow = exports.IpcDirectionalContract = exports.IpcContractMethod = void 0;
|
|
13
|
+
const tslib_1 = require("tslib");
|
|
14
|
+
const path = tslib_1.__importStar(require("path"));
|
|
15
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
16
|
+
const tooling_common_1 = require("@webpieces/tooling-common");
|
|
17
|
+
const ipc_contract_shape_analyzer_1 = require("./ipc-contract-shape-analyzer");
|
|
18
|
+
const ipc_source_1 = require("./ipc-source");
|
|
19
|
+
/** One method of a directional contract class. Data-only. */
|
|
20
|
+
class IpcContractMethod {
|
|
21
|
+
name;
|
|
22
|
+
id;
|
|
23
|
+
kind;
|
|
24
|
+
hasEndpoint;
|
|
25
|
+
declaration;
|
|
26
|
+
constructor(name, id,
|
|
27
|
+
/** `request` / `notification`, or undefined when the decorator has a non-literal kind. */
|
|
28
|
+
kind, hasEndpoint, declaration) {
|
|
29
|
+
this.name = name;
|
|
30
|
+
this.id = id;
|
|
31
|
+
this.kind = kind;
|
|
32
|
+
this.hasEndpoint = hasEndpoint;
|
|
33
|
+
this.declaration = declaration;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.IpcContractMethod = IpcContractMethod;
|
|
37
|
+
/** One directional contract class of the contract library (an `…ToNativeApi`). Data-only. */
|
|
38
|
+
class IpcDirectionalContract {
|
|
39
|
+
name;
|
|
40
|
+
id;
|
|
41
|
+
relFile;
|
|
42
|
+
declaration;
|
|
43
|
+
methods;
|
|
44
|
+
constructor(name,
|
|
45
|
+
/** The `@WpInternal` id, or undefined when absent / not a literal. */
|
|
46
|
+
id, relFile, declaration, methods) {
|
|
47
|
+
this.name = name;
|
|
48
|
+
this.id = id;
|
|
49
|
+
this.relFile = relFile;
|
|
50
|
+
this.declaration = declaration;
|
|
51
|
+
this.methods = methods;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.IpcDirectionalContract = IpcDirectionalContract;
|
|
55
|
+
/** One row of the configured wire table, each field resolved to its literal value. Data-only. */
|
|
56
|
+
class IpcWireRow {
|
|
57
|
+
api;
|
|
58
|
+
method;
|
|
59
|
+
wire;
|
|
60
|
+
direction;
|
|
61
|
+
kind;
|
|
62
|
+
since;
|
|
63
|
+
constructor(api, method, wire, direction, kind, since) {
|
|
64
|
+
this.api = api;
|
|
65
|
+
this.method = method;
|
|
66
|
+
this.wire = wire;
|
|
67
|
+
this.direction = direction;
|
|
68
|
+
this.kind = kind;
|
|
69
|
+
this.since = since;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
exports.IpcWireRow = IpcWireRow;
|
|
73
|
+
/** Where the literal wire table lives, when the contract has one. Data-only. */
|
|
74
|
+
class IpcWireTableRef {
|
|
75
|
+
relFile;
|
|
76
|
+
exportName;
|
|
77
|
+
constructor(relFile, exportName) {
|
|
78
|
+
this.relFile = relFile;
|
|
79
|
+
this.exportName = exportName;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
exports.IpcWireTableRef = IpcWireTableRef;
|
|
83
|
+
/** The wire table a config names, or undefined when it names none; one of the two null is a config error. */
|
|
84
|
+
class IpcWireTableConfig {
|
|
85
|
+
of(ruleName, file, exportName) {
|
|
86
|
+
if (file === null && exportName === null)
|
|
87
|
+
return undefined;
|
|
88
|
+
if (file === null || exportName === null) {
|
|
89
|
+
throw new tooling_common_1.InformAiError(`${ruleName}: "wireTableFile" and "wireTableExport" must both be set or both be null ` +
|
|
90
|
+
`(got ${JSON.stringify(file)} / ${JSON.stringify(exportName)}).`);
|
|
91
|
+
}
|
|
92
|
+
return new IpcWireTableRef(file, exportName);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
exports.IpcWireTableConfig = IpcWireTableConfig;
|
|
96
|
+
/** Everything the two project-scoped rules read from the contract library. */
|
|
97
|
+
class IpcContractInventory {
|
|
98
|
+
program;
|
|
99
|
+
contracts;
|
|
100
|
+
rows;
|
|
101
|
+
errors;
|
|
102
|
+
constructor(program, contracts,
|
|
103
|
+
/** Undefined when no wire table is configured. */
|
|
104
|
+
rows, errors) {
|
|
105
|
+
this.program = program;
|
|
106
|
+
this.contracts = contracts;
|
|
107
|
+
this.rows = rows;
|
|
108
|
+
this.errors = errors;
|
|
109
|
+
}
|
|
110
|
+
get checker() {
|
|
111
|
+
return this.program.getTypeChecker();
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
exports.IpcContractInventory = IpcContractInventory;
|
|
115
|
+
class IpcContractInventoryReader {
|
|
116
|
+
syntax = new ipc_contract_shape_analyzer_1.IpcContractSyntax();
|
|
117
|
+
/**
|
|
118
|
+
* Builds the contract-rooted program and reads its directional contracts and wire table.
|
|
119
|
+
* `contractFiles` are workspace-relative; `wireTable` must name an existing file when given.
|
|
120
|
+
*/
|
|
121
|
+
read(workspaceRoot, options, contractFiles, apiSuffixes, wireTable) {
|
|
122
|
+
const roots = [...contractFiles];
|
|
123
|
+
if (wireTable !== undefined && !roots.includes(wireTable.relFile))
|
|
124
|
+
roots.push(wireTable.relFile);
|
|
125
|
+
const program = ts.createProgram(roots.map((rel) => path.join(workspaceRoot, rel)), { ...options, types: [] });
|
|
126
|
+
const checker = program.getTypeChecker();
|
|
127
|
+
const errors = [];
|
|
128
|
+
const contracts = new Map();
|
|
129
|
+
for (const rel of contractFiles) {
|
|
130
|
+
const source = program.getSourceFile(path.join(workspaceRoot, rel));
|
|
131
|
+
if (source === undefined)
|
|
132
|
+
continue;
|
|
133
|
+
const file = new ipc_source_1.IpcSourceFile(rel, source);
|
|
134
|
+
for (const statement of source.statements) {
|
|
135
|
+
if (!this.syntax.isApiClass(statement, apiSuffixes))
|
|
136
|
+
continue;
|
|
137
|
+
const contract = this.readContract(file, statement, checker);
|
|
138
|
+
contracts.set(contract.name, contract);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const rows = wireTable === undefined ? undefined : this.readRows(workspaceRoot, program, wireTable, errors);
|
|
142
|
+
return new IpcContractInventory(program, contracts, rows, errors);
|
|
143
|
+
}
|
|
144
|
+
readContract(file, node, checker) {
|
|
145
|
+
const decorator = this.syntax.decoratorCall(file, node, ipc_contract_shape_analyzer_1.IpcContractNames.API_DECORATOR);
|
|
146
|
+
const methods = node.members.filter(ts.isMethodDeclaration).map((member) => {
|
|
147
|
+
const endpoint = this.syntax.decoratorCall(file, member, ipc_contract_shape_analyzer_1.IpcContractNames.ENDPOINT_DECORATOR);
|
|
148
|
+
return new IpcContractMethod(member.name.getText(file.source), endpoint === undefined ? undefined : this.literal(checker, endpoint.arguments[0]), endpoint === undefined ? undefined : this.endpointKind(checker, endpoint), endpoint !== undefined, member);
|
|
149
|
+
});
|
|
150
|
+
const id = decorator === undefined ? undefined : this.literal(checker, decorator.arguments[0]);
|
|
151
|
+
return new IpcDirectionalContract(node.name?.text ?? '', id, file.relFile, node, methods);
|
|
152
|
+
}
|
|
153
|
+
/** The endpoint `kind`, resolved through the checker (a const or enum member counts). */
|
|
154
|
+
endpointKind(checker, call) {
|
|
155
|
+
const options = call.arguments[1];
|
|
156
|
+
if (options === undefined || !ts.isObjectLiteralExpression(options))
|
|
157
|
+
return 'request';
|
|
158
|
+
const kind = options.properties.find((property) => ts.isPropertyAssignment(property) && property.name.getText() === 'kind');
|
|
159
|
+
if (kind === undefined)
|
|
160
|
+
return 'request';
|
|
161
|
+
const value = ts.isPropertyAssignment(kind) ? this.literal(checker, kind.initializer) : undefined;
|
|
162
|
+
return typeof value === 'string' ? value : undefined;
|
|
163
|
+
}
|
|
164
|
+
readRows(workspaceRoot, program, table, errors) {
|
|
165
|
+
const source = program.getSourceFile(path.join(workspaceRoot, table.relFile));
|
|
166
|
+
if (source === undefined) {
|
|
167
|
+
throw new tooling_common_1.InformAiError(`ipc rules: wireTableFile "${table.relFile}" does not exist. Point "wireTableFile" at the file ` +
|
|
168
|
+
'declaring the wire table, or set both "wireTableFile" and "wireTableExport" to null.');
|
|
169
|
+
}
|
|
170
|
+
const declaration = this.findConst(source, table.exportName);
|
|
171
|
+
const initializer = declaration?.initializer;
|
|
172
|
+
if (initializer === undefined || !ts.isArrayLiteralExpression(initializer)) {
|
|
173
|
+
errors.push(`${table.exportName} in ${table.relFile} must be a literal array.`);
|
|
174
|
+
return [];
|
|
175
|
+
}
|
|
176
|
+
const checker = program.getTypeChecker();
|
|
177
|
+
const rows = [];
|
|
178
|
+
for (const entry of initializer.elements) {
|
|
179
|
+
if (!ts.isObjectLiteralExpression(entry)) {
|
|
180
|
+
errors.push(`${table.exportName} entries must be literal objects.`);
|
|
181
|
+
continue;
|
|
182
|
+
}
|
|
183
|
+
rows.push(this.readRow(checker, entry));
|
|
184
|
+
}
|
|
185
|
+
return rows;
|
|
186
|
+
}
|
|
187
|
+
readRow(checker, entry) {
|
|
188
|
+
const values = new Map();
|
|
189
|
+
for (const property of entry.properties) {
|
|
190
|
+
if (ts.isPropertyAssignment(property))
|
|
191
|
+
values.set(property.name.getText(), this.literal(checker, property.initializer));
|
|
192
|
+
}
|
|
193
|
+
return new IpcWireRow(values.get('api'), values.get('method'), values.get('wire'), values.get('direction'), values.get('kind'), values.get('since'));
|
|
194
|
+
}
|
|
195
|
+
findConst(source, name) {
|
|
196
|
+
for (const statement of source.statements) {
|
|
197
|
+
if (!ts.isVariableStatement(statement))
|
|
198
|
+
continue;
|
|
199
|
+
const found = statement.declarationList.declarations.find((declaration) => ts.isIdentifier(declaration.name) && declaration.name.text === name);
|
|
200
|
+
if (found !== undefined)
|
|
201
|
+
return found;
|
|
202
|
+
}
|
|
203
|
+
return undefined;
|
|
204
|
+
}
|
|
205
|
+
literal(checker, node) {
|
|
206
|
+
if (node === undefined)
|
|
207
|
+
return undefined;
|
|
208
|
+
const type = checker.getTypeAtLocation(node);
|
|
209
|
+
if (!type.isLiteral())
|
|
210
|
+
return undefined;
|
|
211
|
+
return typeof type.value === 'object' ? undefined : type.value;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
exports.IpcContractInventoryReader = IpcContractInventoryReader;
|
|
215
|
+
//# sourceMappingURL=ipc-contract-inventory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipc-contract-inventory.js","sourceRoot":"","sources":["../../../../../../packages/tooling/code-rules/src/ipc/ipc-contract-inventory.ts"],"names":[],"mappings":";AAAA;;;;;;;;GAQG;;;;AAEH,mDAA6B;AAC7B,uDAAiC;AACjC,8DAA0D;AAC1D,+EAAoF;AACpF,6CAA6C;AAK7C,6DAA6D;AAC7D,MAAa,iBAAiB;IAEb;IACA;IAEA;IACA;IACA;IANb,YACa,IAAY,EACZ,EAAc;IACvB,0FAA0F;IACjF,IAAwB,EACxB,WAAoB,EACpB,WAAiC;QALjC,SAAI,GAAJ,IAAI,CAAQ;QACZ,OAAE,GAAF,EAAE,CAAY;QAEd,SAAI,GAAJ,IAAI,CAAoB;QACxB,gBAAW,GAAX,WAAW,CAAS;QACpB,gBAAW,GAAX,WAAW,CAAsB;IAC3C,CAAC;CACP;AATD,8CASC;AAED,6FAA6F;AAC7F,MAAa,sBAAsB;IAElB;IAEA;IACA;IACA;IACA;IANb,YACa,IAAY;IACrB,sEAAsE;IAC7D,EAAc,EACd,OAAe,EACf,WAAgC,EAChC,OAAqC;QALrC,SAAI,GAAJ,IAAI,CAAQ;QAEZ,OAAE,GAAF,EAAE,CAAY;QACd,YAAO,GAAP,OAAO,CAAQ;QACf,gBAAW,GAAX,WAAW,CAAqB;QAChC,YAA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I,GAAG,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC;QAC7C,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YAAE,OAAO,SAAS,CAAC;QACxC,OAAO,OAAO,IAAI,CAAC,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC;IACnE,CAAC;CACJ;AA3HD,gEA2HC","sourcesContent":["/**\n * The type-checked read of the IPC contract library (#1389), shared by `ipc-pairing` and\n * `ipc-protocol-fingerprint`. Its `ts.Program` is rooted at the configured contract files ALONE (plus the\n * wire table file), so the program holds the contract library and what it imports — never the endpoint\n * apps, never an unrelated project. `types: []` keeps every ambient `@types/*` package out as well.\n *\n * The checker is what makes a wire table row like `{ wire: AUDIO_PLAY, direction: WireDirectionDto.NATIVE }`\n * readable: both are resolved to their literal values, exactly as the replaced script read them.\n */\n\nimport * as path from 'path';\nimport * as ts from 'typescript';\nimport { InformAiError } from '@webpieces/tooling-common';\nimport { IpcContractNames, IpcContractSyntax } from './ipc-contract-shape-analyzer';\nimport { IpcSourceFile } from './ipc-source';\n\n/** A literal value read from the contract through the checker. */\nexport type IpcLiteral = string | number | undefined;\n\n/** One method of a directional contract class. Data-only. */\nexport class IpcContractMethod {\n constructor(\n readonly name: string,\n readonly id: IpcLiteral,\n /** `request` / `notification`, or undefined when the decorator has a non-literal kind. */\n readonly kind: string | undefined,\n readonly hasEndpoint: boolean,\n readonly declaration: ts.MethodDeclaration,\n ) {}\n}\n\n/** One directional contract class of the contract library (an `…ToNativeApi`). Data-only. */\nexport class IpcDirectionalContract {\n constructor(\n readonly name: string,\n /** The `@WpInternal` id, or undefined when absent / not a literal. */\n readonly id: IpcLiteral,\n readonly relFile: string,\n readonly declaration: ts.ClassDeclaration,\n readonly methods: readonly IpcContractMethod[],\n ) {}\n}\n\n/** One row of the configured wire table, each field resolved to its literal value. Data-only. */\nexport class IpcWireRow {\n constructor(\n readonly api: IpcLiteral,\n readonly method: IpcLiteral,\n readonly wire: IpcLiteral,\n readonly direction: IpcLiteral,\n readonly kind: IpcLiteral,\n readonly since: IpcLiteral,\n ) {}\n}\n\n/** Where the literal wire table lives, when the contract has one. Data-only. */\nexport class IpcWireTableRef {\n constructor(\n readonly relFile: string,\n readonly exportName: string,\n ) {}\n}\n\n/** The wire table a config names, or undefined when it names none; one of the two null is a config error. */\nexport class IpcWireTableConfig {\n of(ruleName: string, file: string | null, exportName: string | null): IpcWireTableRef | undefined {\n if (file === null && exportName === null) return undefined;\n if (file === null || exportName === null) {\n throw new InformAiError(\n `${ruleName}: \"wireTableFile\" and \"wireTableExport\" must both be set or both be null ` +\n `(got ${JSON.stringify(file)} / ${JSON.stringify(exportName)}).`,\n );\n }\n return new IpcWireTableRef(file, exportName);\n }\n}\n\n/** Everything the two project-scoped rules read from the contract library. */\nexport class IpcContractInventory {\n constructor(\n readonly program: ts.Program,\n readonly contracts: ReadonlyMap<string, IpcDirectionalContract>,\n /** Undefined when no wire table is configured. */\n readonly rows: readonly IpcWireRow[] | undefined,\n readonly errors: readonly string[],\n ) {}\n\n get checker(): ts.TypeChecker {\n return this.program.getTypeChecker();\n }\n}\n\nexport class IpcContractInventoryReader {\n private readonly syntax = new IpcContractSyntax();\n\n /**\n * Builds the contract-rooted program and reads its directional contracts and wire table.\n * `contractFiles` are workspace-relative; `wireTable` must name an existing file when given.\n */\n read(\n workspaceRoot: string,\n options: ts.CompilerOptions,\n contractFiles: readonly string[],\n apiSuffixes: readonly string[],\n wireTable: IpcWireTableRef | undefined,\n ): IpcContractInventory {\n const roots = [...contractFiles];\n if (wireTable !== undefined && !roots.includes(wireTable.relFile)) roots.push(wireTable.relFile);\n const program = ts.createProgram(\n roots.map((rel: string) => path.join(workspaceRoot, rel)),\n { ...options, types: [] },\n );\n const checker = program.getTypeChecker();\n const errors: string[] = [];\n const contracts = new Map<string, IpcDirectionalContract>();\n for (const rel of contractFiles) {\n const source = program.getSourceFile(path.join(workspaceRoot, rel));\n if (source === undefined) continue;\n const file = new IpcSourceFile(rel, source);\n for (const statement of source.statements) {\n if (!this.syntax.isApiClass(statement, apiSuffixes)) continue;\n const contract = this.readContract(file, statement, checker);\n contracts.set(contract.name, contract);\n }\n }\n const rows = wireTable === undefined ? undefined : this.readRows(workspaceRoot, program, wireTable, errors);\n return new IpcContractInventory(program, contracts, rows, errors);\n }\n\n private readContract(file: IpcSourceFile, node: ts.ClassDeclaration, checker: ts.TypeChecker): IpcDirectionalContract {\n const decorator = this.syntax.decoratorCall(file, node, IpcContractNames.API_DECORATOR);\n const methods = node.members.filter(ts.isMethodDeclaration).map((member: ts.MethodDeclaration) => {\n const endpoint = this.syntax.decoratorCall(file, member, IpcContractNames.ENDPOINT_DECORATOR);\n return new IpcContractMethod(\n member.name.getText(file.source),\n endpoint === undefined ? undefined : this.literal(checker, endpoint.arguments[0]),\n endpoint === undefined ? undefined : this.endpointKind(checker, endpoint),\n endpoint !== undefined,\n member,\n );\n });\n const id = decorator === undefined ? undefined : this.literal(checker, decorator.arguments[0]);\n return new IpcDirectionalContract(node.name?.text ?? '', id, file.relFile, node, methods);\n }\n\n /** The endpoint `kind`, resolved through the checker (a const or enum member counts). */\n private endpointKind(checker: ts.TypeChecker, call: ts.CallExpression): string | undefined {\n const options = call.arguments[1];\n if (options === undefined || !ts.isObjectLiteralExpression(options)) return 'request';\n const kind = options.properties.find(\n (property: ts.ObjectLiteralElementLike) => ts.isPropertyAssignment(property) && property.name.getText() === 'kind',\n );\n if (kind === undefined) return 'request';\n const value = ts.isPropertyAssignment(kind) ? this.literal(checker, kind.initializer) : undefined;\n return typeof value === 'string' ? value : undefined;\n }\n\n private readRows(workspaceRoot: string, program: ts.Program, table: IpcWireTableRef, errors: string[]): IpcWireRow[] {\n const source = program.getSourceFile(path.join(workspaceRoot, table.relFile));\n if (source === undefined) {\n throw new InformAiError(\n `ipc rules: wireTableFile \"${table.relFile}\" does not exist. Point \"wireTableFile\" at the file ` +\n 'declaring the wire table, or set both \"wireTableFile\" and \"wireTableExport\" to null.',\n );\n }\n const declaration = this.findConst(source, table.exportName);\n const initializer = declaration?.initializer;\n if (initializer === undefined || !ts.isArrayLiteralExpression(initializer)) {\n errors.push(`${table.exportName} in ${table.relFile} must be a literal array.`);\n return [];\n }\n const checker = program.getTypeChecker();\n const rows: IpcWireRow[] = [];\n for (const entry of initializer.elements) {\n if (!ts.isObjectLiteralExpression(entry)) {\n errors.push(`${table.exportName} entries must be literal objects.`);\n continue;\n }\n rows.push(this.readRow(checker, entry));\n }\n return rows;\n }\n\n private readRow(checker: ts.TypeChecker, entry: ts.ObjectLiteralExpression): IpcWireRow {\n const values = new Map<string, IpcLiteral>();\n for (const property of entry.properties) {\n if (ts.isPropertyAssignment(property)) values.set(property.name.getText(), this.literal(checker, property.initializer));\n }\n return new IpcWireRow(\n values.get('api'),\n values.get('method'),\n values.get('wire'),\n values.get('direction'),\n values.get('kind'),\n values.get('since'),\n );\n }\n\n private findConst(source: ts.SourceFile, name: string): ts.VariableDeclaration | undefined {\n for (const statement of source.statements) {\n if (!ts.isVariableStatement(statement)) continue;\n const found = statement.declarationList.declarations.find(\n (declaration: ts.VariableDeclaration) => ts.isIdentifier(declaration.name) && declaration.name.text === name,\n );\n if (found !== undefined) return found;\n }\n return undefined;\n }\n\n private literal(checker: ts.TypeChecker, node: ts.Expression | undefined): IpcLiteral {\n if (node === undefined) return undefined;\n const type = checker.getTypeAtLocation(node);\n if (!type.isLiteral()) return undefined;\n return typeof type.value === 'object' ? undefined : type.value;\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ipc-contract-shape` judgement (#1389): one contract-library file, its syntax tree only. Ports the
|
|
3
|
+
* per-file half of the consumer script's `metadata.mjs` plus the contract-import half of `inspect.mjs`:
|
|
4
|
+
*
|
|
5
|
+
* - a directional API class (name ends in a configured suffix) carries `@WpInternal('<literal id>')`;
|
|
6
|
+
* - every method carries `@WpIpcEndpoint('<literal id>')` (a given `kind` is the literal `request` or
|
|
7
|
+
* `notification`) and an explicit `@MaskLog(...)`; ids are unique in the class, API ids in the file;
|
|
8
|
+
* - every method takes ONE required parameter typed by a named DTO and declares a `Promise<…>` return;
|
|
9
|
+
* - a contract file imports no host/factory package and nothing that resolves into endpoint/runtime code.
|
|
10
|
+
*
|
|
11
|
+
* Decorators are recognised by the name their import binds (`import { WpInternal as Internal }` counts),
|
|
12
|
+
* exactly like the checker-based script, which compared the unaliased symbol name. Cross-file facts — API
|
|
13
|
+
* ids unique across the library, wire-table agreement — are `ipc-pairing`'s.
|
|
14
|
+
*/
|
|
15
|
+
import * as ts from 'typescript';
|
|
16
|
+
import { IpcContractShapeConfig } from '../configs/ipc-configs';
|
|
17
|
+
import { IpcFinding, IpcModuleResolver, IpcSourceFile } from './ipc-source';
|
|
18
|
+
/** The decorator names of the IPC contract surface (`@webpieces/core-util/ipc`). */
|
|
19
|
+
export declare class IpcContractNames {
|
|
20
|
+
static readonly API_DECORATOR = "WpInternal";
|
|
21
|
+
static readonly ENDPOINT_DECORATOR = "WpIpcEndpoint";
|
|
22
|
+
static readonly MASK_DECORATOR = "MaskLog";
|
|
23
|
+
static readonly ENDPOINT_KINDS: readonly string[];
|
|
24
|
+
}
|
|
25
|
+
/** Syntactic reading of contract decorators, shared by the shape and pairing rules. */
|
|
26
|
+
export declare class IpcContractSyntax {
|
|
27
|
+
/** The `@name(...)` call on `node`, matching the name an import binds or the identifier as written. */
|
|
28
|
+
decoratorCall(file: IpcSourceFile, node: ts.HasDecorators, name: string): ts.CallExpression | undefined;
|
|
29
|
+
/** The text of a string literal (or a substitution-free template), else undefined. */
|
|
30
|
+
literal(expr: ts.Expression | undefined): string | undefined;
|
|
31
|
+
/**
|
|
32
|
+
* The endpoint kind: `request` when no options object is given, the literal `kind` when it is, and
|
|
33
|
+
* undefined when a `kind` is present but not a literal (which the shape rule refuses).
|
|
34
|
+
*/
|
|
35
|
+
endpointKind(call: ts.CallExpression): string | undefined;
|
|
36
|
+
isApiClass(node: ts.Node, suffixes: readonly string[]): node is ts.ClassDeclaration;
|
|
37
|
+
private calleeName;
|
|
38
|
+
}
|
|
39
|
+
export declare class IpcContractShapeReasons {
|
|
40
|
+
static readonly HOST_IMPORT = "Shared IPC contracts may import portable schemas/errors, never host or factory implementations.";
|
|
41
|
+
static readonly RUNTIME_IMPORT = "Shared IPC contracts cannot import endpoint/runtime implementations.";
|
|
42
|
+
}
|
|
43
|
+
export declare class IpcContractShapeAnalyzer {
|
|
44
|
+
private readonly config;
|
|
45
|
+
private readonly resolver;
|
|
46
|
+
private readonly syntax;
|
|
47
|
+
constructor(config: IpcContractShapeConfig, resolver: IpcModuleResolver);
|
|
48
|
+
analyze(file: IpcSourceFile): IpcFinding[];
|
|
49
|
+
private judgeImports;
|
|
50
|
+
private judgeApi;
|
|
51
|
+
private judgeMethod;
|
|
52
|
+
private hasOneNamedRequest;
|
|
53
|
+
private returnsPromise;
|
|
54
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* `ipc-contract-shape` judgement (#1389): one contract-library file, its syntax tree only. Ports the
|
|
4
|
+
* per-file half of the consumer script's `metadata.mjs` plus the contract-import half of `inspect.mjs`:
|
|
5
|
+
*
|
|
6
|
+
* - a directional API class (name ends in a configured suffix) carries `@WpInternal('<literal id>')`;
|
|
7
|
+
* - every method carries `@WpIpcEndpoint('<literal id>')` (a given `kind` is the literal `request` or
|
|
8
|
+
* `notification`) and an explicit `@MaskLog(...)`; ids are unique in the class, API ids in the file;
|
|
9
|
+
* - every method takes ONE required parameter typed by a named DTO and declares a `Promise<…>` return;
|
|
10
|
+
* - a contract file imports no host/factory package and nothing that resolves into endpoint/runtime code.
|
|
11
|
+
*
|
|
12
|
+
* Decorators are recognised by the name their import binds (`import { WpInternal as Internal }` counts),
|
|
13
|
+
* exactly like the checker-based script, which compared the unaliased symbol name. Cross-file facts — API
|
|
14
|
+
* ids unique across the library, wire-table agreement — are `ipc-pairing`'s.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.IpcContractShapeAnalyzer = exports.IpcContractShapeReasons = exports.IpcContractSyntax = exports.IpcContractNames = void 0;
|
|
18
|
+
const tslib_1 = require("tslib");
|
|
19
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
20
|
+
const rules_config_1 = require("@webpieces/rules-config");
|
|
21
|
+
/** The decorator names of the IPC contract surface (`@webpieces/core-util/ipc`). */
|
|
22
|
+
class IpcContractNames {
|
|
23
|
+
static API_DECORATOR = 'WpInternal';
|
|
24
|
+
static ENDPOINT_DECORATOR = 'WpIpcEndpoint';
|
|
25
|
+
static MASK_DECORATOR = 'MaskLog';
|
|
26
|
+
static ENDPOINT_KINDS = ['request', 'notification'];
|
|
27
|
+
}
|
|
28
|
+
exports.IpcContractNames = IpcContractNames;
|
|
29
|
+
const C = IpcContractNames;
|
|
30
|
+
/** Syntactic reading of contract decorators, shared by the shape and pairing rules. */
|
|
31
|
+
class IpcContractSyntax {
|
|
32
|
+
/** The `@name(...)` call on `node`, matching the name an import binds or the identifier as written. */
|
|
33
|
+
decoratorCall(file, node, name) {
|
|
34
|
+
for (const decorator of ts.getDecorators(node) ?? []) {
|
|
35
|
+
const expression = decorator.expression;
|
|
36
|
+
if (!ts.isCallExpression(expression))
|
|
37
|
+
continue;
|
|
38
|
+
if (this.calleeName(file, expression.expression) === name)
|
|
39
|
+
return expression;
|
|
40
|
+
}
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
/** The text of a string literal (or a substitution-free template), else undefined. */
|
|
44
|
+
literal(expr) {
|
|
45
|
+
return expr !== undefined && ts.isStringLiteralLike(expr) ? expr.text : undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* The endpoint kind: `request` when no options object is given, the literal `kind` when it is, and
|
|
49
|
+
* undefined when a `kind` is present but not a literal (which the shape rule refuses).
|
|
50
|
+
*/
|
|
51
|
+
endpointKind(call) {
|
|
52
|
+
const options = call.arguments[1];
|
|
53
|
+
if (options === undefined || !ts.isObjectLiteralExpression(options))
|
|
54
|
+
return 'request';
|
|
55
|
+
const kind = options.properties.find((property) => ts.isPropertyAssignment(property) && property.name.getText() === 'kind');
|
|
56
|
+
if (kind === undefined)
|
|
57
|
+
return 'request';
|
|
58
|
+
return ts.isPropertyAssignment(kind) ? this.literal(kind.initializer) : undefined;
|
|
59
|
+
}
|
|
60
|
+
isApiClass(node, suffixes) {
|
|
61
|
+
if (!ts.isClassDeclaration(node) || node.name === undefined)
|
|
62
|
+
return false;
|
|
63
|
+
const name = node.name.text;
|
|
64
|
+
return suffixes.some((suffix) => name.endsWith(suffix));
|
|
65
|
+
}
|
|
66
|
+
calleeName(file, callee) {
|
|
67
|
+
if (ts.isIdentifier(callee))
|
|
68
|
+
return file.importOf(callee.text)?.imported ?? callee.text;
|
|
69
|
+
if (ts.isPropertyAccessExpression(callee))
|
|
70
|
+
return callee.name.text;
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
exports.IpcContractSyntax = IpcContractSyntax;
|
|
75
|
+
class IpcContractShapeReasons {
|
|
76
|
+
static HOST_IMPORT = 'Shared IPC contracts may import portable schemas/errors, never host or factory implementations.';
|
|
77
|
+
static RUNTIME_IMPORT = 'Shared IPC contracts cannot import endpoint/runtime implementations.';
|
|
78
|
+
}
|
|
79
|
+
exports.IpcContractShapeReasons = IpcContractShapeReasons;
|
|
80
|
+
class IpcContractShapeAnalyzer {
|
|
81
|
+
config;
|
|
82
|
+
resolver;
|
|
83
|
+
syntax = new IpcContractSyntax();
|
|
84
|
+
constructor(config, resolver) {
|
|
85
|
+
this.config = config;
|
|
86
|
+
this.resolver = resolver;
|
|
87
|
+
}
|
|
88
|
+
analyze(file) {
|
|
89
|
+
const out = [...this.judgeImports(file)];
|
|
90
|
+
const apiIds = new Map();
|
|
91
|
+
for (const statement of file.source.statements) {
|
|
92
|
+
if (!this.syntax.isApiClass(statement, this.config.apiClassSuffixes))
|
|
93
|
+
continue;
|
|
94
|
+
out.push(...this.judgeApi(file, statement, apiIds));
|
|
95
|
+
}
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
judgeImports(file) {
|
|
99
|
+
const out = [];
|
|
100
|
+
for (const statement of file.source.statements) {
|
|
101
|
+
const declaration = ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement) ? statement : undefined;
|
|
102
|
+
const specifier = declaration?.moduleSpecifier;
|
|
103
|
+
if (declaration === undefined || specifier === undefined || !ts.isStringLiteral(specifier))
|
|
104
|
+
continue;
|
|
105
|
+
if ((0, rules_config_1.matchesAnyGlob)(specifier.text, this.config.forbiddenImportModules)) {
|
|
106
|
+
out.push(file.finding(declaration, IpcContractShapeReasons.HOST_IMPORT));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const target = this.resolver.resolve(file.relFile, specifier.text);
|
|
110
|
+
if (target !== undefined && (0, rules_config_1.matchesAnyGlob)(target, this.config.forbiddenImportPaths)) {
|
|
111
|
+
out.push(file.finding(declaration, IpcContractShapeReasons.RUNTIME_IMPORT));
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
judgeApi(file, api, apiIds) {
|
|
117
|
+
const name = api.name?.text ?? '<anonymous>';
|
|
118
|
+
const decorator = this.syntax.decoratorCall(file, api, C.API_DECORATOR);
|
|
119
|
+
const out = [];
|
|
120
|
+
if (decorator === undefined) {
|
|
121
|
+
out.push(file.finding(api.name ?? api, `IPC ${name} needs @WpInternal('<api-id>') metadata.`));
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
const id = this.syntax.literal(decorator.arguments[0]);
|
|
125
|
+
const clash = id === undefined ? undefined : apiIds.get(id);
|
|
126
|
+
if (id === undefined || clash !== undefined) {
|
|
127
|
+
out.push(file.finding(decorator, `IPC ${name} needs one uniquely identified @WpInternal id${clash ? ` (also used by ${clash})` : ''}.`));
|
|
128
|
+
}
|
|
129
|
+
if (id !== undefined && clash === undefined)
|
|
130
|
+
apiIds.set(id, name);
|
|
131
|
+
}
|
|
132
|
+
const methodIds = new Set();
|
|
133
|
+
for (const member of api.members) {
|
|
134
|
+
if (!ts.isMethodDeclaration(member))
|
|
135
|
+
continue;
|
|
136
|
+
out.push(...this.judgeMethod(file, name, member, methodIds));
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
judgeMethod(file, api, method, methodIds) {
|
|
141
|
+
const name = method.name.getText(file.source);
|
|
142
|
+
const label = `IPC ${api}.${name}`;
|
|
143
|
+
const out = [];
|
|
144
|
+
const endpoint = this.syntax.decoratorCall(file, method, C.ENDPOINT_DECORATOR);
|
|
145
|
+
if (endpoint === undefined) {
|
|
146
|
+
out.push(file.finding(method.name, `${label} needs @WpIpcEndpoint('<method-id>') metadata.`));
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const id = this.syntax.literal(endpoint.arguments[0]);
|
|
150
|
+
if (id === undefined || methodIds.has(id)) {
|
|
151
|
+
out.push(file.finding(endpoint, `${label} requires one uniquely identified @WpIpcEndpoint id.`));
|
|
152
|
+
}
|
|
153
|
+
if (id !== undefined)
|
|
154
|
+
methodIds.add(id);
|
|
155
|
+
const kind = this.syntax.endpointKind(endpoint);
|
|
156
|
+
if (kind === undefined || !C.ENDPOINT_KINDS.includes(kind)) {
|
|
157
|
+
out.push(file.finding(endpoint, `${label} @WpIpcEndpoint kind must be the literal 'request' or 'notification'.`));
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (this.syntax.decoratorCall(file, method, C.MASK_DECORATOR) === undefined) {
|
|
161
|
+
out.push(file.finding(method.name, `${label} needs explicit @MaskLog metadata.`));
|
|
162
|
+
}
|
|
163
|
+
if (!this.hasOneNamedRequest(method) || !this.returnsPromise(method)) {
|
|
164
|
+
out.push(file.finding(method.name, `${label} must take one named required DTO and return Promise.`));
|
|
165
|
+
}
|
|
166
|
+
return out;
|
|
167
|
+
}
|
|
168
|
+
hasOneNamedRequest(method) {
|
|
169
|
+
if (method.parameters.length !== 1)
|
|
170
|
+
return false;
|
|
171
|
+
const parameter = method.parameters[0];
|
|
172
|
+
if (parameter.questionToken !== undefined || parameter.initializer !== undefined || parameter.dotDotDotToken !== undefined) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
return parameter.type !== undefined && ts.isTypeReferenceNode(parameter.type);
|
|
176
|
+
}
|
|
177
|
+
returnsPromise(method) {
|
|
178
|
+
const type = method.type;
|
|
179
|
+
if (type === undefined || !ts.isTypeReferenceNode(type))
|
|
180
|
+
return false;
|
|
181
|
+
const name = ts.isIdentifier(type.typeName) ? type.typeName.text : type.typeName.right.text;
|
|
182
|
+
return name === 'Promise';
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
exports.IpcContractShapeAnalyzer = IpcContractShapeAnalyzer;
|
|
186
|
+
//# sourceMappingURL=ipc-contract-shape-analyzer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ipc-contract-shape-analyzer.js","sourceRoot":"","sources":["../../../../../../packages/tooling/code-rules/src/ipc/ipc-contract-shape-analyzer.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;GAaG;;;;AAEH,uDAAiC;AACjC,0DAAyD;AAIzD,oFAAoF;AACpF,MAAa,gBAAgB;IACzB,MAAM,CAAU,aAAa,GAAG,YAAY,CAAC;IAC7C,MAAM,CAAU,kBAAkB,GAAG,eAAe,CAAC;IACrD,MAAM,CAAU,cAAc,GAAG,SAAS,CAAC;IAC3C,MAAM,CAAU,cAAc,GAAsB,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;;AAJpF,4CAKC;AAED,MAAM,CAAC,GAAG,gBAAgB,CAAC;AAE3B,uFAAuF;AACvF,MAAa,iBAAiB;IAC1B,uGAAuG;IACvG,aAAa,CAAC,IAAmB,EA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AAC,IAAI,KAAK,SAAS,IAAI,EAAE,CAAC,mBAAmB,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;IAClF,CAAC;IAEO,cAAc,CAAC,MAA4B;QAC/C,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC;QACzB,IAAI,IAAI,KAAK,SAAS,IAAI,CAAC,EAAE,CAAC,mBAAmB,CAAC,IAAI,CAAC;YAAE,OAAO,KAAK,CAAC;QACtE,MAAM,IAAI,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC;QAC5F,OAAO,IAAI,KAAK,SAAS,CAAC;IAC9B,CAAC;CACJ;AApGD,4DAoGC","sourcesContent":["/**\n * `ipc-contract-shape` judgement (#1389): one contract-library file, its syntax tree only. Ports the\n * per-file half of the consumer script's `metadata.mjs` plus the contract-import half of `inspect.mjs`:\n *\n * - a directional API class (name ends in a configured suffix) carries `@WpInternal('<literal id>')`;\n * - every method carries `@WpIpcEndpoint('<literal id>')` (a given `kind` is the literal `request` or\n * `notification`) and an explicit `@MaskLog(...)`; ids are unique in the class, API ids in the file;\n * - every method takes ONE required parameter typed by a named DTO and declares a `Promise<…>` return;\n * - a contract file imports no host/factory package and nothing that resolves into endpoint/runtime code.\n *\n * Decorators are recognised by the name their import binds (`import { WpInternal as Internal }` counts),\n * exactly like the checker-based script, which compared the unaliased symbol name. Cross-file facts — API\n * ids unique across the library, wire-table agreement — are `ipc-pairing`'s.\n */\n\nimport * as ts from 'typescript';\nimport { matchesAnyGlob } from '@webpieces/rules-config';\nimport { IpcContractShapeConfig } from '../configs/ipc-configs';\nimport { IpcFinding, IpcModuleResolver, IpcSourceFile } from './ipc-source';\n\n/** The decorator names of the IPC contract surface (`@webpieces/core-util/ipc`). */\nexport class IpcContractNames {\n static readonly API_DECORATOR = 'WpInternal';\n static readonly ENDPOINT_DECORATOR = 'WpIpcEndpoint';\n static readonly MASK_DECORATOR = 'MaskLog';\n static readonly ENDPOINT_KINDS: readonly string[] = ['request', 'notification'];\n}\n\nconst C = IpcContractNames;\n\n/** Syntactic reading of contract decorators, shared by the shape and pairing rules. */\nexport class IpcContractSyntax {\n /** The `@name(...)` call on `node`, matching the name an import binds or the identifier as written. */\n decoratorCall(file: IpcSourceFile, node: ts.HasDecorators, name: string): ts.CallExpression | undefined {\n for (const decorator of ts.getDecorators(node) ?? []) {\n const expression = decorator.expression;\n if (!ts.isCallExpression(expression)) continue;\n if (this.calleeName(file, expression.expression) === name) return expression;\n }\n return undefined;\n }\n\n /** The text of a string literal (or a substitution-free template), else undefined. */\n literal(expr: ts.Expression | undefined): string | undefined {\n return expr !== undefined && ts.isStringLiteralLike(expr) ? expr.text : undefined;\n }\n\n /**\n * The endpoint kind: `request` when no options object is given, the literal `kind` when it is, and\n * undefined when a `kind` is present but not a literal (which the shape rule refuses).\n */\n endpointKind(call: ts.CallExpression): string | undefined {\n const options = call.arguments[1];\n if (options === undefined || !ts.isObjectLiteralExpression(options)) return 'request';\n const kind = options.properties.find(\n (property: ts.ObjectLiteralElementLike) => ts.isPropertyAssignment(property) && property.name.getText() === 'kind',\n );\n if (kind === undefined) return 'request';\n return ts.isPropertyAssignment(kind) ? this.literal(kind.initializer) : undefined;\n }\n\n isApiClass(node: ts.Node, suffixes: readonly string[]): node is ts.ClassDeclaration {\n if (!ts.isClassDeclaration(node) || node.name === undefined) return false;\n const name = node.name.text;\n return suffixes.some((suffix: string) => name.endsWith(suffix));\n }\n\n private calleeName(file: IpcSourceFile, callee: ts.Expression): string | undefined {\n if (ts.isIdentifier(callee)) return file.importOf(callee.text)?.imported ?? callee.text;\n if (ts.isPropertyAccessExpression(callee)) return callee.name.text;\n return undefined;\n }\n}\n\nexport class IpcContractShapeReasons {\n static readonly HOST_IMPORT = 'Shared IPC contracts may import portable schemas/errors, never host or factory implementations.';\n static readonly RUNTIME_IMPORT = 'Shared IPC contracts cannot import endpoint/runtime implementations.';\n}\n\nexport class IpcContractShapeAnalyzer {\n private readonly syntax = new IpcContractSyntax();\n\n constructor(\n private readonly config: IpcContractShapeConfig,\n private readonly resolver: IpcModuleResolver,\n ) {}\n\n analyze(file: IpcSourceFile): IpcFinding[] {\n const out: IpcFinding[] = [...this.judgeImports(file)];\n const apiIds = new Map<string, string>();\n for (const statement of file.source.statements) {\n if (!this.syntax.isApiClass(statement, this.config.apiClassSuffixes)) continue;\n out.push(...this.judgeApi(file, statement, apiIds));\n }\n return out;\n }\n\n private judgeImports(file: IpcSourceFile): IpcFinding[] {\n const out: IpcFinding[] = [];\n for (const statement of file.source.statements) {\n const declaration = ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement) ? statement : undefined;\n const specifier = declaration?.moduleSpecifier;\n if (declaration === undefined || specifier === undefined || !ts.isStringLiteral(specifier)) continue;\n if (matchesAnyGlob(specifier.text, this.config.forbiddenImportModules)) {\n out.push(file.finding(declaration, IpcContractShapeReasons.HOST_IMPORT));\n continue;\n }\n const target = this.resolver.resolve(file.relFile, specifier.text);\n if (target !== undefined && matchesAnyGlob(target, this.config.forbiddenImportPaths)) {\n out.push(file.finding(declaration, IpcContractShapeReasons.RUNTIME_IMPORT));\n }\n }\n return out;\n }\n\n private judgeApi(file: IpcSourceFile, api: ts.ClassDeclaration, apiIds: Map<string, string>): IpcFinding[] {\n const name = api.name?.text ?? '<anonymous>';\n const decorator = this.syntax.decoratorCall(file, api, C.API_DECORATOR);\n const out: IpcFinding[] = [];\n if (decorator === undefined) {\n out.push(file.finding(api.name ?? api, `IPC ${name} needs @WpInternal('<api-id>') metadata.`));\n } else {\n const id = this.syntax.literal(decorator.arguments[0]);\n const clash = id === undefined ? undefined : apiIds.get(id);\n if (id === undefined || clash !== undefined) {\n out.push(file.finding(decorator, `IPC ${name} needs one uniquely identified @WpInternal id${clash ? ` (also used by ${clash})` : ''}.`));\n }\n if (id !== undefined && clash === undefined) apiIds.set(id, name);\n }\n const methodIds = new Set<string>();\n for (const member of api.members) {\n if (!ts.isMethodDeclaration(member)) continue;\n out.push(...this.judgeMethod(file, name, member, methodIds));\n }\n return out;\n }\n\n private judgeMethod(file: IpcSourceFile, api: string, method: ts.MethodDeclaration, methodIds: Set<string>): IpcFinding[] {\n const name = method.name.getText(file.source);\n const label = `IPC ${api}.${name}`;\n const out: IpcFinding[] = [];\n const endpoint = this.syntax.decoratorCall(file, method, C.ENDPOINT_DECORATOR);\n if (endpoint === undefined) {\n out.push(file.finding(method.name, `${label} needs @WpIpcEndpoint('<method-id>') metadata.`));\n } else {\n const id = this.syntax.literal(endpoint.arguments[0]);\n if (id === undefined || methodIds.has(id)) {\n out.push(file.finding(endpoint, `${label} requires one uniquely identified @WpIpcEndpoint id.`));\n }\n if (id !== undefined) methodIds.add(id);\n const kind = this.syntax.endpointKind(endpoint);\n if (kind === undefined || !C.ENDPOINT_KINDS.includes(kind)) {\n out.push(file.finding(endpoint, `${label} @WpIpcEndpoint kind must be the literal 'request' or 'notification'.`));\n }\n }\n if (this.syntax.decoratorCall(file, method, C.MASK_DECORATOR) === undefined) {\n out.push(file.finding(method.name, `${label} needs explicit @MaskLog metadata.`));\n }\n if (!this.hasOneNamedRequest(method) || !this.returnsPromise(method)) {\n out.push(file.finding(method.name, `${label} must take one named required DTO and return Promise.`));\n }\n return out;\n }\n\n private hasOneNamedRequest(method: ts.MethodDeclaration): boolean {\n if (method.parameters.length !== 1) return false;\n const parameter = method.parameters[0];\n if (parameter.questionToken !== undefined || parameter.initializer !== undefined || parameter.dotDotDotToken !== undefined) {\n return false;\n }\n return parameter.type !== undefined && ts.isTypeReferenceNode(parameter.type);\n }\n\n private returnsPromise(method: ts.MethodDeclaration): boolean {\n const type = method.type;\n if (type === undefined || !ts.isTypeReferenceNode(type)) return false;\n const name = ts.isIdentifier(type.typeName) ? type.typeName.text : type.typeName.right.text;\n return name === 'Promise';\n }\n}\n"]}
|