@cplieger/deadset-ts 8.0.0-dev.14 → 8.0.0-dev.15

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.
Files changed (46) hide show
  1. package/dist/src/alias-chain.js +2 -2
  2. package/dist/src/analysis.js +2 -1
  3. package/dist/src/conformance-record.js +1 -1
  4. package/dist/src/consumers.js +1 -1
  5. package/dist/src/contract/contract.generated.js +1 -0
  6. package/dist/src/dependencies.js +2 -1
  7. package/dist/src/entry-points.js +26 -95
  8. package/dist/src/file-facts.js +1 -1
  9. package/dist/src/graph.js +0 -1
  10. package/dist/src/inventory/commonjs.js +462 -0
  11. package/dist/src/inventory/enumerate.js +166 -0
  12. package/dist/src/inventory/syntax.js +236 -0
  13. package/dist/src/inventory.js +305 -705
  14. package/dist/src/load/discover.js +1 -1
  15. package/dist/src/load/package-scope.js +56 -0
  16. package/dist/src/own-body.js +28 -0
  17. package/dist/src/position.js +41 -0
  18. package/dist/src/ref.js +11 -20
  19. package/dist/src/references.js +91 -30
  20. package/dist/src/session.js +3 -0
  21. package/dist/src/suppress-inline.js +33 -81
  22. package/dist/src/unseen-loads.js +106 -17
  23. package/jsr.json +1 -1
  24. package/package.json +2 -2
  25. package/src/alias-chain.ts +2 -2
  26. package/src/analysis.ts +2 -1
  27. package/src/conformance-record.ts +1 -1
  28. package/src/consumers.ts +1 -1
  29. package/src/contract/contract.generated.ts +1 -0
  30. package/src/dependencies.ts +2 -1
  31. package/src/entry-points.ts +30 -107
  32. package/src/file-facts.ts +1 -1
  33. package/src/graph.ts +0 -1
  34. package/src/inventory/commonjs.ts +611 -0
  35. package/src/inventory/enumerate.ts +202 -0
  36. package/src/inventory/syntax.ts +311 -0
  37. package/src/inventory.ts +424 -878
  38. package/src/load/discover.ts +1 -1
  39. package/src/load/package-scope.ts +74 -0
  40. package/src/own-body.ts +42 -0
  41. package/src/position.ts +42 -0
  42. package/src/ref.ts +11 -20
  43. package/src/references.ts +116 -53
  44. package/src/session.ts +17 -1
  45. package/src/suppress-inline.ts +53 -89
  46. package/src/unseen-loads.ts +166 -18
@@ -0,0 +1,202 @@
1
+ import { isShorthandPropertyAssignment, ModifierFlags, type Node } from "@typescript/native/ast";
2
+ import type { Symbol as TSSymbol } from "@typescript/native/sync";
3
+ import type { Host } from "../host.ts";
4
+ import {
5
+ Builder,
6
+ modifiersOf,
7
+ type Building,
8
+ type Container,
9
+ type DeclarationSource,
10
+ type Inventory,
11
+ type InventoryCost,
12
+ } from "../inventory.ts";
13
+ import { packageScope, packageTypeIsModule } from "../load/package-scope.ts";
14
+ import { relativePath } from "../paths.ts";
15
+ import { renderPosition } from "../position.ts";
16
+ import { isAnswered, UNANSWERED } from "../query.ts";
17
+ import type { Handle, ProjectView } from "../session.ts";
18
+ import { commonJS, type Resolved } from "./commonjs.ts";
19
+ import { SYNTAX } from "./syntax.ts";
20
+
21
+ /**
22
+ * Every declaration one project's own files hold, in rendered position order.
23
+ *
24
+ * `targetRoot` is the absolute path every position is rendered against; a file the program
25
+ * holds that is not below it ends the enumeration. `host` is the filesystem the manifests
26
+ * that name each file's package are read through, and nothing else is read from it.
27
+ */
28
+ export function inventory<Brand>(
29
+ project: ProjectView<Brand>,
30
+ host: Host,
31
+ targetRoot: string,
32
+ ): Inventory {
33
+ const files = project.ownSourceFiles();
34
+ const moduleOf = packageScope(host, targetRoot);
35
+ const sources: readonly DeclarationSource[] = [
36
+ SYNTAX,
37
+ commonJS(packageTypeIsModule(host, targetRoot), resolvedIn(project)),
38
+ ];
39
+ const into = new Builder(targetRoot);
40
+ for (const file of files) {
41
+ const path = relativePath(targetRoot, file.fileName);
42
+ if (path === undefined) {
43
+ // renderPosition states the refusal, with the root it was rendered against.
44
+ renderPosition(file, targetRoot, 0);
45
+ continue;
46
+ }
47
+ const owner = into.file(file, moduleOf(path), path);
48
+ for (const source of sources) {
49
+ source.declare(file, owner, into);
50
+ }
51
+ }
52
+ return into.build(readTables(project, into, new Set(files.map((file) => file.fileName))));
53
+ }
54
+
55
+ /** The declarations of this program each occurrence resolves to, asked in one batch. */
56
+ function resolvedIn<Brand>(project: ProjectView<Brand>): Resolved {
57
+ return (occurrences) => {
58
+ const symbols = project.symbolsAt(occurrences.map((occurrence) => project.handle(occurrence)));
59
+ return occurrences.map((occurrence, index) => {
60
+ // A shorthand property's name resolves to the property it declares, not the binding.
61
+ const symbol = isShorthandPropertyAssignment(occurrence.parent)
62
+ ? project.shorthandValueAt(project.handle(occurrence.parent))
63
+ : symbols[index];
64
+ return typeof symbol === "object"
65
+ ? symbol.declarations.flatMap(
66
+ (declaration) => project.declarationAt(declaration)?.node ?? [],
67
+ )
68
+ : [];
69
+ });
70
+ };
71
+ }
72
+
73
+ /**
74
+ * The binder's half: which declarations each module and namespace exports, and the members
75
+ * each container's symbol tables hold, then the members only its tree holds. One batch
76
+ * resolves every scope and container, because an accessor with an array overload costs one
77
+ * round trip for the batch and one per node without it.
78
+ */
79
+ function readTables<Brand>(
80
+ project: ProjectView<Brand>,
81
+ into: Builder,
82
+ ownFiles: ReadonlySet<string>,
83
+ ): InventoryCost {
84
+ const scopes = into.exportScopes;
85
+ const named = into.containers.filter((container) => container.nameNode !== undefined);
86
+ const batched: Node[] = [
87
+ ...scopes.map((scope) => scope.nameNode),
88
+ ...named.flatMap((container) => container.nameNode ?? []),
89
+ ];
90
+ const handles: Handle<Brand>[] = batched.map((node) => project.handle(node));
91
+ const resolved = project.symbolsAt(handles);
92
+ const symbolOf = new Map<Node, TSSymbol>();
93
+ batched.forEach((node, index) => {
94
+ const symbol = resolved[index];
95
+ if (typeof symbol === "object") {
96
+ symbolOf.set(node, symbol);
97
+ }
98
+ });
99
+ // A scope or a container whose symbol or table went unanswered keeps what its syntax
100
+ // declares, and everything under it is recorded unanswered.
101
+ scopes.forEach((scope, index) => {
102
+ if (resolved[index] === UNANSWERED) {
103
+ into.unanswered(scope.owner);
104
+ }
105
+ });
106
+ named.forEach((container, index) => {
107
+ if (resolved[scopes.length + index] === UNANSWERED) {
108
+ into.unanswered(container.owner);
109
+ }
110
+ });
111
+
112
+ let exportTables = 0;
113
+ for (const scope of scopes) {
114
+ const symbol = symbolOf.get(scope.nameNode);
115
+ if (symbol === undefined) {
116
+ continue;
117
+ }
118
+ exportTables += 1;
119
+ const table = project.queries.exportsOf(symbol);
120
+ if (!isAnswered(table)) {
121
+ into.unanswered(scope.owner);
122
+ continue;
123
+ }
124
+ for (const [, exported] of table) {
125
+ for (const declaration of exported.declarations) {
126
+ const held = project.declarationAt(declaration);
127
+ const record = held === undefined ? undefined : into.recordAt(held.node);
128
+ if (record !== undefined) {
129
+ record.exported = true;
130
+ }
131
+ }
132
+ }
133
+ }
134
+
135
+ let memberTables = 0;
136
+ const readTable = (
137
+ container: Container,
138
+ table: ReadonlyMap<string, TSSymbol>,
139
+ seen: Map<string, Building>,
140
+ areStatic: boolean,
141
+ ): void => {
142
+ for (const [, member] of table) {
143
+ const declarations = member.declarations.flatMap(
144
+ (handle) => project.declarationAt(handle) ?? [],
145
+ );
146
+ // A member with no declaration is the language's own (a class's `prototype`), and a
147
+ // member declared outside the target's own files belongs to another program. A
148
+ // merged symbol's table holds the members every one of its declarations writes, and
149
+ // each member is kept once, by the declaration that writes it.
150
+ for (const held of declarations) {
151
+ const node = held.node;
152
+ if (
153
+ ownFiles.has(node.getSourceFile().fileName) &&
154
+ writtenInside(container.declaration, node)
155
+ ) {
156
+ into.member(container, node, seen, areStatic);
157
+ }
158
+ }
159
+ }
160
+ };
161
+ for (const container of into.containers) {
162
+ const symbol = container.nameNode === undefined ? undefined : symbolOf.get(container.nameNode);
163
+ const seen = new Map<string, Building>();
164
+ const isClass = container.memberKind === "class-member";
165
+ const isEnum = container.memberKind === "enum-member";
166
+ if (symbol !== undefined) {
167
+ if (!isEnum) {
168
+ memberTables += 1;
169
+ const table = project.queries.membersOf(symbol);
170
+ if (isAnswered(table)) {
171
+ readTable(container, table, seen, false);
172
+ } else {
173
+ into.unanswered(container.owner);
174
+ }
175
+ }
176
+ if (isEnum || isClass) {
177
+ memberTables += 1;
178
+ const table = project.queries.exportsOf(symbol);
179
+ if (isAnswered(table)) {
180
+ readTable(container, table, seen, isClass);
181
+ } else {
182
+ into.unanswered(container.owner);
183
+ }
184
+ }
185
+ }
186
+ // A member whose key is computed is bound to the container's type later than the
187
+ // table is built, so only the container's own declaration names it.
188
+ for (const member of container.members) {
189
+ into.member(container, member, seen, (modifiersOf(member) & ModifierFlags.Static) !== 0);
190
+ }
191
+ }
192
+ return { batches: 1, exportTables, memberTables };
193
+ }
194
+
195
+ /** Whether `node` is written inside `declaration`, compared by file and range. */
196
+ function writtenInside(declaration: Node, node: Node): boolean {
197
+ return (
198
+ node.getSourceFile().fileName === declaration.getSourceFile().fileName &&
199
+ declaration.pos <= node.pos &&
200
+ node.end <= declaration.end
201
+ );
202
+ }
@@ -0,0 +1,311 @@
1
+ import {
2
+ isArrayBindingPattern,
3
+ isBindingElement,
4
+ isClassDeclaration,
5
+ isConstructorDeclaration,
6
+ isEnumDeclaration,
7
+ isExportAssignment,
8
+ isExportDeclaration,
9
+ isFunctionDeclaration,
10
+ isIdentifier,
11
+ isInterfaceDeclaration,
12
+ isIntersectionTypeNode,
13
+ isModuleBlock,
14
+ isModuleDeclaration,
15
+ isNamespaceExport,
16
+ isObjectBindingPattern,
17
+ isParenthesizedTypeNode,
18
+ isStringLiteral,
19
+ isTypeAliasDeclaration,
20
+ isTypeLiteralNode,
21
+ isUnionTypeNode,
22
+ isVariableStatement,
23
+ SyntaxKind,
24
+ type BindingElement,
25
+ type ClassDeclaration,
26
+ type Identifier,
27
+ type Node,
28
+ type SourceFile,
29
+ } from "@typescript/native/ast";
30
+ import {
31
+ componentOf,
32
+ exportedAs,
33
+ isParameterProperty,
34
+ memberKey,
35
+ nameNodeOf,
36
+ type Builder,
37
+ type Building,
38
+ type Container,
39
+ type DeclarationSource,
40
+ type SymbolKind,
41
+ } from "../inventory.ts";
42
+ import { writtenLength } from "../position.ts";
43
+
44
+ /** What one statement form declares under `owner`; `walk` reads a statement it holds. */
45
+ type Arm = (node: Node, owner: Building, into: Builder, walk: Walk) => void;
46
+ type Walk = (statement: Node, owner: Building) => void;
47
+
48
+ /** An arm for one statement form, which reads only a node the guard admits. */
49
+ function arm<T extends Node>(
50
+ guard: (node: Node) => node is T,
51
+ declare: (node: T, owner: Building, into: Builder, walk: Walk) => void,
52
+ ): Arm {
53
+ return (node, owner, into, walk) => {
54
+ if (guard(node)) {
55
+ declare(node, owner, into, walk);
56
+ }
57
+ };
58
+ }
59
+
60
+ /** The members a named declaration's container holds, where it holds any. */
61
+ type Members<T> = (node: T) => Omit<Container, "owner" | "declaration"> | undefined;
62
+
63
+ /** A declaration named by its own name, or `default` as a default export, and its members. */
64
+ function named<T extends Node>(
65
+ guard: (node: Node) => node is T,
66
+ kind: SymbolKind,
67
+ members?: Members<T>,
68
+ ): Arm {
69
+ return arm(guard, (node, owner, into) => {
70
+ const name = nameNodeOf(node);
71
+ const { component, localName } = exportedAs(node, name);
72
+ if (component === undefined) {
73
+ return;
74
+ }
75
+ const record = into.declare(node, owner, kind, name, { component, localName });
76
+ const held = members?.(node);
77
+ if (held !== undefined) {
78
+ into.container({ owner: record, declaration: node, ...held });
79
+ }
80
+ });
81
+ }
82
+
83
+ /** The parameter properties every constructor of one class declares. */
84
+ function parameterPropertiesOf(node: ClassDeclaration): readonly Node[] {
85
+ return node.members.flatMap((member) =>
86
+ isConstructorDeclaration(member) ? member.parameters.filter(isParameterProperty) : [],
87
+ );
88
+ }
89
+
90
+ // A type alias keeps no member table of its own: the members belong to the object types
91
+ // its declaration writes, so the declaration is what carries them.
92
+ const TYPE_ALIAS = named(isTypeAliasDeclaration, "type", (node) => {
93
+ const members = aliasMembers(node.type);
94
+ return members.length === 0
95
+ ? undefined
96
+ : {
97
+ nameNode: undefined,
98
+ members,
99
+ memberKind: "type-member",
100
+ };
101
+ });
102
+
103
+ /** The statement forms that declare, each with what it declares. */
104
+ const ARMS: ReadonlyMap<SyntaxKind, Arm> = new Map([
105
+ [SyntaxKind.FunctionDeclaration, named(isFunctionDeclaration, "function")],
106
+ [
107
+ SyntaxKind.ClassDeclaration,
108
+ named(isClassDeclaration, "class", (node) => ({
109
+ nameNode: node.name,
110
+ members: [...node.members, ...parameterPropertiesOf(node)],
111
+ memberKind: "class-member",
112
+ })),
113
+ ],
114
+ [
115
+ SyntaxKind.InterfaceDeclaration,
116
+ named(isInterfaceDeclaration, "interface", (node) => ({
117
+ nameNode: node.name,
118
+ members: node.members,
119
+ memberKind: "interface-method",
120
+ })),
121
+ ],
122
+ [SyntaxKind.TypeAliasDeclaration, TYPE_ALIAS],
123
+ // A JavaScript file's `@typedef` is reparsed into this declaration.
124
+ [SyntaxKind.JSTypeAliasDeclaration, TYPE_ALIAS],
125
+ [
126
+ SyntaxKind.EnumDeclaration,
127
+ named(isEnumDeclaration, "enum", (node) => ({
128
+ nameNode: node.name,
129
+ members: node.members,
130
+ memberKind: "enum-member",
131
+ })),
132
+ ],
133
+ [
134
+ SyntaxKind.ModuleDeclaration,
135
+ arm(isModuleDeclaration, (node, owner, into, walk) => {
136
+ const component = componentOf(node.name);
137
+ if (component === undefined) {
138
+ return;
139
+ }
140
+ const record = into.declare(node, owner, "namespace", node.name, {
141
+ component,
142
+ localName: undefined,
143
+ });
144
+ into.exportScope(record, node.name);
145
+ if (isStringLiteral(node.name)) {
146
+ into.module(record, node.name.text);
147
+ }
148
+ const body = node.body;
149
+ if (body === undefined) {
150
+ return;
151
+ }
152
+ for (const statement of isModuleBlock(body) ? body.statements : [body]) {
153
+ walk(statement, record);
154
+ }
155
+ }),
156
+ ],
157
+ [
158
+ SyntaxKind.VariableStatement,
159
+ arm(isVariableStatement, (node, owner, into) => {
160
+ for (const declaration of node.declarationList.declarations) {
161
+ const component = componentOf(declaration.name);
162
+ if (component !== undefined) {
163
+ into.declare(declaration, owner, "variable", declaration.name, {
164
+ component,
165
+ localName: undefined,
166
+ });
167
+ continue;
168
+ }
169
+ for (const { element, name } of patternElements(declaration.name)) {
170
+ into.declare(element, owner, "variable", name, {
171
+ component: { text: name.text, computed: false },
172
+ localName: undefined,
173
+ });
174
+ }
175
+ }
176
+ }),
177
+ ],
178
+ [
179
+ SyntaxKind.ExportDeclaration,
180
+ arm(isExportDeclaration, (node, owner, into) => {
181
+ const clause = node.exportClause;
182
+ // A bare star re-export declares nothing: the names it carries are the named module's.
183
+ if (clause === undefined) {
184
+ return;
185
+ }
186
+ const specifiers = isNamespaceExport(clause) ? [clause] : clause.elements;
187
+ for (const specifier of specifiers) {
188
+ const component = componentOf(specifier.name);
189
+ if (component !== undefined) {
190
+ into.declare(specifier, owner, "export-alias", specifier.name, {
191
+ component,
192
+ localName: undefined,
193
+ });
194
+ }
195
+ }
196
+ }),
197
+ ],
198
+ [
199
+ SyntaxKind.ExportAssignment,
200
+ arm(isExportAssignment, (node, owner, into) => {
201
+ if (!node.isExportEquals) {
202
+ into.declare(node, owner, "export-alias", undefined, {
203
+ component: { text: "default", computed: false },
204
+ localName: undefined,
205
+ });
206
+ }
207
+ }),
208
+ ],
209
+ ]);
210
+
211
+ /** The declarations a module, a namespace or a container body writes. */
212
+ export const SYNTAX: DeclarationSource = {
213
+ declare(file: SourceFile, owner: Building, into: Builder): void {
214
+ const walk: Walk = (statement, holder) => {
215
+ ARMS.get(statement.kind)?.(statement, holder, into, walk);
216
+ };
217
+ // A statement a component file's module appends is not written in the file.
218
+ const written = writtenLength(file);
219
+ for (const statement of file.statements) {
220
+ if (statement.end <= written) {
221
+ walk(statement, owner);
222
+ }
223
+ }
224
+ },
225
+ };
226
+
227
+ /** The type one node is, through however many parentheses are written around it. */
228
+ function unparenthesizedType(type: Node): Node {
229
+ let held = type;
230
+ while (isParenthesizedTypeNode(held)) {
231
+ held = held.type;
232
+ }
233
+ return held;
234
+ }
235
+
236
+ /**
237
+ * The direct constituents of one type. Parentheses are syntax and are not a constituent, so
238
+ * the constituents of `(A | B) & C` are `A`, `B` and `C`; a union or an intersection written
239
+ * without them stays one constituent.
240
+ */
241
+ function constituentsOf(type: Node): readonly Node[] {
242
+ const held = unparenthesizedType(type);
243
+ if (!isUnionTypeNode(held) && !isIntersectionTypeNode(held)) {
244
+ return [held];
245
+ }
246
+ return held.types.flatMap((member) =>
247
+ isParenthesizedTypeNode(member) ? constituentsOf(member) : [member],
248
+ );
249
+ }
250
+
251
+ /**
252
+ * The members one type alias declares: those of each object type that is the alias or a
253
+ * direct constituent of it. A name two constituents both declare is left out of all of
254
+ * them: the alias stands for it, and neither declaration is the one a reference names.
255
+ */
256
+ function aliasMembers(type: Node): readonly Node[] {
257
+ const literals = constituentsOf(type).filter(isTypeLiteralNode);
258
+ if (literals.length < 2) {
259
+ return literals[0]?.members ?? [];
260
+ }
261
+ const declaring = new Map<string, number>();
262
+ for (const literal of literals) {
263
+ for (const key of new Set(literal.members.map(keyOf))) {
264
+ if (key !== undefined) {
265
+ declaring.set(key, (declaring.get(key) ?? 0) + 1);
266
+ }
267
+ }
268
+ }
269
+ return literals.flatMap((literal) =>
270
+ literal.members.filter((member) => {
271
+ const key = keyOf(member);
272
+ return key !== undefined && declaring.get(key) === 1;
273
+ }),
274
+ );
275
+ }
276
+
277
+ /** One type member's identity, and undefined for a member the grammar cannot name. */
278
+ function keyOf(member: Node): string | undefined {
279
+ const component = componentOf(nameNodeOf(member));
280
+ return component === undefined ? undefined : memberKey(component, false);
281
+ }
282
+
283
+ /**
284
+ * The binding elements of a destructuring declaration that each declare a name of their
285
+ * own, at any depth, but one with a default, whose removal skips the default's evaluation,
286
+ * and one beside a rest element of its object pattern, whose removal changes the rest.
287
+ */
288
+ function patternElements(
289
+ name: Node,
290
+ ): readonly { readonly element: BindingElement; readonly name: Identifier }[] {
291
+ if (!isObjectBindingPattern(name) && !isArrayBindingPattern(name)) {
292
+ return [];
293
+ }
294
+ const rest =
295
+ isObjectBindingPattern(name) &&
296
+ name.elements.some((element) => element.dotDotDotToken !== undefined);
297
+ return name.elements.flatMap((element) => {
298
+ if (
299
+ !isBindingElement(element) ||
300
+ element.initializer !== undefined ||
301
+ (rest && element.dotDotDotToken === undefined)
302
+ ) {
303
+ return [];
304
+ }
305
+ const bound = element.name;
306
+ if (bound === undefined) {
307
+ return [];
308
+ }
309
+ return isIdentifier(bound) ? [{ element, name: bound }] : patternElements(bound);
310
+ });
311
+ }