lawspec 0.31.1 → 0.31.3

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 (79) hide show
  1. package/CHANGELOG.md +89 -2
  2. package/README.md +85 -9
  3. package/api.mjs +14 -9
  4. package/authoring.mjs +899 -0
  5. package/beam-coverage.mjs +190 -65
  6. package/beam-doctor.mjs +343 -124
  7. package/bin/lawspec.mjs +1949 -661
  8. package/build.json +134 -128
  9. package/component-packages.mjs +662 -176
  10. package/component.d.ts +82 -20
  11. package/component.mjs +317 -94
  12. package/core.wasm +2 -2
  13. package/core_jsffi.js +5 -5
  14. package/doctor.mjs +558 -299
  15. package/examples-command.mjs +44 -23
  16. package/failure-database.mjs +60 -17
  17. package/files.mjs +122 -62
  18. package/index.d.ts +585 -312
  19. package/input-files.mjs +90 -25
  20. package/launcher.mjs +72 -31
  21. package/learn/coverage.json +184 -112
  22. package/learn/curriculum.json +4 -1
  23. package/learn/vendor/fantomas/{LawSpec.Learn.Formatter.5arcua4vy0.wasm → LawSpec.Learn.Formatter.poiijdbsl9.wasm} +0 -0
  24. package/learn/vendor/fantomas/dotnet.js +3 -3
  25. package/learn/vendor/manifest.json +4 -8
  26. package/lsp.mjs +444 -133
  27. package/mcp.mjs +250 -0
  28. package/native-evidence.mjs +668 -182
  29. package/native-examples.mjs +56 -19
  30. package/native-host.mjs +136 -42
  31. package/native-parsers/clojure.LICENSE +121 -0
  32. package/native-parsers/clojure.wasm +0 -0
  33. package/native-parsers/csharp.LICENSE +21 -0
  34. package/native-parsers/csharp.wasm +0 -0
  35. package/native-parsers/elixir.LICENSE +176 -0
  36. package/native-parsers/elixir.wasm +0 -0
  37. package/native-parsers/erlang.LICENSE +201 -0
  38. package/native-parsers/erlang.wasm +0 -0
  39. package/native-parsers/fsharp.LICENSE +21 -0
  40. package/native-parsers/fsharp.wasm +0 -0
  41. package/native-parsers/gleam.LICENSE +202 -0
  42. package/native-parsers/gleam.wasm +0 -0
  43. package/native-parsers/go.LICENSE +21 -0
  44. package/native-parsers/go.wasm +0 -0
  45. package/native-parsers/groovy.LICENSE +21 -0
  46. package/native-parsers/groovy.wasm +0 -0
  47. package/native-parsers/haskell.LICENSE +21 -0
  48. package/native-parsers/haskell.wasm +0 -0
  49. package/native-parsers/java.LICENSE +21 -0
  50. package/native-parsers/java.wasm +0 -0
  51. package/native-parsers/javascript.LICENSE +21 -0
  52. package/native-parsers/javascript.wasm +0 -0
  53. package/native-parsers/kotlin.LICENSE +9 -0
  54. package/native-parsers/kotlin.wasm +0 -0
  55. package/native-parsers/lock.json +114 -0
  56. package/native-parsers/prolog.LICENSE +21 -0
  57. package/native-parsers/prolog.wasm +0 -0
  58. package/native-parsers/python.LICENSE +21 -0
  59. package/native-parsers/python.wasm +0 -0
  60. package/native-parsers/rust.LICENSE +21 -0
  61. package/native-parsers/rust.wasm +0 -0
  62. package/native-parsers/scala.LICENSE +21 -0
  63. package/native-parsers/scala.wasm +0 -0
  64. package/native-parsers/swift.LICENSE +21 -0
  65. package/native-parsers/swift.wasm +0 -0
  66. package/native-parsers/typescript.LICENSE +21 -0
  67. package/native-parsers/typescript.wasm +0 -0
  68. package/node-events-reporter.mjs +41 -17
  69. package/package.json +4 -3
  70. package/project.mjs +338 -0
  71. package/proof.d.ts +93 -19
  72. package/proof.mjs +1538 -447
  73. package/read-only-hpack.mjs +6 -5
  74. package/scala-build-state.mjs +131 -41
  75. package/scala-coverage.mjs +57 -34
  76. package/scalars.mjs +23 -8
  77. package/templates.mjs +566 -617
  78. package/test-command.mjs +1671 -521
  79. package/learn/vendor/fantomas/LawSpec.Learn.Formatter.u1ne3a6xd2.wasm +0 -0
package/beam-coverage.mjs CHANGED
@@ -1,17 +1,39 @@
1
1
  // Generated from templates/npm/beam-coverage.mjs by lawspec-dev generate. Do not edit.
2
- import {execFile} from "node:child_process";
3
- import {createHash} from "node:crypto";
4
- import {mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile} from "node:fs/promises";
5
- import os from "node:os";
6
- import path from "node:path";
7
- import {promisify} from "node:util";
2
+ import {execFile} from 'node:child_process';
3
+ import {createHash} from 'node:crypto';
4
+ import {
5
+ mkdir,
6
+ mkdtemp,
7
+ readFile,
8
+ readdir,
9
+ rm,
10
+ stat,
11
+ writeFile,
12
+ } from 'node:fs/promises';
13
+ import os from 'node:os';
14
+ import path from 'node:path';
15
+ import {promisify} from 'node:util';
8
16
 
9
17
  const exec = promisify(execFile);
10
- export function beamCoverageRequirement(target, report) {
11
- if (target.language === "gleam" && Number(report?.versions?.gleam?.split(".")[1] ?? 0) < 19)
12
- return {tool: "Gleam 1.19 for source coverage", install: "upgrade Gleam to 1.19 so native counters refer to the original source lines"};
13
- if (target.language === "elixir" && report?.coverage?.tool !== "Mix.Tasks.Test.Coverage")
14
- return {tool: "Mix.Tasks.Test.Coverage", install: "set test_coverage[:tool] to Mix.Tasks.Test.Coverage in mix.exs"};
18
+ export function beamCoverageRequirement(target, report)
19
+ {
20
+ if (
21
+ target.language === 'gleam' &&
22
+ Number(report?.versions?.gleam?.split('.')[1] ?? 0) < 19
23
+ )
24
+ return {
25
+ tool: 'Gleam 1.19 for source coverage',
26
+ install:
27
+ 'upgrade Gleam to 1.19 so native counters refer to the original source lines',
28
+ };
29
+ if (
30
+ target.language === 'elixir' &&
31
+ report?.coverage?.tool !== 'Mix.Tasks.Test.Coverage'
32
+ )
33
+ return {
34
+ tool: 'Mix.Tasks.Test.Coverage',
35
+ install: 'set test_coverage[:tool] to Mix.Tasks.Test.Coverage in mix.exs',
36
+ };
15
37
  return null;
16
38
  }
17
39
 
@@ -60,101 +82,204 @@ source(Module, Beam) ->
60
82
  text(Value) -> unicode:characters_to_list(Value).
61
83
  `;
62
84
 
63
- const escape = (value) => String(value).replace(/[&<>"']/g, (c) =>
64
- ({"&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"}[c]));
65
- const totals = (lines) => ({covered: lines.filter(({hits}) => hits > 0).length, total: lines.length});
66
- const percentage = ({covered, total}) => total ? `${(100 * covered / total).toFixed(1)}%` : "no executable lines";
67
- const page = (title, body) => `<!doctype html><html lang="en"><meta charset="utf-8"><title>${escape(title)}</title>
85
+ const escape = value =>
86
+ String(value).replace(
87
+ /[&<>"']/g,
88
+ c =>
89
+ ({'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'})[
90
+ c
91
+ ],
92
+ );
93
+ const totals = lines => ({
94
+ covered: lines.filter(({hits}) => hits > 0).length,
95
+ total: lines.length,
96
+ });
97
+ const percentage = ({covered, total}) =>
98
+ total ? `${((100 * covered) / total).toFixed(1)}%` : 'no executable lines';
99
+ const page = (
100
+ title,
101
+ body,
102
+ ) => `<!doctype html><html lang="en"><meta charset="utf-8"><title>${escape(title)}</title>
68
103
  <style>body{font:16px system-ui;margin:2rem;color:#222}table{border-collapse:collapse;width:100%}td,th{padding:.3rem .7rem;text-align:left;border-bottom:1px solid #ddd}pre{margin:0;tab-size:4}.hit{background:#e4f5e9}.miss{background:#ffe6e6}.line,.count{width:4rem;text-align:right;color:#555}a{color:#174da8}</style>
69
104
  <h1>${escape(title)}</h1>${body}</html>\n`;
70
105
 
71
106
  // Only exports explicitly returned by this run's invocations are imported.
72
107
  // A missing export is an error even if the native runner exited successfully.
73
- export async function collectBeamCoverage({root, directory, runs}) {
108
+ export async function collectBeamCoverage({root, directory, runs})
109
+ {
74
110
  if (!runs.length) return null;
75
111
  root = path.resolve(root);
76
112
  directory = path.resolve(directory);
77
- const temporary = await mkdtemp(path.join(os.tmpdir(), "lawspec-beam-coverage-"));
78
- try {
113
+ const temporary = await mkdtemp(
114
+ path.join(os.tmpdir(), 'lawspec-beam-coverage-'),
115
+ );
116
+ try
117
+ {
79
118
  const inputs = [];
80
119
  const seen = new Set();
81
120
  let rebarDirectories;
82
- for (const run of runs) {
83
- if (!run.run || seen.has(run.run) || path.basename(run.file) !== `${run.run}.coverdata`)
84
- throw new Error("Invalid or duplicate BEAM coverage invocation");
121
+ for (const run of runs)
122
+ {
123
+ if (
124
+ !run.run ||
125
+ seen.has(run.run) ||
126
+ path.basename(run.file) !== `${run.run}.coverdata`
127
+ )
128
+ throw new Error('Invalid or duplicate BEAM coverage invocation');
85
129
  seen.add(run.run);
86
130
  const info = await stat(run.file).catch(() => null);
87
- if (!info?.isFile() || !info.size) throw new Error(`Native runner did not export coverage for ${run.run}: ${run.file}`);
131
+ if (!info?.isFile() || !info.size)
132
+ throw new Error(
133
+ `Native runner did not export coverage for ${run.run}: ${run.file}`,
134
+ );
88
135
  let beams;
89
- if (run.tool === "rebar3" && !rebarDirectories) {
136
+ if (run.tool === 'rebar3' && !rebarDirectories)
137
+ {
90
138
  // Before the first compile Rebar only knows dependency ebin paths.
91
139
  // Ask after the test run so fresh and relocated applications are found.
92
- const result = await exec("rebar3", ["as", "test", "path", "--ebin", "--separator", "\n"], {cwd: root});
93
- rebarDirectories = result.stdout.trim().split("\n").filter(Boolean);
94
- if (!rebarDirectories.length || !rebarDirectories.every((item) => path.isAbsolute(item)))
95
- throw new Error("Cannot resolve Rebar's application beam directories after coverage");
140
+ const result = await exec(
141
+ 'rebar3',
142
+ ['as', 'test', 'path', '--ebin', '--separator', '\n'],
143
+ {cwd: root},
144
+ );
145
+ rebarDirectories = result.stdout.trim().split('\n').filter(Boolean);
146
+ if (
147
+ !rebarDirectories.length ||
148
+ !rebarDirectories.every(item => path.isAbsolute(item))
149
+ )
150
+ throw new Error(
151
+ "Cannot resolve Rebar's application beam directories after coverage",
152
+ );
96
153
  }
97
- const directories = run.tool === "rebar3" ? rebarDirectories
98
- : run.beamDirectories ?? (run.beamDirectory ? [run.beamDirectory] : []);
99
- for (const directory of directories) {
100
- const entries = (await readdir(directory)).filter((name) => name.endsWith(".beam"));
101
- beams = {...beams, ...Object.fromEntries(entries.map((name) => [name.slice(0, -5), path.resolve(directory, name)]))};
154
+ const directories =
155
+ run.tool === 'rebar3'
156
+ ? rebarDirectories
157
+ : (run.beamDirectories ??
158
+ (run.beamDirectory ? [run.beamDirectory] : []));
159
+ for (const directory of directories)
160
+ {
161
+ const entries = (await readdir(directory)).filter(name =>
162
+ name.endsWith('.beam'),
163
+ );
164
+ beams = {
165
+ ...beams,
166
+ ...Object.fromEntries(
167
+ entries.map(name => [
168
+ name.slice(0, -5),
169
+ path.resolve(directory, name),
170
+ ]),
171
+ ),
172
+ };
102
173
  }
103
- if (run.metadata) {
104
- const metadata = JSON.parse(await readFile(run.metadata, "utf8"));
105
- if (metadata.run !== run.run || metadata.file !== run.file || !metadata.modules?.length)
106
- throw new Error(`Gleam coverage metadata does not match invocation ${run.run}`);
107
- beams = Object.fromEntries(metadata.modules.map(({name, beam}) => [name, beam]));
174
+ if (run.metadata)
175
+ {
176
+ const metadata = JSON.parse(await readFile(run.metadata, 'utf8'));
177
+ if (
178
+ metadata.run !== run.run ||
179
+ metadata.file !== run.file ||
180
+ !metadata.modules?.length
181
+ )
182
+ throw new Error(
183
+ `Gleam coverage metadata does not match invocation ${run.run}`,
184
+ );
185
+ beams = Object.fromEntries(
186
+ metadata.modules.map(({name, beam}) => [name, beam]),
187
+ );
108
188
  }
109
189
  inputs.push({file: path.resolve(run.file), ...(beams ? {beams} : {})});
110
190
  }
111
191
  // Assemble all pages off to the side. A failed import or source mapping
112
192
  // cannot leave an apparently complete index behind.
113
- const pages = path.join(temporary, "modules");
193
+ const pages = path.join(temporary, 'modules');
114
194
  await mkdir(pages);
115
- const input = path.join(temporary, "input.json"), output = path.join(temporary, "output.json");
116
- const script = path.join(temporary, "collect.escript");
195
+ const input = path.join(temporary, 'input.json'),
196
+ output = path.join(temporary, 'output.json');
197
+ const script = path.join(temporary, 'collect.escript');
117
198
  await writeFile(script, collector);
118
199
  await writeFile(input, JSON.stringify({runs: inputs, directory: pages}));
119
- await exec("escript", [script, input, output], {cwd: root, maxBuffer: 8 * 1024 * 1024});
120
- const modules = JSON.parse(await readFile(output, "utf8"));
121
- for (const module of modules) {
200
+ await exec('escript', [script, input, output], {
201
+ cwd: root,
202
+ maxBuffer: 8 * 1024 * 1024,
203
+ });
204
+ const modules = JSON.parse(await readFile(output, 'utf8'));
205
+ for (const module of modules)
206
+ {
122
207
  module.summary = totals(module.lines);
123
208
  if (!module.source) continue;
124
209
  let source = path.resolve(root, module.source);
125
- const text = await readFile(source, "utf8");
210
+ const text = await readFile(source, 'utf8');
126
211
  // Gleam copies foreign Erlang sources into its build tree. Prefer the
127
212
  // original when it is byte-for-byte the source that was compiled.
128
213
  const marker = `${path.sep}_gleam_artefacts${path.sep}`;
129
- if (source.includes(marker)) {
130
- const original = path.join(root, "src", source.split(marker).at(-1));
131
- if (await readFile(original, "utf8").catch(() => null) === text) source = original;
214
+ if (source.includes(marker))
215
+ {
216
+ const original = path.join(root, 'src', source.split(marker).at(-1));
217
+ if ((await readFile(original, 'utf8').catch(() => null)) === text)
218
+ source = original;
132
219
  }
133
220
  module.source = path.relative(root, source);
134
221
  const sourceLines = text.split(/\r?\n/);
135
222
  if (module.lines.some(({line}) => line > sourceLines.length))
136
223
  throw new Error(`Coverage lines do not match source ${module.source}`);
137
224
  const hits = new Map(module.lines.map(({line, hits}) => [line, hits]));
138
- module.report = createHash("sha256").update(module.name).digest("hex") + ".html";
139
- const rows = sourceLines.map((line, index) => {
140
- const count = hits.get(index + 1);
141
- return `<tr class="${count === undefined ? "" : count ? "hit" : "miss"}" id="L${index + 1}"><td class="line">${index + 1}</td><td class="count">${count ?? ""}</td><td><pre>${escape(line)}</pre></td></tr>`;
142
- }).join("\n");
143
- await writeFile(path.join(pages, module.report), page(module.name,
144
- `<p>${escape(module.source)} — ${percentage(module.summary)}</p><table>${rows}</table>`));
225
+ module.report =
226
+ createHash('sha256').update(module.name).digest('hex') + '.html';
227
+ const rows = sourceLines
228
+ .map((line, index) =>
229
+ {
230
+ const count = hits.get(index + 1);
231
+ return `<tr class="${count === undefined ? '' : count ? 'hit' : 'miss'}" id="L${index + 1}"><td class="line">${index + 1}</td><td class="count">${count ?? ''}</td><td><pre>${escape(line)}</pre></td></tr>`;
232
+ })
233
+ .join('\n');
234
+ await writeFile(
235
+ path.join(pages, module.report),
236
+ page(
237
+ module.name,
238
+ `<p>${escape(module.source)} — ${percentage(module.summary)}</p><table>${rows}</table>`,
239
+ ),
240
+ );
145
241
  }
146
- const summary = modules.reduce((sum, module) => ({covered: sum.covered + module.summary.covered,
147
- total: sum.total + module.summary.total}), {covered: 0, total: 0});
148
- const result = {version: 1, root, runs: runs.map(({run}) => run), summary, modules};
149
- const rows = modules.map((module) => `<tr><td><a href="modules/${module.report}">${escape(module.name)}</a></td><td>${module.summary.covered}/${module.summary.total}</td><td>${percentage(module.summary)}</td></tr>`).join("\n");
150
- await mkdir(path.join(directory, "modules"), {recursive: true});
242
+ const summary = modules.reduce(
243
+ (sum, module) => ({
244
+ covered: sum.covered + module.summary.covered,
245
+ total: sum.total + module.summary.total,
246
+ }),
247
+ {covered: 0, total: 0},
248
+ );
249
+ const result = {
250
+ version: 1,
251
+ root,
252
+ runs: runs.map(({run}) => run),
253
+ summary,
254
+ modules,
255
+ };
256
+ const rows = modules
257
+ .map(
258
+ module =>
259
+ `<tr><td><a href="modules/${module.report}">${escape(module.name)}</a></td><td>${module.summary.covered}/${module.summary.total}</td><td>${percentage(module.summary)}</td></tr>`,
260
+ )
261
+ .join('\n');
262
+ await mkdir(path.join(directory, 'modules'), {recursive: true});
151
263
  for (const module of modules)
152
- await writeFile(path.join(directory, "modules", module.report), await readFile(path.join(pages, module.report)));
153
- await writeFile(path.join(directory, "coverage.json"), JSON.stringify(result, null, 2) + "\n");
154
- await writeFile(path.join(directory, "index.html"), page("BEAM line coverage",
155
- `<p>${summary.covered}/${summary.total} executable lines covered (${percentage(summary)}); ${runs.length} native run(s).</p><table><thead><tr><th>Module</th><th>Lines</th><th>Coverage</th></tr></thead><tbody>${rows}</tbody></table>`));
264
+ await writeFile(
265
+ path.join(directory, 'modules', module.report),
266
+ await readFile(path.join(pages, module.report)),
267
+ );
268
+ await writeFile(
269
+ path.join(directory, 'coverage.json'),
270
+ JSON.stringify(result, null, 2) + '\n',
271
+ );
272
+ await writeFile(
273
+ path.join(directory, 'index.html'),
274
+ page(
275
+ 'BEAM line coverage',
276
+ `<p>${summary.covered}/${summary.total} executable lines covered (${percentage(summary)}); ${runs.length} native run(s).</p><table><thead><tr><th>Module</th><th>Lines</th><th>Coverage</th></tr></thead><tbody>${rows}</tbody></table>`,
277
+ ),
278
+ );
156
279
  return result;
157
- } finally {
280
+ }
281
+ finally
282
+ {
158
283
  await rm(temporary, {recursive: true, force: true});
159
284
  }
160
285
  }