@cplieger/deadset-ts 8.0.0-dev.7 → 8.0.0-dev.8

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 (78) hide show
  1. package/dist/bin/node-host.js +21 -3
  2. package/dist/src/analysis.js +13 -14
  3. package/dist/src/config.js +6 -83
  4. package/dist/src/configured-declarations.js +15 -20
  5. package/dist/src/consumers.js +2 -2
  6. package/dist/src/contract/config.generated.js +544 -0
  7. package/dist/src/contract/report.generated.js +3 -2
  8. package/dist/src/discover.js +1 -1
  9. package/dist/src/edges.js +36 -112
  10. package/dist/src/exempt-classes.js +0 -2
  11. package/dist/src/exempt.js +1 -1
  12. package/dist/src/findings/completion.js +5 -4
  13. package/dist/src/findings/emitters.js +1 -1
  14. package/dist/src/findings/intra-function.js +1 -1
  15. package/dist/src/findings/non-code-artifacts.js +2 -2
  16. package/dist/src/findings/unused-declarations.js +1 -1
  17. package/dist/src/findings/visibility-narrowing.js +2 -2
  18. package/dist/src/findings-pass.js +1 -1
  19. package/dist/src/framework-lifecycle.js +7 -7
  20. package/dist/src/fs/document.js +59 -0
  21. package/dist/src/injection-container.js +1 -1
  22. package/dist/src/json-document.js +194 -67
  23. package/dist/src/kinds.js +0 -7
  24. package/dist/src/print.js +10 -79
  25. package/dist/src/report-schema.js +3 -224
  26. package/dist/src/resolve.js +245 -715
  27. package/dist/src/run.js +48 -55
  28. package/dist/src/schema.js +320 -344
  29. package/dist/src/scope.js +33 -101
  30. package/dist/src/serialization-contract.js +1 -1
  31. package/dist/src/suppress-file.js +15 -17
  32. package/dist/src/suppress.js +3 -3
  33. package/dist/src/template-field.js +4 -4
  34. package/dist/src/usage.js +10 -0
  35. package/dist/src/verbs/analyze.js +12 -15
  36. package/dist/src/verbs/explain.js +3 -3
  37. package/dist/src/verbs/print-projects.js +2 -2
  38. package/jsr.json +1 -1
  39. package/package.json +1 -1
  40. package/src/analysis.ts +30 -26
  41. package/src/config.ts +31 -298
  42. package/src/configured-declarations.ts +18 -23
  43. package/src/consumers.ts +4 -4
  44. package/src/contract/config.generated.ts +798 -0
  45. package/src/contract/report.generated.ts +3 -2
  46. package/src/discover.ts +1 -1
  47. package/src/edges.ts +39 -124
  48. package/src/exempt-classes.ts +0 -5
  49. package/src/exempt.ts +4 -4
  50. package/src/findings/completion.ts +5 -6
  51. package/src/findings/emitter.ts +2 -2
  52. package/src/findings/emitters.ts +1 -1
  53. package/src/findings/intra-function.ts +1 -1
  54. package/src/findings/non-code-artifacts.ts +4 -4
  55. package/src/findings/unused-declarations.ts +1 -1
  56. package/src/findings/visibility-narrowing.ts +2 -2
  57. package/src/findings-pass.ts +3 -3
  58. package/src/framework-lifecycle.ts +7 -7
  59. package/src/fs/document.ts +74 -0
  60. package/src/host.ts +12 -5
  61. package/src/injection-container.ts +1 -1
  62. package/src/json-document.ts +236 -76
  63. package/src/kinds.ts +0 -8
  64. package/src/print.ts +11 -92
  65. package/src/report-schema.ts +3 -299
  66. package/src/resolve.ts +281 -1105
  67. package/src/run.ts +50 -63
  68. package/src/schema.ts +391 -389
  69. package/src/scope.ts +39 -119
  70. package/src/serialization-contract.ts +1 -1
  71. package/src/suppress-file.ts +15 -20
  72. package/src/suppress.ts +3 -3
  73. package/src/template-field.ts +10 -7
  74. package/src/usage.ts +10 -0
  75. package/src/verbs/analyze.ts +12 -23
  76. package/src/verbs/explain.ts +3 -7
  77. package/src/verbs/print-projects.ts +2 -2
  78. package/src/verbs/verb.ts +2 -2
@@ -1,5 +1,5 @@
1
1
  import { randomBytes } from "node:crypto";
2
- import { closeSync, fchmodSync, fsyncSync, mkdtempSync, openSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, statSync, writeSync, } from "node:fs";
2
+ import { closeSync, fchmodSync, fsyncSync, mkdtempSync, openSync, readdirSync, readFileSync, readSync, realpathSync, renameSync, rmSync, statSync, writeSync, } from "node:fs";
3
3
  import { tmpdir } from "node:os";
4
4
  import { basename, dirname, extname, join } from "node:path";
5
5
  import { fileURLToPath } from "node:url";
@@ -77,6 +77,21 @@ function writeAtomically(path, pieces) {
77
77
  throw error;
78
78
  }
79
79
  }
80
+ /** The text of a file's first `limit` bytes, or of all of it where it holds fewer. */
81
+ function readHead(path, limit) {
82
+ const bytes = Buffer.alloc(limit);
83
+ const fd = openSync(path, "r");
84
+ try {
85
+ let held = 0;
86
+ for (let got = -1; got !== 0 && held < limit; held += got) {
87
+ got = readSync(fd, bytes, held, limit - held, null);
88
+ }
89
+ return bytes.toString("utf8", 0, held);
90
+ }
91
+ finally {
92
+ closeSync(fd);
93
+ }
94
+ }
80
95
  /** A new directory under the platform's temporary directory. */
81
96
  function temporaryDirectory() {
82
97
  const path = mkdtempSync(join(tmpdir(), `${PRODUCT}-mapper-`));
@@ -106,7 +121,7 @@ function componentMapperCommand() {
106
121
  export function nodeHost() {
107
122
  return {
108
123
  workingDirectory: () => process.cwd(),
109
- readFile: (path) => readFileSync(path, "utf8"),
124
+ readFile: (path, limit) => limit === undefined ? readFileSync(path, "utf8") : readHead(path, limit),
110
125
  readDirectory: (path) => readdirSync(path, { withFileTypes: true }).map((entry) => ({
111
126
  name: entry.name,
112
127
  directory: entry.isDirectory(),
@@ -116,7 +131,10 @@ export function nodeHost() {
116
131
  if (stat === undefined) {
117
132
  return "absent";
118
133
  }
119
- return stat.isDirectory() ? "directory" : "file";
134
+ if (stat.isDirectory()) {
135
+ return "directory";
136
+ }
137
+ return stat.isFile() ? "file" : "other";
120
138
  },
121
139
  realPath: (path) => realpathSync(path),
122
140
  analyzerVersion: () => {
@@ -56,7 +56,6 @@ import { ownErrors, workspaceRun } from "./workspace-run.js";
56
56
  import { bytewise } from "./text/compare.js";
57
57
  import { holdsManifest, inInstalledPackages, MANIFEST, manifestAt } from "./fs/manifest.js";
58
58
  import { globExpression } from "./text/glob.js";
59
- import { LANGUAGE } from "./product.js";
60
59
  /** The setting whose source decides where a finding about a configured root sits. */
61
60
  const ROOTS_SETTING = "roots.patterns";
62
61
  /**
@@ -141,13 +140,13 @@ function readProjects(engine, host, scope, config, stage) {
141
140
  // The nested packages are known only once every program is open, and every visit reads
142
141
  // the conventions and options they decide, so both are set in the session's first step.
143
142
  let nestedDirs = [];
144
- let conventions = readConventions(host, parse, [], config.ts.disabledConventions);
143
+ let conventions = readConventions(host, parse, [], config.ts.disabled_conventions);
145
144
  let options;
146
145
  const readManifests = (opened) => {
147
146
  nestedDirs = nestedPackageDirs(host, normalizePath(absoluteRoot), opened
148
147
  .filter((one) => !consumerOf.has(one.configFile))
149
148
  .flatMap((one) => [one.configFile, ...one.fileNames()]), new Set(memberDirs));
150
- conventions = readConventions(host, parse, conventionManifests(host, absoluteRoot, memberDirs, nestedDirs), config.ts.disabledConventions);
149
+ conventions = readConventions(host, parse, conventionManifests(host, absoluteRoot, memberDirs, nestedDirs), config.ts.disabled_conventions);
151
150
  options = {
152
151
  host,
153
152
  manifest: workspace?.manifest ?? readManifest(host, targetRoot),
@@ -155,12 +154,12 @@ function readProjects(engine, host, scope, config, stage) {
155
154
  ...(workspace?.entries ?? []),
156
155
  ...packageEntries(host, normalizePath(absoluteRoot), nestedDirs),
157
156
  ],
158
- patterns: config.rootPatterns,
159
- entryFiles: config.ts.entryFiles,
157
+ patterns: config.roots.patterns,
158
+ entryFiles: config.ts.entry_files,
160
159
  conventions,
161
- testFiles: config.ts.testFiles,
160
+ testFiles: config.ts.test_files,
162
161
  parse,
163
- publishedAPI: config.targetKind !== "" && kindOf(config.targetKind, LANGUAGE) === "library",
162
+ publishedAPI: config.target.kind !== undefined && kindOf(config.target.kind) === "library",
164
163
  publishedMembers: new Set((workspace?.entries ?? []).filter((entry) => entry.published).map((entry) => entry.dir)),
165
164
  runFiles: new Set(opened.flatMap((one) => one.fileNames().map((name) => resolvePath(host.workingDirectory(), name)))),
166
165
  };
@@ -190,7 +189,7 @@ function readProjects(engine, host, scope, config, stage) {
190
189
  unbuildable.push(...met);
191
190
  }
192
191
  };
193
- const testPatterns = config.ts.testFiles.map(globExpression);
192
+ const testPatterns = config.ts.test_files.map(globExpression);
194
193
  const joins = discovered.derived
195
194
  ? joinedTestFiles(engine, host, absoluteRoot, discovered.configFiles, testPatterns)
196
195
  : new Map();
@@ -399,7 +398,7 @@ export function runRoots(engine, host, scope, config, provenance) {
399
398
  const ordered = [...merged.values()].sort((a, b) => byPosition(a.position, b.position) ||
400
399
  bytewise(a.kind, b.kind) ||
401
400
  bytewise(a.source, b.source));
402
- const unmatched = unmatchedEverywhere(config.rootPatterns, projects.map((answer) => answer.rooted));
401
+ const unmatched = unmatchedEverywhere(config.roots.patterns, projects.map((answer) => answer.rooted));
403
402
  return {
404
403
  configurations,
405
404
  roots: ordered,
@@ -468,13 +467,13 @@ function readRun(engine, host, scope, config, mode, detectors, extra) {
468
467
  const targetRoot = scope.target.path;
469
468
  const disabled = disabledClasses(config);
470
469
  const templates = disabled.has("template-field")
471
- ? { delimiters: config.analysis.templateDelimiters, files: [] }
470
+ ? { delimiters: config.analysis.template_delimiters, files: [] }
472
471
  : readTemplates(host, targetRoot, config.analysis);
473
472
  const consumers = scope.consumers.map((consumer) => consumer.path);
474
473
  const read = readProjects(engine, host, scope, config, (project, opened) => {
475
474
  const projectRead = opened.read();
476
475
  const resolved = references(project, projectRead.held, targetRoot, {
477
- testFiles: config.ts.testFiles,
476
+ testFiles: config.ts.test_files,
478
477
  testFileNames: projectRead.testFileNames,
479
478
  });
480
479
  const evidence = evidenceRecords({
@@ -484,7 +483,7 @@ function readRun(engine, host, scope, config, mode, detectors, extra) {
484
483
  templates,
485
484
  ts: config.ts,
486
485
  consumers,
487
- ...(config.analysis.generatedFiles === "include"
486
+ ...(config.analysis.generated_files === "include"
488
487
  ? {}
489
488
  : { generated: projectRead.generated }),
490
489
  }, detectors, disabled);
@@ -904,7 +903,7 @@ function readAnalysis(engine, host, scope, config, provenance, mode, detectors)
904
903
  id: one.configured.configuration,
905
904
  configFile: one.configFile,
906
905
  })),
907
- testFileRules: testFileRulesOf(config.ts.testFiles, read.projects.flatMap((one) => one.configured.testFiles), read.projects.flatMap((one) => one.configured.supportFiles ?? [])),
906
+ testFileRules: testFileRulesOf(config.ts.test_files, read.projects.flatMap((one) => one.configured.testFiles), read.projects.flatMap((one) => one.configured.supportFiles ?? [])),
908
907
  typeErrorSkips: read.projects.flatMap((one) => one.typeErrorSkips),
909
908
  componentWarnings: [
910
909
  ...new Map(read.projects
@@ -934,7 +933,7 @@ function inputsOver(host, scope, config, provenance, mode, read, documents) {
934
933
  };
935
934
  const facts = {
936
935
  refusals: suppressions.refusals,
937
- unmatchedRoots: unmatchedEverywhere(config.rootPatterns, read.projects.map((one) => one.extra.rooted)),
936
+ unmatchedRoots: unmatchedEverywhere(config.roots.patterns, read.projects.map((one) => one.extra.rooted)),
938
937
  rootsDocument: documentOf(host, provenance, ROOTS_SETTING, targetRoot),
939
938
  unmatchedDeclarations: configuredDeclarations(config)
940
939
  .filter((_, at) => !read.projects.some((one) => one.extra.declarationsNamed[at] === true))
@@ -1,75 +1,18 @@
1
- import { CONTRACT_VERSION, } from "./contract/contract.generated.js";
2
- import { LANGUAGE, PRODUCT } from "./product.js";
1
+ import { SEVERITIES } from "./contract/contract.generated.js";
2
+ import { LANGUAGE } from "./product.js";
3
3
  /**
4
4
  * The name of the repository configuration at the target root, which is also the
5
5
  * document a finding about the configuration names where no document of the
6
6
  * target supplied the setting it is about.
7
7
  */
8
8
  export const REPOSITORY_DOCUMENT = "deadset.json";
9
- /** The kind a target kind setting gives one language's declarations. */
10
- export function kindOf(setting, language) {
11
- return typeof setting === "string" ? setting : setting[language];
9
+ /** The kind a target kind setting gives this analyzer's declarations. */
10
+ export function kindOf(kind) {
11
+ return typeof kind === "string" ? kind : kind[LANGUAGE];
12
12
  }
13
- /** The severities ranked, the strongest highest. */
14
- const SEVERITY_RANK = { allow: 0, warn: 1, deny: 2 };
15
13
  /** Whether a finding at one severity fails a run under the failing severity. */
16
14
  export function fails(severity, failOn) {
17
- return SEVERITY_RANK[severity] >= SEVERITY_RANK[failOn];
18
- }
19
- /**
20
- * The configuration every documented default states. `targetKind` is empty: the
21
- * field has no default and is never inferred.
22
- */
23
- export function defaultConfig() {
24
- return {
25
- contractVersion: CONTRACT_VERSION,
26
- targetKind: "",
27
- analysis: {
28
- languages: [],
29
- minConfidence: "probable",
30
- generatedFiles: "exclude",
31
- consumerTests: "test",
32
- configurations: [],
33
- matrixComplete: false,
34
- templateDirs: [],
35
- templateDelimiters: { left: "{{", right: "}}" },
36
- },
37
- consumersComplete: false,
38
- rootPatterns: [],
39
- severity: new Map(),
40
- exemptionsDisabled: [],
41
- reporters: {
42
- formats: ["text"],
43
- sort: "position",
44
- cascade: "roots",
45
- maxFindings: 0,
46
- failOn: "deny",
47
- },
48
- providers: {
49
- analyzers: [
50
- { shape: "installed", name: "deadset-go", languages: ["go"], command: "deadset-go" },
51
- { shape: "installed", name: PRODUCT, languages: [LANGUAGE], command: PRODUCT },
52
- ],
53
- },
54
- ts: {
55
- testFiles: [
56
- "**/*.test.{ts,tsx,mts,cts}",
57
- "**/*.test-d.{ts,tsx,mts,cts}",
58
- "**/*.spec-d.{ts,tsx,mts,cts}",
59
- "**/*.spec.*",
60
- "**/__tests__/**",
61
- "**/__mocks__/**",
62
- "**/test-d/**",
63
- ],
64
- entryFiles: [],
65
- componentExtensions: [".vue", ".svelte", ".astro"],
66
- disabledConventions: [],
67
- injectionRegistrations: [],
68
- lifecycleContracts: [],
69
- serializers: [],
70
- rootsComplete: false,
71
- },
72
- };
15
+ return SEVERITIES.indexOf(severity) >= SEVERITIES.indexOf(failOn);
73
16
  }
74
17
  /**
75
18
  * Renders the origin as the provenance value the Contract declares: `default`,
@@ -78,23 +21,3 @@ export function defaultConfig() {
78
21
  export function renderOrigin(origin) {
79
22
  return origin.source === "default" ? "default" : `${origin.source}: ${origin.label}`;
80
23
  }
81
- /**
82
- * Every refusal configuration resolution makes. Each maps to the usage exit
83
- * code, and each is thrown rather than returned: a refusal ends the run, and the
84
- * one caller that turns it into an exit code is the command line.
85
- */
86
- export class ConfigError extends Error {
87
- /** Which refusal this is. */
88
- kind;
89
- /**
90
- * The key or field the message names, spelled as the document spells it. It is
91
- * empty when the refusal names none.
92
- */
93
- key;
94
- constructor(kind, key, message) {
95
- super(message);
96
- this.name = "ConfigError";
97
- this.kind = kind;
98
- this.key = key;
99
- }
100
- }
@@ -179,19 +179,19 @@ export function resolveEntries(project, held, entries) {
179
179
  for (const symbol of held.symbols) {
180
180
  byRef.set(symbol.ref, [...(byRef.get(symbol.ref) ?? []), symbol.id]);
181
181
  }
182
- const modules = modulesNamed(project, new Set(entries.flatMap((entry) => (entry.shape === "module" ? [entry.module] : []))));
182
+ const modules = modulesNamed(project, new Set(entries.flatMap((entry) => ("module" in entry ? [entry.module] : []))));
183
183
  return entries.map((entry) => {
184
- if (entry.shape === "symbol") {
184
+ if ("symbol" in entry) {
185
185
  return { entry, handles: new Set(), ids: new Set(byRef.get(entry.symbol) ?? []) };
186
186
  }
187
- const { components, static: isStatic } = componentsOf(entry.shape === "module" ? entry.name : entry.global);
187
+ const { components, static: isStatic } = componentsOf("module" in entry ? entry.name : entry.global);
188
188
  const [first = "", ...rest] = components;
189
189
  let start;
190
190
  if (isStatic && rest.length === 0) {
191
191
  // Only a member is static, and a path of one component names no member.
192
192
  start = undefined;
193
193
  }
194
- else if (entry.shape === "module") {
194
+ else if ("module" in entry) {
195
195
  const module = modules.get(entry.module);
196
196
  start = module === undefined ? undefined : memberOf(project, module, first, false);
197
197
  }
@@ -246,14 +246,10 @@ export function entriesAt(project, chains, nodes, resolved) {
246
246
  * program the display name the inventory gives it.
247
247
  */
248
248
  export function spelledEntry(entry, held) {
249
- switch (entry.shape) {
250
- case "symbol":
251
- return held.symbols.find((symbol) => symbol.ref === entry.symbol)?.name ?? entry.symbol;
252
- case "module":
253
- return entry.name;
254
- case "global":
255
- return entry.global;
249
+ if ("symbol" in entry) {
250
+ return held.symbols.find((symbol) => symbol.ref === entry.symbol)?.name ?? entry.symbol;
256
251
  }
252
+ return "module" in entry ? entry.name : entry.global;
257
253
  }
258
254
  /** The key under which a lifecycle contract's declarations are configured. */
259
255
  const LIFECYCLE_KEY = "ts.lifecycle_contracts";
@@ -264,11 +260,14 @@ const LIFECYCLE_KEY = "ts.lifecycle_contracts";
264
260
  */
265
261
  export function configuredDeclarations(config) {
266
262
  return [
267
- ...config.ts.injectionRegistrations.map((entry) => ({
263
+ ...config.ts.injection_registrations.map((entry) => ({
268
264
  key: "ts.injection_registrations",
269
265
  entry,
270
266
  })),
271
- ...config.ts.lifecycleContracts.flatMap((contract) => [...contract.components, ...contract.bases].map((entry) => ({ key: LIFECYCLE_KEY, entry }))),
267
+ ...config.ts.lifecycle_contracts.flatMap((contract) => [...(contract.components ?? []), ...(contract.bases ?? [])].map((entry) => ({
268
+ key: LIFECYCLE_KEY,
269
+ entry,
270
+ }))),
272
271
  ...config.ts.serializers.map((entry) => ({ key: "ts.serializers", entry })),
273
272
  ];
274
273
  }
@@ -294,12 +293,8 @@ export function declarationsNamed(project, held, configured) {
294
293
  * sign and its name, and a global entry's path after a number sign.
295
294
  */
296
295
  export function configuredRef(entry) {
297
- switch (entry.shape) {
298
- case "symbol":
299
- return entry.symbol;
300
- case "module":
301
- return `${entry.module}#${entry.name}`;
302
- case "global":
303
- return `#${entry.global}`;
296
+ if ("symbol" in entry) {
297
+ return entry.symbol;
304
298
  }
299
+ return "module" in entry ? `${entry.module}#${entry.name}` : `#${entry.global}`;
305
300
  }
@@ -39,10 +39,10 @@ export function consumerLoads(engine, host, scope, targetConfigFiles) {
39
39
  export function consumerReferences(project, host, targetRoot, consumer, config) {
40
40
  const held = inventory(narrowedTo(project, (file) => relativePath(targetRoot, file.fileName) !== undefined), host, targetRoot);
41
41
  const found = references(project, held, targetRoot, {
42
- testFiles: config.ts.testFiles,
42
+ testFiles: config.ts.test_files,
43
43
  consumer,
44
44
  }).references;
45
- if (config.analysis.consumerTests !== "production") {
45
+ if (config.analysis.consumer_tests !== "production") {
46
46
  return found;
47
47
  }
48
48
  return found.map((reference) => (reference.test ? { ...reference, test: false } : reference));