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

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 (207) hide show
  1. package/dist/src/alias-chain.js +0 -5
  2. package/dist/src/amd.js +81 -0
  3. package/dist/src/analysis.js +94 -54
  4. package/dist/src/augmentations.js +23 -24
  5. package/dist/src/bindings.js +382 -0
  6. package/dist/src/calls.js +25 -8
  7. package/dist/src/commands/read.js +6 -7
  8. package/dist/src/commands/tools.js +5 -9
  9. package/dist/src/component-files.js +3 -2
  10. package/dist/src/conformance-record.js +1 -1
  11. package/dist/src/consumers.js +9 -10
  12. package/dist/src/contextual-uses.js +1 -17
  13. package/dist/src/contract/config.generated.js +5 -2
  14. package/dist/src/contract/contract.generated.js +119 -2
  15. package/dist/src/contract/report.generated.js +6 -2
  16. package/dist/src/dependencies.js +98 -71
  17. package/dist/src/destructuring.js +1 -19
  18. package/dist/src/element-access.js +179 -164
  19. package/dist/src/entries/build-scripts.js +222 -0
  20. package/dist/src/{configuration-files.js → entries/configuration-files.js} +22 -64
  21. package/dist/src/{convention-rows.js → entries/convention-rows.js} +120 -27
  22. package/dist/src/entries/conventions.js +739 -0
  23. package/dist/src/{html-entries.js → entries/html-entries.js} +9 -12
  24. package/dist/src/{mdx-entries.js → entries/mdx-entries.js} +3 -3
  25. package/dist/src/entries/run-directories.js +77 -0
  26. package/dist/src/entries/sources.js +144 -0
  27. package/dist/src/entries/static-module.js +670 -0
  28. package/dist/src/entries/test-runners.js +684 -0
  29. package/dist/src/{workflow-steps.js → entries/workflow-steps.js} +18 -8
  30. package/dist/src/extensions.js +37 -0
  31. package/dist/src/facts.js +38 -0
  32. package/dist/src/findings/completion.js +40 -4
  33. package/dist/src/findings/emitters.js +5 -8
  34. package/dist/src/{implementations.js → findings/implementations.js} +3 -3
  35. package/dist/src/{in-operands.js → findings/in-operands.js} +2 -2
  36. package/dist/src/findings/self-check.js +36 -20
  37. package/dist/src/findings-pass.js +5 -6
  38. package/dist/src/framework-lifecycle.js +42 -15
  39. package/dist/src/fs/manifest.js +15 -20
  40. package/dist/src/generated-file.js +172 -29
  41. package/dist/src/graph.js +14 -8
  42. package/dist/src/import-bindings.js +73 -28
  43. package/dist/src/index/sites.js +557 -0
  44. package/dist/src/index.js +1 -1
  45. package/dist/src/indexed-access.js +523 -478
  46. package/dist/src/injection-container.js +1 -1
  47. package/dist/src/interface-satisfaction.js +1 -1
  48. package/dist/src/inventory/commonjs.js +62 -17
  49. package/dist/src/inventory/enumerate.js +2 -1
  50. package/dist/src/javascript-file.js +26 -11
  51. package/dist/src/keyof-comparisons.js +124 -103
  52. package/dist/src/load/classify.js +28 -9
  53. package/dist/src/load/diagnostics.js +8 -1
  54. package/dist/src/load/discover.js +15 -7
  55. package/dist/src/load/expected-module.js +2 -2
  56. package/dist/src/load/plan.js +2 -2
  57. package/dist/src/load/specifiers.js +2 -4
  58. package/dist/src/load/synthesize.js +10 -5
  59. package/dist/src/load/workspace-manifests.js +1 -1
  60. package/dist/src/load/workspace-resolution.js +2 -2
  61. package/dist/src/matrix.js +12 -17
  62. package/dist/src/module-declarations.js +70 -85
  63. package/dist/src/overrides.js +134 -112
  64. package/dist/src/paths.js +56 -1
  65. package/dist/src/platform-calls.js +78 -0
  66. package/dist/src/platform-symbols.js +134 -0
  67. package/dist/src/references/read-rule.js +1 -0
  68. package/dist/src/references/read-rules.js +48 -0
  69. package/dist/src/references/sink.js +151 -0
  70. package/dist/src/references/sources.js +450 -0
  71. package/dist/src/references.js +111 -1116
  72. package/dist/src/resolve.js +1 -1
  73. package/dist/src/{configured-declarations.js → roots/configured-declarations.js} +7 -4
  74. package/dist/src/{entry-point-gaps.js → roots/entry-point-gaps.js} +5 -8
  75. package/dist/src/{manifest.js → roots/manifest.js} +4 -4
  76. package/dist/src/roots/published-reach.js +412 -0
  77. package/dist/src/roots/root-set.js +489 -0
  78. package/dist/src/roots/unseen-loads.js +236 -0
  79. package/dist/src/serialization-contract.js +9 -4
  80. package/dist/src/source-files.js +5 -9
  81. package/dist/src/stores.js +4 -2
  82. package/dist/src/suppress.js +30 -32
  83. package/dist/src/sweep.js +25 -13
  84. package/dist/src/symbol-keys.js +89 -83
  85. package/dist/src/tests/file-class.js +76 -0
  86. package/dist/src/{outside-tests.js → tests/outside.js} +7 -7
  87. package/dist/src/{test-support.js → tests/support.js} +5 -12
  88. package/dist/src/type-parameter-reads.js +178 -139
  89. package/dist/src/unimplemented-callees.js +262 -242
  90. package/dist/src/verbs/analyze.js +15 -3
  91. package/dist/src/verbs/describe.js +5 -3
  92. package/dist/src/verbs/explain.js +50 -28
  93. package/dist/src/verbs/print-retained.js +2 -2
  94. package/jsr.json +1 -1
  95. package/package.json +3 -3
  96. package/src/alias-chain.ts +0 -7
  97. package/src/amd.ts +130 -0
  98. package/src/analysis.ts +156 -70
  99. package/src/augmentations.ts +41 -27
  100. package/src/automatic-types.ts +1 -1
  101. package/src/bindings.ts +460 -0
  102. package/src/calls.ts +25 -7
  103. package/src/commands/read.ts +6 -8
  104. package/src/commands/tools.ts +5 -11
  105. package/src/component-files.ts +4 -2
  106. package/src/conformance-record.ts +1 -1
  107. package/src/consumers.ts +12 -11
  108. package/src/contextual-uses.ts +2 -22
  109. package/src/contract/config.generated.ts +5 -2
  110. package/src/contract/contract.generated.ts +138 -2
  111. package/src/contract/report.generated.ts +6 -2
  112. package/src/dependencies.ts +117 -71
  113. package/src/destructuring.ts +1 -31
  114. package/src/element-access.ts +188 -182
  115. package/src/entries/build-scripts.ts +292 -0
  116. package/src/{configuration-files.ts → entries/configuration-files.ts} +33 -82
  117. package/src/{convention-rows.ts → entries/convention-rows.ts} +167 -28
  118. package/src/{conventions.ts → entries/conventions.ts} +349 -523
  119. package/src/{html-entries.ts → entries/html-entries.ts} +19 -18
  120. package/src/{mdx-entries.ts → entries/mdx-entries.ts} +5 -5
  121. package/src/entries/run-directories.ts +108 -0
  122. package/src/entries/sources.ts +266 -0
  123. package/src/entries/static-module.ts +858 -0
  124. package/src/entries/test-runners.ts +868 -0
  125. package/src/{workflow-steps.ts → entries/workflow-steps.ts} +29 -11
  126. package/src/exempt.ts +3 -2
  127. package/src/extensions.ts +45 -0
  128. package/src/facts.ts +46 -0
  129. package/src/file-facts.ts +1 -1
  130. package/src/findings/boundary.ts +2 -0
  131. package/src/findings/completion.ts +43 -4
  132. package/src/findings/emitter.ts +1 -1
  133. package/src/findings/emitters.ts +14 -10
  134. package/src/{implementations.ts → findings/implementations.ts} +4 -4
  135. package/src/{in-operands.ts → findings/in-operands.ts} +3 -3
  136. package/src/findings/self-check.ts +41 -22
  137. package/src/findings/visibility-narrowing.ts +1 -1
  138. package/src/findings-pass.ts +5 -12
  139. package/src/framework-lifecycle.ts +43 -15
  140. package/src/fs/manifest.ts +25 -21
  141. package/src/generated-file.ts +240 -36
  142. package/src/graph.ts +27 -11
  143. package/src/import-bindings.ts +98 -29
  144. package/src/index/sites.ts +781 -0
  145. package/src/index.ts +1 -1
  146. package/src/indexed-access.ts +570 -527
  147. package/src/injection-container.ts +1 -1
  148. package/src/interface-satisfaction.ts +1 -1
  149. package/src/inventory/commonjs.ts +102 -17
  150. package/src/inventory/enumerate.ts +2 -1
  151. package/src/javascript-file.ts +29 -12
  152. package/src/keyof-comparisons.ts +131 -116
  153. package/src/load/classify.ts +49 -13
  154. package/src/load/diagnostics.ts +11 -1
  155. package/src/load/discover.ts +27 -11
  156. package/src/load/expected-module.ts +8 -2
  157. package/src/load/plan.ts +11 -2
  158. package/src/load/specifiers.ts +8 -5
  159. package/src/load/synthesize.ts +12 -6
  160. package/src/load/test-globals.ts +1 -1
  161. package/src/load/workspace-manifests.ts +1 -1
  162. package/src/load/workspace-resolution.ts +9 -2
  163. package/src/load/workspace-run.ts +1 -1
  164. package/src/matrix.ts +13 -17
  165. package/src/module-declarations.ts +74 -98
  166. package/src/overrides.ts +162 -134
  167. package/src/paths.ts +65 -1
  168. package/src/platform-calls.ts +119 -0
  169. package/src/platform-symbols.ts +189 -0
  170. package/src/references/read-rule.ts +60 -0
  171. package/src/references/read-rules.ts +55 -0
  172. package/src/references/sink.ts +213 -0
  173. package/src/references/sources.ts +632 -0
  174. package/src/references.ts +174 -1492
  175. package/src/report.ts +2 -2
  176. package/src/resolve.ts +1 -1
  177. package/src/{configured-declarations.ts → roots/configured-declarations.ts} +10 -7
  178. package/src/{entry-point-gaps.ts → roots/entry-point-gaps.ts} +5 -8
  179. package/src/{manifest.ts → roots/manifest.ts} +5 -5
  180. package/src/roots/published-reach.ts +481 -0
  181. package/src/{roots.ts → roots/root-set.ts} +203 -630
  182. package/src/roots/unseen-loads.ts +345 -0
  183. package/src/serialization-contract.ts +9 -4
  184. package/src/source-files.ts +5 -10
  185. package/src/stores.ts +13 -2
  186. package/src/suppress.ts +48 -33
  187. package/src/sweep.ts +37 -14
  188. package/src/symbol-keys.ts +102 -102
  189. package/src/tests/file-class.ts +118 -0
  190. package/src/{outside-tests.ts → tests/outside.ts} +16 -10
  191. package/src/{test-support.ts → tests/support.ts} +15 -21
  192. package/src/type-parameter-reads.ts +187 -158
  193. package/src/unimplemented-callees.ts +297 -280
  194. package/src/verbs/analyze.ts +19 -5
  195. package/src/verbs/describe.ts +5 -2
  196. package/src/verbs/explain.ts +75 -32
  197. package/src/verbs/print-retained.ts +2 -2
  198. package/dist/src/build-scripts.js +0 -418
  199. package/dist/src/conventions.js +0 -907
  200. package/dist/src/entry-points.js +0 -444
  201. package/dist/src/own-body.js +0 -28
  202. package/dist/src/roots.js +0 -865
  203. package/dist/src/unseen-loads.js +0 -189
  204. package/src/build-scripts.ts +0 -536
  205. package/src/entry-points.ts +0 -584
  206. package/src/own-body.ts +0 -42
  207. package/src/unseen-loads.ts +0 -282
@@ -1,907 +0,0 @@
1
- /**
2
- * The convention rows a run applies, and the files they root. A row applies to a
3
- * manifest that declares its enabling package when the installed package's version is
4
- * in the row's range; its globs are read against that manifest's directory, with every
5
- * directory a configuration property moves read from the property's literal value.
6
- * No configuration file is run.
7
- */
8
- import { isArrayLiteralExpression, isBinaryExpression, isCallExpression, isClassDeclaration, isComputedPropertyName, isExportAssignment, isExportDeclaration, isExpressionStatement, isFunctionDeclaration, isIdentifier, isImportDeclaration, isNamedExports, isNamespaceImport, isNoSubstitutionTemplateLiteral, isNumericLiteral, isObjectBindingPattern, isObjectLiteralExpression, isPropertyAccessExpression, isPropertyAssignment, isShorthandPropertyAssignment, isSpreadAssignment, isStringLiteral, isVariableStatement, SyntaxKind, } from "@typescript/native/ast";
9
- import { lineOf } from "./component-files.js";
10
- import { CONVENTION_ROWS, } from "./convention-rows.js";
11
- import { documentStrings } from "./configuration-files.js";
12
- import { ancestors, dirnamePath, joinPath, normalizePath, relativePath, resolvePath, } from "./paths.js";
13
- import { satisfies } from "./version-range.js";
14
- import { bytewise } from "./text/compare.js";
15
- import { isRecord } from "./json-document.js";
16
- import { unwrapped } from "./unwrap.js";
17
- import { filesIn } from "./fs/tree.js";
18
- import { DEPENDENCY_SECTIONS, INSTALLED_PACKAGES, MANIFEST, readJSON } from "./fs/manifest.js";
19
- import { globExpression } from "./text/glob.js";
20
- import { yamlDocument } from "./text/yaml.js";
21
- const JSON_EXTENSION = ".json";
22
- /**
23
- * A configuration file its tool reads as a YAML document, read as the JSON document it
24
- * denotes. Its nodes' offsets are that JSON text's, so no move may read one: a failure's
25
- * line would name the wrong line.
26
- */
27
- const YAML_NAME = /\.ya?ml$/u;
28
- /** A configuration file with no extension that its tool reads as a JSON document. */
29
- const JSON_RC = /^\.[\w-]+rc$/u;
30
- /** The sections an installed package is required by, so its absence fails the run. */
31
- const REQUIRED_SECTIONS = ["dependencies", "devDependencies"];
32
- function declares(manifest, name, sections = DEPENDENCY_SECTIONS) {
33
- if (!isRecord(manifest)) {
34
- return false;
35
- }
36
- return sections.some((section) => {
37
- const held = manifest[section];
38
- return isRecord(held) && Object.hasOwn(held, name);
39
- });
40
- }
41
- /**
42
- * The version the installed package's manifest names, read from the nearest
43
- * `node_modules` at or above `dir` that holds it, `""` where that manifest names
44
- * none, or `undefined` where no `node_modules` holds it.
45
- */
46
- function installedVersion(host, dir, name) {
47
- for (const at of ancestors(dir)) {
48
- const path = joinPath(at, INSTALLED_PACKAGES, name, MANIFEST);
49
- if (host.kindOf(path) === "file") {
50
- const manifest = readJSON(host, path);
51
- const version = isRecord(manifest) ? manifest["version"] : undefined;
52
- return typeof version === "string" ? version : "";
53
- }
54
- }
55
- return undefined;
56
- }
57
- /** The key one property is written with, where it is written as a name or as literal text. */
58
- function keyOf(property) {
59
- if (!isPropertyAssignment(property) && !isShorthandPropertyAssignment(property)) {
60
- return undefined;
61
- }
62
- const { name } = property;
63
- const key = isComputedPropertyName(name) ? name.expression : name;
64
- if ((isIdentifier(key) && key === name) ||
65
- isStringLiteral(key) ||
66
- isNoSubstitutionTemplateLiteral(key)) {
67
- return key.text;
68
- }
69
- return undefined;
70
- }
71
- /** Whether one member's computed name is no literal, so it may set any key. */
72
- function computesKey(member) {
73
- const name = member.name;
74
- if (name === undefined || !isComputedPropertyName(name)) {
75
- return false;
76
- }
77
- const key = name.expression;
78
- return !(isStringLiteral(key) || isNumericLiteral(key) || isNoSubstitutionTemplateLiteral(key));
79
- }
80
- /** The values one file's top-level variable declarations bind, by name. */
81
- function declaredValues(file) {
82
- const values = new Map();
83
- for (const statement of file.statements) {
84
- if (isVariableStatement(statement)) {
85
- for (const declaration of statement.declarationList.declarations) {
86
- if (isIdentifier(declaration.name) && declaration.initializer !== undefined) {
87
- values.set(declaration.name.text, declaration.initializer);
88
- }
89
- }
90
- }
91
- }
92
- return values;
93
- }
94
- function isModuleExports(node) {
95
- return (isPropertyAccessExpression(node) &&
96
- isIdentifier(node.expression) &&
97
- node.expression.text === "module" &&
98
- node.name.text === "exports");
99
- }
100
- /**
101
- * Every expression or declaration one file exports as its default, in any module syntax.
102
- * A JSON module's default export is its document's value.
103
- */
104
- function defaultExports(file) {
105
- if (file.fileName.endsWith(JSON_EXTENSION)) {
106
- return file.statements.flatMap((statement) => isExpressionStatement(statement) ? [statement.expression] : []);
107
- }
108
- const found = [];
109
- for (const statement of file.statements) {
110
- if (isExportAssignment(statement)) {
111
- found.push(statement.expression);
112
- }
113
- else if ((isFunctionDeclaration(statement) || isClassDeclaration(statement)) &&
114
- statement.modifiers?.some((one) => one.kind === SyntaxKind.DefaultKeyword) === true) {
115
- found.push(statement);
116
- }
117
- else if (isExportDeclaration(statement) &&
118
- statement.moduleSpecifier === undefined &&
119
- statement.exportClause !== undefined &&
120
- isNamedExports(statement.exportClause)) {
121
- for (const element of statement.exportClause.elements) {
122
- if (element.name.text === "default") {
123
- found.push(element.propertyName ?? element.name);
124
- }
125
- }
126
- }
127
- else if (isExpressionStatement(statement) &&
128
- isBinaryExpression(statement.expression) &&
129
- statement.expression.operatorToken.kind === SyntaxKind.EqualsToken &&
130
- isModuleExports(statement.expression.left)) {
131
- found.push(statement.expression.right);
132
- }
133
- }
134
- return found;
135
- }
136
- /** The declarations whose name is a binding of the scope they are written in. */
137
- const BINDING_KINDS = new Set([
138
- SyntaxKind.VariableDeclaration,
139
- SyntaxKind.Parameter,
140
- SyntaxKind.BindingElement,
141
- SyntaxKind.FunctionDeclaration,
142
- SyntaxKind.ClassDeclaration,
143
- SyntaxKind.ImportClause,
144
- SyntaxKind.ImportSpecifier,
145
- SyntaxKind.NamespaceImport,
146
- SyntaxKind.ImportEqualsDeclaration,
147
- ]);
148
- /** Whether one file declares a binding named `require` at any depth, which shadows the global. */
149
- function declaresRequire(file) {
150
- let found = false;
151
- const visit = (node) => {
152
- const name = node.name;
153
- if (name !== undefined &&
154
- isIdentifier(name) &&
155
- name.text === "require" &&
156
- BINDING_KINDS.has(node.kind)) {
157
- found = true;
158
- }
159
- if (!found) {
160
- node.forEachChild(visit);
161
- }
162
- };
163
- file.forEachChild(visit);
164
- return found;
165
- }
166
- /**
167
- * The top-level `const`, `let` and `var` declarations that bind a `require` of one module,
168
- * written with the module's name as its one string-literal argument, in a file that
169
- * declares no binding named `require`: each binds as an import of the module does.
170
- */
171
- function requireBindings(file, module) {
172
- const found = [];
173
- for (const statement of file.statements) {
174
- if (!isVariableStatement(statement)) {
175
- continue;
176
- }
177
- for (const declaration of statement.declarationList.declarations) {
178
- const value = declaration.initializer === undefined ? undefined : unwrapped(declaration.initializer);
179
- const [argument, ...rest] = value !== undefined && isCallExpression(value) ? value.arguments : [];
180
- if (value === undefined ||
181
- !isCallExpression(value) ||
182
- !isIdentifier(value.expression) ||
183
- value.expression.text !== "require" ||
184
- argument === undefined ||
185
- rest.length > 0 ||
186
- !isStringLiteral(argument) ||
187
- argument.text !== module) {
188
- continue;
189
- }
190
- if (isIdentifier(declaration.name) || isObjectBindingPattern(declaration.name)) {
191
- found.push(declaration.name);
192
- }
193
- }
194
- }
195
- return found.length > 0 && declaresRequire(file) ? [] : found;
196
- }
197
- /**
198
- * The first argument of every call one file makes to one module's export, through a
199
- * named or a namespace import; `undefined` for a call with no argument.
200
- */
201
- function optionsArguments(file, call) {
202
- const names = new Set();
203
- const namespaces = new Set();
204
- for (const name of requireBindings(file, call.module)) {
205
- if (isIdentifier(name)) {
206
- namespaces.add(name.text);
207
- continue;
208
- }
209
- for (const element of name.elements) {
210
- const local = element.name;
211
- const exported = element.propertyName ?? local;
212
- if (element.dotDotDotToken === undefined &&
213
- local !== undefined &&
214
- isIdentifier(local) &&
215
- exported !== undefined &&
216
- (isIdentifier(exported) || isStringLiteral(exported)) &&
217
- exported.text === call.export) {
218
- names.add(local.text);
219
- }
220
- }
221
- }
222
- for (const statement of file.statements) {
223
- const bindings = isImportDeclaration(statement)
224
- ? statement.importClause?.namedBindings
225
- : undefined;
226
- if (bindings === undefined ||
227
- !isImportDeclaration(statement) ||
228
- !isStringLiteral(statement.moduleSpecifier) ||
229
- statement.moduleSpecifier.text !== call.module) {
230
- continue;
231
- }
232
- if (isNamespaceImport(bindings)) {
233
- namespaces.add(bindings.name.text);
234
- continue;
235
- }
236
- for (const element of bindings.elements) {
237
- if ((element.propertyName ?? element.name).text === call.export) {
238
- names.add(element.name.text);
239
- }
240
- }
241
- }
242
- const found = [];
243
- const visit = (node) => {
244
- if (isCallExpression(node)) {
245
- const callee = unwrapped(node.expression);
246
- const named = isIdentifier(callee) && names.has(callee.text);
247
- const throughNamespace = isPropertyAccessExpression(callee) &&
248
- isIdentifier(callee.expression) &&
249
- namespaces.has(callee.expression.text) &&
250
- callee.name.text === call.export;
251
- if (named || throughNamespace) {
252
- found.push(node.arguments[0]);
253
- }
254
- }
255
- node.forEachChild(visit);
256
- };
257
- file.forEachChild(visit);
258
- return found;
259
- }
260
- /**
261
- * The object literal one expression stands for, read through the file's top-level
262
- * declarations and, where `throughCalls`, through a call's first argument: `undefined`
263
- * for a call with no argument, and any other node where the expression is not written
264
- * out as an object literal.
265
- */
266
- function objectOf(node, declared, throughCalls, seen = new Set()) {
267
- const at = unwrapped(node);
268
- if (isIdentifier(at)) {
269
- const value = declared.get(at.text);
270
- if (value !== undefined && !seen.has(at.text)) {
271
- return objectOf(value, declared, throughCalls, new Set([...seen, at.text]));
272
- }
273
- }
274
- else if (throughCalls && isCallExpression(at)) {
275
- const [first] = at.arguments;
276
- return first === undefined ? undefined : objectOf(first, declared, throughCalls, seen);
277
- }
278
- return at;
279
- }
280
- /** The text of a string literal or a template literal with no substitution, or undefined. */
281
- function literalText(node) {
282
- const at = unwrapped(node);
283
- return isStringLiteral(at) || isNoSubstitutionTemplateLiteral(at) ? at.text : undefined;
284
- }
285
- /**
286
- * The literal texts one value at a path's end writes: the literal itself, or, where
287
- * `elements`, each element of an array literal of literals. Undefined for any other value.
288
- */
289
- function literalTexts(held, elements) {
290
- const one = literalText(held);
291
- if (one !== undefined) {
292
- return [one];
293
- }
294
- if (!elements || !isArrayLiteralExpression(held)) {
295
- return undefined;
296
- }
297
- const texts = held.elements.map(literalText);
298
- return texts.every((text) => text !== undefined) ? texts : undefined;
299
- }
300
- /**
301
- * Reads the values at one path of keys below one object literal into `values`, and
302
- * answers the first member that does not write its value out: a spread, a value that is
303
- * not a literal at the path's end, or one that is not an object literal before it.
304
- */
305
- function readPath(object, keys, values, elements) {
306
- const [key, ...rest] = keys;
307
- for (const member of object.properties) {
308
- if (isSpreadAssignment(member) || computesKey(member)) {
309
- return member;
310
- }
311
- if (keyOf(member) !== key) {
312
- continue;
313
- }
314
- const held = isPropertyAssignment(member) ? unwrapped(member.initializer) : undefined;
315
- if (rest.length === 0) {
316
- const texts = held === undefined ? undefined : literalTexts(held, elements);
317
- if (texts === undefined) {
318
- return member;
319
- }
320
- values.push(...texts);
321
- }
322
- else {
323
- if (held === undefined || !isObjectLiteralExpression(held)) {
324
- return member;
325
- }
326
- const failed = readPath(held, rest, values, elements);
327
- if (failed !== undefined) {
328
- return failed;
329
- }
330
- }
331
- }
332
- return undefined;
333
- }
334
- /**
335
- * Every value one file writes at one property's path inside the objects it is read from,
336
- * as {@link readProperty} reads them, whatever else the path holds.
337
- */
338
- function valuesAt(file, property, call) {
339
- const declared = declaredValues(file);
340
- const objects = call === undefined
341
- ? defaultExports(file).map((one) => objectOf(one, declared, true))
342
- : optionsArguments(file, call).map((one) => one === undefined ? undefined : objectOf(one, declared, false));
343
- const found = [];
344
- const visit = (object, keys) => {
345
- if (!isObjectLiteralExpression(object)) {
346
- return;
347
- }
348
- const [key, ...rest] = keys;
349
- for (const member of object.properties) {
350
- if (!isPropertyAssignment(member) || keyOf(member) !== key) {
351
- continue;
352
- }
353
- const held = unwrapped(member.initializer);
354
- if (rest.length > 0) {
355
- visit(held, rest);
356
- }
357
- else {
358
- found.push(held);
359
- }
360
- }
361
- };
362
- for (const object of objects) {
363
- if (object !== undefined) {
364
- visit(object, property.split("."));
365
- }
366
- }
367
- return found;
368
- }
369
- /** The string literals among values: each value itself, or each element of an array value. */
370
- function stringsAt(file, property, call) {
371
- return valuesAt(file, property, call).flatMap((held) => {
372
- if (literalText(held) !== undefined) {
373
- return [held];
374
- }
375
- return isArrayLiteralExpression(held)
376
- ? held.elements.map(unwrapped).filter((one) => literalText(one) !== undefined)
377
- : [];
378
- });
379
- }
380
- /** Whether one value holds a helper setting's value, as {@link HelperReading} states it. */
381
- function holdsValue(held, value) {
382
- if (value === true) {
383
- return held.kind === SyntaxKind.TrueKeyword;
384
- }
385
- if (literalText(held) === value) {
386
- return true;
387
- }
388
- return (isArrayLiteralExpression(held) &&
389
- held.elements.some((element) => {
390
- const one = unwrapped(element);
391
- const first = isArrayLiteralExpression(one) ? one.elements[0] : one;
392
- return first !== undefined && literalText(first) === value;
393
- }));
394
- }
395
- /** The directory one file's test-file strings are read against, as {@link TestFileReading} states it. */
396
- function testFileRoot(file, dir, reading) {
397
- for (const property of reading.roots) {
398
- const values = valuesAt(file, property, reading.call);
399
- if (values.length > 0) {
400
- const texts = new Set(values.map(literalText));
401
- const [text] = texts;
402
- return texts.size === 1 && text !== undefined ? resolvePath(dir, text) : undefined;
403
- }
404
- }
405
- return dir;
406
- }
407
- /** The files an applied row's test-file properties name. */
408
- function testFilesNamed(host, parse, dir, readings, into) {
409
- for (const reading of readings) {
410
- for (const { file } of readingFiles(host, parse, dir, reading.files)) {
411
- const at = testFileRoot(file, dir, reading);
412
- if (at === undefined) {
413
- continue;
414
- }
415
- for (const node of stringsAt(file, reading.property, reading.call)) {
416
- const text = literalText(node);
417
- if (text !== undefined && text !== "") {
418
- into.add(resolvePath(at, text));
419
- }
420
- }
421
- }
422
- }
423
- }
424
- /** The strings an applied row's selection keys hold, added per configuration file to `into`. */
425
- function selectedStrings(host, parse, dir, readings, into) {
426
- for (const reading of readings) {
427
- for (const { name, file } of readingFiles(host, parse, dir, reading.files)) {
428
- const path = joinPath(dir, name);
429
- const held = into.get(path) ?? { starts: new Set(), texts: [] };
430
- for (const node of stringsAt(file, reading.property, reading.call)) {
431
- held.starts.add(node.getStart());
432
- held.texts.push(literalText(node) ?? "");
433
- }
434
- into.set(path, held);
435
- }
436
- }
437
- }
438
- /**
439
- * The dependencies one manifest declares that a string at any depth of one of its members
440
- * names, alone or followed by `/` and a subpath, a member's name aside.
441
- */
442
- function manifestKeyUses(manifest, keys) {
443
- if (!isRecord(manifest)) {
444
- return [];
445
- }
446
- const declared = DEPENDENCY_SECTIONS.flatMap((section) => {
447
- const held = manifest[section];
448
- return isRecord(held) ? Object.keys(held) : [];
449
- });
450
- const strings = keys.flatMap((key) => documentStrings(manifest[key]));
451
- return declared.filter((name) => strings.some((text) => text === name || text.startsWith(`${name}/`)));
452
- }
453
- /** The extensions an entry a manifest entry key names without one may carry. */
454
- const ENTRY_EXTENSIONS = "{js,jsx,mjs,cjs,ts,tsx,mts,cts}";
455
- /** The characters that make a manifest string a pattern rather than a directory's name. */
456
- const PATTERN = /[*?{}[\]]/u;
457
- /**
458
- * The entry globs one applied row's manifest entry keys write, below the manifest's
459
- * directory: a directory's every module, or a file with or without an extension. A string
460
- * that leaves the directory names nothing.
461
- */
462
- function manifestEntryGlobs(manifest, keys) {
463
- const at = (value, path) => {
464
- const [key, ...rest] = path;
465
- if (key === undefined) {
466
- return [value];
467
- }
468
- if (!isRecord(value)) {
469
- return [];
470
- }
471
- const next = key === "*" ? Object.values(value) : Object.hasOwn(value, key) ? [value[key]] : [];
472
- return next.flatMap((one) => at(one, rest));
473
- };
474
- return keys.flatMap(({ key, directory }) => at(manifest, key.split(".")).flatMap((value) => documentStrings(value).flatMap((text) => {
475
- const path = normalizePath(text)
476
- .replace(/^(?:\.\/)+/u, "")
477
- .replace(/\/+$/u, "");
478
- if (path === "" || path === ".." || path.startsWith("../") || path.startsWith("/")) {
479
- return [];
480
- }
481
- if (!directory) {
482
- return [path, `${path}.${ENTRY_EXTENSIONS}`];
483
- }
484
- return PATTERN.test(path) ? [] : [`${path}/**/*.${ENTRY_EXTENSIONS}`];
485
- })));
486
- }
487
- /**
488
- * Every value one file writes at one property's path inside the object it is read from:
489
- * the options argument of every call `call` names, or the file's default export. A string
490
- * literal or a template literal with no substitution is the value, and so is each element
491
- * of an array literal of them where `elements`. A read object, or an object along the
492
- * path, that is not written out as an object literal, or that spreads another object into
493
- * itself, is not a literal, and neither is any other value at the path.
494
- */
495
- export function readProperty(file, property, call, elements = false) {
496
- const declared = declaredValues(file);
497
- const objects = call === undefined
498
- ? defaultExports(file).map((one) => objectOf(one, declared, true))
499
- : optionsArguments(file, call).map((one) => one === undefined ? undefined : objectOf(one, declared, false));
500
- const values = [];
501
- for (const object of objects) {
502
- if (object === undefined) {
503
- continue;
504
- }
505
- const notLiteral = isObjectLiteralExpression(object)
506
- ? readPath(object, property.split("."), values, elements)
507
- : object;
508
- if (notLiteral !== undefined) {
509
- return { values, notLiteral };
510
- }
511
- }
512
- return { values };
513
- }
514
- /** A directory below a manifest's, `.` for the directory itself, `undefined` outside it. */
515
- function below(dir, path) {
516
- return normalizePath(path) === normalizePath(dir) ? "." : relativePath(dir, path);
517
- }
518
- /**
519
- * Every text a template stands for, each placeholder replaced by every directory its move
520
- * resolved to. A placeholder whose move resolved outside the manifest's directory leaves
521
- * the text standing for nothing.
522
- */
523
- function expand(template, resolved) {
524
- const match = /<([A-Za-z]+)>/u.exec(template);
525
- if (match === null) {
526
- return [template];
527
- }
528
- const [placeholder, id = ""] = match;
529
- const out = [];
530
- for (const value of resolved.get(id) ?? []) {
531
- if (value === undefined) {
532
- continue;
533
- }
534
- const replaced = value === "." && template.startsWith(`${placeholder}/`, match.index)
535
- ? template.replace(`${placeholder}/`, "")
536
- : template.replace(placeholder, value);
537
- out.push(...expand(replaced, resolved));
538
- }
539
- return out;
540
- }
541
- /** The configuration files in one directory that one glob names, by name. */
542
- function configurationFiles(host, dir, glob) {
543
- const expression = globExpression(glob);
544
- return filesIn(host, dir, (name) => expression.test(name));
545
- }
546
- function resolveMove(host, parse, dir, row, move, resolved) {
547
- const set = [];
548
- for (const reading of move.readings) {
549
- for (const { name, text, file } of readingFiles(host, parse, dir, reading.files)) {
550
- const read = readProperty(file, reading.property, reading.call);
551
- if (read.notLiteral !== undefined) {
552
- const line = lineOf(text, read.notLiteral.getStart());
553
- return {
554
- failure: `${name}:${String(line)} sets ${reading.property}, which moves a directory ` +
555
- `the convention row ${row.name} reads, to a value that is not a string literal: ` +
556
- `write it as a literal, or name ${row.name} in ts.disabled_conventions and its ` +
557
- `files in ts.entry_files`,
558
- };
559
- }
560
- set.push(...read.values);
561
- }
562
- }
563
- const dirs = [];
564
- for (const base of expand(move.base ?? ".", resolved)) {
565
- const against = joinPath(dir, base);
566
- const texts = set.length > 0 ? set : move.defaults.flatMap((one) => expand(one, resolved));
567
- for (const text of texts) {
568
- dirs.push(below(dir, resolvePath(against, text)));
569
- }
570
- }
571
- return { dirs: [...new Set(dirs)] };
572
- }
573
- /** The texts each configuration file a reading names holds, parsed, in name order. */
574
- function readingFiles(host, parse, dir, files) {
575
- const found = [];
576
- for (const glob of files) {
577
- for (const name of configurationFiles(host, dir, glob)) {
578
- const path = joinPath(dir, name);
579
- let text;
580
- try {
581
- text = host.readFile(path);
582
- }
583
- catch {
584
- continue;
585
- }
586
- if (YAML_NAME.test(name)) {
587
- const value = yamlDocument(text);
588
- if (value !== undefined) {
589
- const json = JSON.stringify(value);
590
- found.push({ name, text: json, file: parse(`${path}${JSON_EXTENSION}`, json) });
591
- }
592
- continue;
593
- }
594
- found.push({
595
- name,
596
- text,
597
- file: parse(JSON_RC.test(name) ? `${path}${JSON_EXTENSION}` : path, text),
598
- });
599
- }
600
- }
601
- return found;
602
- }
603
- /**
604
- * The dependencies one manifest declares that an applied row's short names use: the
605
- * package each literal value stands for, and, for a value that is not a literal, every
606
- * declared dependency the row's package name stands for with some value.
607
- */
608
- function shortNameUses(host, parse, dir, manifest, readings) {
609
- const declared = new Set(DEPENDENCY_SECTIONS.flatMap((section) => {
610
- const held = isRecord(manifest) ? manifest[section] : undefined;
611
- return isRecord(held) ? Object.keys(held) : [];
612
- }));
613
- const used = new Set();
614
- for (const reading of readings) {
615
- const [before = "", after = ""] = reading.package.split("{}");
616
- for (const { file } of readingFiles(host, parse, dir, reading.files)) {
617
- const read = readProperty(file, reading.property, reading.call, true);
618
- for (const value of read.values) {
619
- used.add(`${before}${value}${after}`);
620
- }
621
- if (read.notLiteral !== undefined) {
622
- for (const name of declared) {
623
- if (name.length >= before.length + after.length &&
624
- name.startsWith(before) &&
625
- name.endsWith(after)) {
626
- used.add(name);
627
- }
628
- }
629
- }
630
- }
631
- }
632
- return [...used].filter((name) => declared.has(name)).sort(bytewise);
633
- }
634
- /**
635
- * The package one value of a prefixed-name reading names, or none: ESLint's own naming,
636
- * https://github.com/eslint/eslintrc/blob/main/lib/shared/naming.js
637
- */
638
- function prefixedPackage(value, reading) {
639
- const { prefix, marker } = reading;
640
- let name;
641
- if (marker === undefined) {
642
- if (value.includes(":") || value.startsWith(".") || value.startsWith("/")) {
643
- return undefined;
644
- }
645
- const parts = value.split("/");
646
- name = (value.startsWith("@") ? parts.slice(0, 2) : parts.slice(0, 1)).join("/");
647
- }
648
- else {
649
- if (!value.startsWith(marker) || !value.includes("/", marker.length)) {
650
- return undefined;
651
- }
652
- name = value.slice(marker.length, value.lastIndexOf("/"));
653
- }
654
- if (name === "") {
655
- return undefined;
656
- }
657
- if (!name.startsWith("@")) {
658
- return name.startsWith(`${prefix}-`) ? name : `${prefix}-${name}`;
659
- }
660
- const [scope, rest] = name.split("/");
661
- if (rest === undefined || rest === "" || rest === prefix) {
662
- return `${scope ?? name}/${prefix}`;
663
- }
664
- return rest.startsWith(`${prefix}-`) ? name : `${scope ?? name}/${prefix}-${rest}`;
665
- }
666
- /** The declared dependencies an applied row's prefixed names use. */
667
- function prefixedNameUses(host, parse, dir, manifest, readings) {
668
- const declared = new Set(DEPENDENCY_SECTIONS.flatMap((section) => {
669
- const held = isRecord(manifest) ? manifest[section] : undefined;
670
- return isRecord(held) ? Object.keys(held) : [];
671
- }));
672
- const used = new Set();
673
- for (const reading of readings) {
674
- for (const { file } of readingFiles(host, parse, dir, reading.files)) {
675
- for (const value of readProperty(file, reading.property, reading.call, true).values) {
676
- const named = prefixedPackage(value, reading);
677
- if (named !== undefined && declared.has(named)) {
678
- used.add(named);
679
- }
680
- }
681
- }
682
- }
683
- return [...used].sort(bytewise);
684
- }
685
- /** The declared dependencies an applied row's helper settings use. */
686
- function helperUses(host, parse, dir, manifest, readings) {
687
- const declared = new Set(DEPENDENCY_SECTIONS.flatMap((section) => {
688
- const held = isRecord(manifest) ? manifest[section] : undefined;
689
- return isRecord(held) ? Object.keys(held) : [];
690
- }));
691
- return readings
692
- .filter((reading) => declared.has(reading.package) &&
693
- readingFiles(host, parse, dir, reading.files).some(({ file }) => valuesAt(file, reading.property, reading.call).some((held) => holdsValue(held, reading.value))))
694
- .map((reading) => reading.package);
695
- }
696
- /**
697
- * The dependencies one manifest declares that an applied row's package keys use: each
698
- * whose name a string under a member of a key's name equals, or begins followed by `/` or
699
- * by `:` and a name. A member's own name is no such string.
700
- */
701
- function packageKeyUses(host, parse, dir, manifest, keys) {
702
- const declared = DEPENDENCY_SECTIONS.flatMap((section) => {
703
- const held = isRecord(manifest) ? manifest[section] : undefined;
704
- return isRecord(held) ? Object.keys(held) : [];
705
- });
706
- const strings = [];
707
- const collect = (node) => {
708
- const text = literalText(node);
709
- if (text !== undefined) {
710
- strings.push(text);
711
- }
712
- else if (isPropertyAssignment(node)) {
713
- collect(node.initializer);
714
- }
715
- else {
716
- node.forEachChild(collect);
717
- }
718
- };
719
- for (const { key, files } of keys) {
720
- const visit = (node) => {
721
- if (isPropertyAssignment(node) && keyOf(node) === key) {
722
- collect(node.initializer);
723
- }
724
- node.forEachChild(visit);
725
- };
726
- for (const { file } of readingFiles(host, parse, dir, files)) {
727
- const values = declaredValues(file);
728
- for (const one of defaultExports(file)) {
729
- const object = objectOf(one, values, true);
730
- if (object !== undefined) {
731
- visit(object);
732
- }
733
- }
734
- }
735
- }
736
- return declared.filter((name) => strings.some((text) => text === name ||
737
- text.startsWith(`${name}/`) ||
738
- (text.startsWith(`${name}:`) && text.length > name.length + 1)));
739
- }
740
- /**
741
- * What the rows decide over the manifests the analysis reads. A row `disabled` names
742
- * is skipped before anything is read for it.
743
- */
744
- export function readConventions(host, parse, manifests, disabled, rows = CONVENTION_ROWS) {
745
- const applied = [];
746
- const globs = [];
747
- const generated = [];
748
- const failures = [];
749
- const uses = new Set();
750
- const selected = new Map();
751
- const testFiles = new Set();
752
- for (const manifest of manifests) {
753
- const declared = readJSON(host, joinPath(manifest.dir, MANIFEST));
754
- const versions = new Map();
755
- const missing = new Set();
756
- for (const row of rows) {
757
- if (disabled.includes(row.name) || !declares(declared, row.package)) {
758
- continue;
759
- }
760
- if (!versions.has(row.package)) {
761
- versions.set(row.package, installedVersion(host, manifest.dir, row.package));
762
- }
763
- const version = versions.get(row.package);
764
- if (version === undefined) {
765
- if (missing.has(row.package) ||
766
- manifest.nested === true ||
767
- !declares(declared, row.package, REQUIRED_SECTIONS)) {
768
- continue;
769
- }
770
- missing.add(row.package);
771
- failures.push({
772
- dir: manifest.dir,
773
- failure: {
774
- failureClass: "missing-module",
775
- detail: `${manifest.path} declares ${row.package}, which no node_modules at or ` +
776
- `above its directory holds: run the package manager's install`,
777
- },
778
- });
779
- continue;
780
- }
781
- if (!satisfies(version, row.range)) {
782
- continue;
783
- }
784
- const resolved = new Map();
785
- let failed;
786
- for (const move of row.moves) {
787
- const result = resolveMove(host, parse, manifest.dir, row, move, resolved);
788
- if ("failure" in result) {
789
- failed = result.failure;
790
- break;
791
- }
792
- resolved.set(move.id, result.dirs);
793
- }
794
- if (failed !== undefined) {
795
- const where = dirnamePath(manifest.path);
796
- failures.push({
797
- dir: manifest.dir,
798
- failure: {
799
- failureClass: "convention-not-literal",
800
- detail: where === "." ? failed : `${where}/${failed}`,
801
- },
802
- });
803
- continue;
804
- }
805
- applied.push({
806
- name: row.name,
807
- package: row.package,
808
- version,
809
- manifest: manifest.path,
810
- });
811
- for (const name of shortNameUses(host, parse, manifest.dir, declared, row.shortNames ?? [])) {
812
- uses.add(name);
813
- }
814
- const prefixed = row.prefixedNames ?? [];
815
- for (const name of prefixedNameUses(host, parse, manifest.dir, declared, prefixed)) {
816
- uses.add(name);
817
- }
818
- const keys = row.packageKeys ?? [];
819
- for (const name of packageKeyUses(host, parse, manifest.dir, declared, keys)) {
820
- uses.add(name);
821
- }
822
- for (const name of manifestKeyUses(declared, row.manifestKeys ?? [])) {
823
- uses.add(name);
824
- }
825
- for (const name of helperUses(host, parse, manifest.dir, declared, row.helpers ?? [])) {
826
- uses.add(name);
827
- }
828
- selectedStrings(host, parse, manifest.dir, row.selections ?? [], selected);
829
- testFilesNamed(host, parse, manifest.dir, row.testFiles ?? [], testFiles);
830
- const excludes = [
831
- ...new Set((row.excludes ?? []).flatMap((one) => expand(one, resolved))),
832
- ].map(globExpression);
833
- for (const below of new Set((row.generated ?? []).flatMap((one) => expand(one, resolved)))) {
834
- generated.push({ dir: manifest.dir, row: row.name, below });
835
- }
836
- const written = [
837
- ...row.entries.flatMap((entry) => expand(entry, resolved)),
838
- ...manifestEntryGlobs(declared, row.manifestEntries ?? []),
839
- ];
840
- for (const glob of new Set(written)) {
841
- globs.push({
842
- dir: manifest.dir,
843
- row: row.name,
844
- expression: globExpression(glob),
845
- excludes,
846
- });
847
- }
848
- }
849
- }
850
- const unique = [...new Map(applied.map((one) => [JSON.stringify(one), one])).values()];
851
- unique.sort((a, b) => bytewise(a.name, b.name) ||
852
- bytewise(a.manifest, b.manifest) ||
853
- bytewise(JSON.stringify(a), JSON.stringify(b)));
854
- return {
855
- applied: unique,
856
- globs,
857
- generated,
858
- failures,
859
- uses: [...uses].sort(bytewise),
860
- selected,
861
- testFiles,
862
- };
863
- }
864
- /**
865
- * Every own file of one project below a directory an applied row names as generated, by
866
- * its path below the target root, with the name of the first such row.
867
- */
868
- export function generatedFiles(conventions, files) {
869
- const found = new Map();
870
- for (const { dir, row, below } of conventions.generated) {
871
- const root = joinPath(dir, below);
872
- for (const [path, file] of files.byPath) {
873
- if (!found.has(path) && relativePath(root, file.fileName) !== undefined) {
874
- found.set(path, row);
875
- }
876
- }
877
- }
878
- return found;
879
- }
880
- /** Every own file of one project an applied row's globs match, once per row. */
881
- export function conventionEntries(conventions, files) {
882
- const found = new Map();
883
- for (const glob of conventions.globs) {
884
- for (const file of files.byName.values()) {
885
- const matched = [file.fileName, ...files.writtenAs(file)].some((name) => {
886
- const path = relativePath(glob.dir, name);
887
- return (path !== undefined &&
888
- glob.expression.test(path) &&
889
- !glob.excludes.some((one) => one.test(path)));
890
- });
891
- if (matched) {
892
- found.set(`${file.fileName}\u0000${glob.row}`, { file, row: glob.row });
893
- }
894
- }
895
- }
896
- return [...found.values()];
897
- }
898
- /**
899
- * The failures the rows met that hold for one configuration: those of a manifest whose
900
- * directory holds the configuration file or one of the files the project owns.
901
- */
902
- export function conventionFailuresFor(conventions, configFile, ownFiles) {
903
- return conventions.failures
904
- .filter(({ dir }) => below(dir, dirnamePath(configFile)) !== undefined ||
905
- ownFiles.some((file) => relativePath(dir, file) !== undefined))
906
- .map(({ failure }) => failure);
907
- }