@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,236 @@
1
+ import { isArrayBindingPattern, isBindingElement, isClassDeclaration, isConstructorDeclaration, isEnumDeclaration, isExportAssignment, isExportDeclaration, isFunctionDeclaration, isIdentifier, isInterfaceDeclaration, isIntersectionTypeNode, isModuleBlock, isModuleDeclaration, isNamespaceExport, isObjectBindingPattern, isParenthesizedTypeNode, isStringLiteral, isTypeAliasDeclaration, isTypeLiteralNode, isUnionTypeNode, isVariableStatement, SyntaxKind, } from "@typescript/native/ast";
2
+ import { componentOf, exportedAs, isParameterProperty, memberKey, nameNodeOf, } from "../inventory.js";
3
+ import { writtenLength } from "../position.js";
4
+ /** An arm for one statement form, which reads only a node the guard admits. */
5
+ function arm(guard, declare) {
6
+ return (node, owner, into, walk) => {
7
+ if (guard(node)) {
8
+ declare(node, owner, into, walk);
9
+ }
10
+ };
11
+ }
12
+ /** A declaration named by its own name, or `default` as a default export, and its members. */
13
+ function named(guard, kind, members) {
14
+ return arm(guard, (node, owner, into) => {
15
+ const name = nameNodeOf(node);
16
+ const { component, localName } = exportedAs(node, name);
17
+ if (component === undefined) {
18
+ return;
19
+ }
20
+ const record = into.declare(node, owner, kind, name, { component, localName });
21
+ const held = members?.(node);
22
+ if (held !== undefined) {
23
+ into.container({ owner: record, declaration: node, ...held });
24
+ }
25
+ });
26
+ }
27
+ /** The parameter properties every constructor of one class declares. */
28
+ function parameterPropertiesOf(node) {
29
+ return node.members.flatMap((member) => isConstructorDeclaration(member) ? member.parameters.filter(isParameterProperty) : []);
30
+ }
31
+ // A type alias keeps no member table of its own: the members belong to the object types
32
+ // its declaration writes, so the declaration is what carries them.
33
+ const TYPE_ALIAS = named(isTypeAliasDeclaration, "type", (node) => {
34
+ const members = aliasMembers(node.type);
35
+ return members.length === 0
36
+ ? undefined
37
+ : {
38
+ nameNode: undefined,
39
+ members,
40
+ memberKind: "type-member",
41
+ };
42
+ });
43
+ /** The statement forms that declare, each with what it declares. */
44
+ const ARMS = new Map([
45
+ [SyntaxKind.FunctionDeclaration, named(isFunctionDeclaration, "function")],
46
+ [
47
+ SyntaxKind.ClassDeclaration,
48
+ named(isClassDeclaration, "class", (node) => ({
49
+ nameNode: node.name,
50
+ members: [...node.members, ...parameterPropertiesOf(node)],
51
+ memberKind: "class-member",
52
+ })),
53
+ ],
54
+ [
55
+ SyntaxKind.InterfaceDeclaration,
56
+ named(isInterfaceDeclaration, "interface", (node) => ({
57
+ nameNode: node.name,
58
+ members: node.members,
59
+ memberKind: "interface-method",
60
+ })),
61
+ ],
62
+ [SyntaxKind.TypeAliasDeclaration, TYPE_ALIAS],
63
+ // A JavaScript file's `@typedef` is reparsed into this declaration.
64
+ [SyntaxKind.JSTypeAliasDeclaration, TYPE_ALIAS],
65
+ [
66
+ SyntaxKind.EnumDeclaration,
67
+ named(isEnumDeclaration, "enum", (node) => ({
68
+ nameNode: node.name,
69
+ members: node.members,
70
+ memberKind: "enum-member",
71
+ })),
72
+ ],
73
+ [
74
+ SyntaxKind.ModuleDeclaration,
75
+ arm(isModuleDeclaration, (node, owner, into, walk) => {
76
+ const component = componentOf(node.name);
77
+ if (component === undefined) {
78
+ return;
79
+ }
80
+ const record = into.declare(node, owner, "namespace", node.name, {
81
+ component,
82
+ localName: undefined,
83
+ });
84
+ into.exportScope(record, node.name);
85
+ if (isStringLiteral(node.name)) {
86
+ into.module(record, node.name.text);
87
+ }
88
+ const body = node.body;
89
+ if (body === undefined) {
90
+ return;
91
+ }
92
+ for (const statement of isModuleBlock(body) ? body.statements : [body]) {
93
+ walk(statement, record);
94
+ }
95
+ }),
96
+ ],
97
+ [
98
+ SyntaxKind.VariableStatement,
99
+ arm(isVariableStatement, (node, owner, into) => {
100
+ for (const declaration of node.declarationList.declarations) {
101
+ const component = componentOf(declaration.name);
102
+ if (component !== undefined) {
103
+ into.declare(declaration, owner, "variable", declaration.name, {
104
+ component,
105
+ localName: undefined,
106
+ });
107
+ continue;
108
+ }
109
+ for (const { element, name } of patternElements(declaration.name)) {
110
+ into.declare(element, owner, "variable", name, {
111
+ component: { text: name.text, computed: false },
112
+ localName: undefined,
113
+ });
114
+ }
115
+ }
116
+ }),
117
+ ],
118
+ [
119
+ SyntaxKind.ExportDeclaration,
120
+ arm(isExportDeclaration, (node, owner, into) => {
121
+ const clause = node.exportClause;
122
+ // A bare star re-export declares nothing: the names it carries are the named module's.
123
+ if (clause === undefined) {
124
+ return;
125
+ }
126
+ const specifiers = isNamespaceExport(clause) ? [clause] : clause.elements;
127
+ for (const specifier of specifiers) {
128
+ const component = componentOf(specifier.name);
129
+ if (component !== undefined) {
130
+ into.declare(specifier, owner, "export-alias", specifier.name, {
131
+ component,
132
+ localName: undefined,
133
+ });
134
+ }
135
+ }
136
+ }),
137
+ ],
138
+ [
139
+ SyntaxKind.ExportAssignment,
140
+ arm(isExportAssignment, (node, owner, into) => {
141
+ if (!node.isExportEquals) {
142
+ into.declare(node, owner, "export-alias", undefined, {
143
+ component: { text: "default", computed: false },
144
+ localName: undefined,
145
+ });
146
+ }
147
+ }),
148
+ ],
149
+ ]);
150
+ /** The declarations a module, a namespace or a container body writes. */
151
+ export const SYNTAX = {
152
+ declare(file, owner, into) {
153
+ const walk = (statement, holder) => {
154
+ ARMS.get(statement.kind)?.(statement, holder, into, walk);
155
+ };
156
+ // A statement a component file's module appends is not written in the file.
157
+ const written = writtenLength(file);
158
+ for (const statement of file.statements) {
159
+ if (statement.end <= written) {
160
+ walk(statement, owner);
161
+ }
162
+ }
163
+ },
164
+ };
165
+ /** The type one node is, through however many parentheses are written around it. */
166
+ function unparenthesizedType(type) {
167
+ let held = type;
168
+ while (isParenthesizedTypeNode(held)) {
169
+ held = held.type;
170
+ }
171
+ return held;
172
+ }
173
+ /**
174
+ * The direct constituents of one type. Parentheses are syntax and are not a constituent, so
175
+ * the constituents of `(A | B) & C` are `A`, `B` and `C`; a union or an intersection written
176
+ * without them stays one constituent.
177
+ */
178
+ function constituentsOf(type) {
179
+ const held = unparenthesizedType(type);
180
+ if (!isUnionTypeNode(held) && !isIntersectionTypeNode(held)) {
181
+ return [held];
182
+ }
183
+ return held.types.flatMap((member) => isParenthesizedTypeNode(member) ? constituentsOf(member) : [member]);
184
+ }
185
+ /**
186
+ * The members one type alias declares: those of each object type that is the alias or a
187
+ * direct constituent of it. A name two constituents both declare is left out of all of
188
+ * them: the alias stands for it, and neither declaration is the one a reference names.
189
+ */
190
+ function aliasMembers(type) {
191
+ const literals = constituentsOf(type).filter(isTypeLiteralNode);
192
+ if (literals.length < 2) {
193
+ return literals[0]?.members ?? [];
194
+ }
195
+ const declaring = new Map();
196
+ for (const literal of literals) {
197
+ for (const key of new Set(literal.members.map(keyOf))) {
198
+ if (key !== undefined) {
199
+ declaring.set(key, (declaring.get(key) ?? 0) + 1);
200
+ }
201
+ }
202
+ }
203
+ return literals.flatMap((literal) => literal.members.filter((member) => {
204
+ const key = keyOf(member);
205
+ return key !== undefined && declaring.get(key) === 1;
206
+ }));
207
+ }
208
+ /** One type member's identity, and undefined for a member the grammar cannot name. */
209
+ function keyOf(member) {
210
+ const component = componentOf(nameNodeOf(member));
211
+ return component === undefined ? undefined : memberKey(component, false);
212
+ }
213
+ /**
214
+ * The binding elements of a destructuring declaration that each declare a name of their
215
+ * own, at any depth, but one with a default, whose removal skips the default's evaluation,
216
+ * and one beside a rest element of its object pattern, whose removal changes the rest.
217
+ */
218
+ function patternElements(name) {
219
+ if (!isObjectBindingPattern(name) && !isArrayBindingPattern(name)) {
220
+ return [];
221
+ }
222
+ const rest = isObjectBindingPattern(name) &&
223
+ name.elements.some((element) => element.dotDotDotToken !== undefined);
224
+ return name.elements.flatMap((element) => {
225
+ if (!isBindingElement(element) ||
226
+ element.initializer !== undefined ||
227
+ (rest && element.dotDotDotToken === undefined)) {
228
+ return [];
229
+ }
230
+ const bound = element.name;
231
+ if (bound === undefined) {
232
+ return [];
233
+ }
234
+ return isIdentifier(bound) ? [{ element, name: bound }] : patternElements(bound);
235
+ });
236
+ }