tslaws 0.9.0 → 1.0.0

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 (123) hide show
  1. package/README.md +8 -2
  2. package/bin/tslaws.js +8 -2
  3. package/dist/browser.d.ts +1 -1
  4. package/dist/browser.js +36 -7
  5. package/dist/bundled.js +2 -2
  6. package/dist/bundlerPlugins.d.ts +2 -0
  7. package/dist/bundlerPlugins.js +7 -0
  8. package/dist/certificates.d.ts +139 -0
  9. package/dist/certificates.js +433 -0
  10. package/dist/chc.d.ts +85 -0
  11. package/dist/chc.js +298 -0
  12. package/dist/check.d.ts +13 -1
  13. package/dist/check.js +215 -28
  14. package/dist/cli.d.ts +4 -0
  15. package/dist/cli.js +324 -41
  16. package/dist/compile.d.ts +17 -1
  17. package/dist/compile.js +244 -25
  18. package/dist/coverage.d.ts +55 -3
  19. package/dist/coverage.js +231 -12
  20. package/dist/e2eApi.js +3 -1
  21. package/dist/generators.js +12 -2
  22. package/dist/github.d.ts +49 -5
  23. package/dist/github.js +107 -24
  24. package/dist/githubCli.d.ts +28 -0
  25. package/dist/githubCli.js +1039 -211
  26. package/dist/hints.js +2 -1
  27. package/dist/init.js +42 -0
  28. package/dist/isolate.d.ts +28 -0
  29. package/dist/isolate.js +262 -0
  30. package/dist/isolatedCheck.d.ts +1 -0
  31. package/dist/isolatedCheck.js +16 -0
  32. package/dist/lawHost.d.ts +50 -0
  33. package/dist/lawHost.js +199 -0
  34. package/dist/lawRuntime.d.ts +39 -0
  35. package/dist/lawRuntime.js +242 -0
  36. package/dist/lock.d.ts +49 -9
  37. package/dist/lock.js +132 -21
  38. package/dist/mcp.js +28 -7
  39. package/dist/mutate.d.ts +4 -0
  40. package/dist/mutate.js +7 -1
  41. package/dist/plan.d.ts +1 -0
  42. package/dist/plan.js +4 -0
  43. package/dist/pluginHooks.d.ts +16 -0
  44. package/dist/pluginHooks.js +48 -0
  45. package/dist/pluginHost.d.ts +5 -0
  46. package/dist/pluginHost.js +43 -0
  47. package/dist/pluginWorker.d.ts +1 -0
  48. package/dist/pluginWorker.js +40 -0
  49. package/dist/plural.d.ts +2 -0
  50. package/dist/plural.js +4 -0
  51. package/dist/pool.d.ts +80 -7
  52. package/dist/pool.js +222 -11
  53. package/dist/projects.d.ts +70 -0
  54. package/dist/projects.js +457 -0
  55. package/dist/promote.d.ts +56 -2
  56. package/dist/promote.js +429 -13
  57. package/dist/property.js +13 -2
  58. package/dist/propertyWorker.js +11 -1
  59. package/dist/proposals.d.ts +52 -4
  60. package/dist/proposals.js +484 -21
  61. package/dist/reporters.js +5 -1
  62. package/dist/runtimeEnv.d.ts +32 -0
  63. package/dist/runtimeEnv.js +146 -0
  64. package/dist/sandbox.d.ts +98 -0
  65. package/dist/sandbox.js +673 -0
  66. package/dist/sandboxGuard.d.ts +56 -0
  67. package/dist/sandboxGuard.js +249 -0
  68. package/dist/seal.d.ts +9 -0
  69. package/dist/seal.js +426 -0
  70. package/dist/security/cli.d.ts +6 -0
  71. package/dist/security/cli.js +39 -0
  72. package/dist/security/cwe.d.ts +14 -0
  73. package/dist/security/cwe.js +40 -0
  74. package/dist/security/deps/cache.d.ts +28 -0
  75. package/dist/security/deps/cache.js +107 -0
  76. package/dist/security/deps/config.d.ts +16 -0
  77. package/dist/security/deps/config.js +43 -0
  78. package/dist/security/deps/imports.d.ts +4 -0
  79. package/dist/security/deps/imports.js +33 -0
  80. package/dist/security/deps/lockfiles.d.ts +23 -0
  81. package/dist/security/deps/lockfiles.js +153 -0
  82. package/dist/security/deps/osv.d.ts +66 -0
  83. package/dist/security/deps/osv.js +137 -0
  84. package/dist/security/deps/run.d.ts +54 -0
  85. package/dist/security/deps/run.js +135 -0
  86. package/dist/security/deps/severity.d.ts +26 -0
  87. package/dist/security/deps/severity.js +78 -0
  88. package/dist/security/generators.d.ts +9 -0
  89. package/dist/security/generators.js +36 -0
  90. package/dist/security/index.d.ts +27 -0
  91. package/dist/security/index.js +18 -0
  92. package/dist/security/integers.d.ts +31 -0
  93. package/dist/security/integers.js +105 -0
  94. package/dist/security/law.d.ts +54 -0
  95. package/dist/security/law.js +54 -0
  96. package/dist/security/pathTraversal.d.ts +29 -0
  97. package/dist/security/pathTraversal.js +146 -0
  98. package/dist/security/prototypePollution.d.ts +30 -0
  99. package/dist/security/prototypePollution.js +142 -0
  100. package/dist/security/redirect.d.ts +21 -0
  101. package/dist/security/redirect.js +154 -0
  102. package/dist/security/redos.d.ts +21 -0
  103. package/dist/security/redos.js +93 -0
  104. package/dist/security/validation.d.ts +25 -0
  105. package/dist/security/validation.js +89 -0
  106. package/dist/security/xss.d.ts +21 -0
  107. package/dist/security/xss.js +90 -0
  108. package/dist/settings.d.ts +20 -0
  109. package/dist/settings.js +32 -0
  110. package/dist/smt.d.ts +29 -1
  111. package/dist/smt.js +239 -107
  112. package/dist/smtWorker.d.ts +1 -0
  113. package/dist/smtWorker.js +26 -0
  114. package/dist/solvers.d.ts +9 -0
  115. package/dist/solvers.js +11 -5
  116. package/dist/symbolic.d.ts +54 -1
  117. package/dist/symbolic.js +239 -13
  118. package/dist/tsconfigResolve.d.ts +28 -0
  119. package/dist/tsconfigResolve.js +141 -0
  120. package/dist/types.d.ts +28 -0
  121. package/env.d.ts +17 -0
  122. package/hooks/claude-hook.mjs +2 -0
  123. package/package.json +22 -6
package/dist/github.js CHANGED
@@ -5,6 +5,8 @@ export const githubDefaults = {
5
5
  approvers: ['author', 'reviewers'],
6
6
  approvePullRequest: false,
7
7
  requirePassing: true,
8
+ flow: 'shared',
9
+ local: true,
8
10
  };
9
11
  const approverKinds = ['author', 'reviewers', 'write', 'maintain', 'admin'];
10
12
  /** Logins any workflow can act as. */
@@ -21,7 +23,7 @@ export function parseGithubConfig(raw) {
21
23
  if (typeof raw !== 'object' || raw === null || Array.isArray(raw))
22
24
  return fail('must be an object');
23
25
  const o = raw;
24
- const known = ['accept', 'branch', 'bot', 'approvers', 'approvePullRequest', 'requirePassing'];
26
+ const known = ['accept', 'branch', 'bot', 'approvers', 'approvePullRequest', 'requirePassing', 'flow', 'local'];
25
27
  for (const key of Object.keys(o))
26
28
  if (!known.includes(key))
27
29
  fail(`unknown option "${key}"; supported: ${known.join(', ')}`);
@@ -35,7 +37,9 @@ export function parseGithubConfig(raw) {
35
37
  o.approvers.length === 0 ||
36
38
  o.approvers.some((a) => !approverKinds.includes(a))))
37
39
  fail(`"approvers" must be a non-empty array of ${approverKinds.join(', ')}`);
38
- for (const key of ['approvePullRequest', 'requirePassing'])
40
+ if (o.flow !== undefined && o.flow !== 'shared' && o.flow !== 'own')
41
+ fail(`"flow" must be "shared" or "own", got ${JSON.stringify(o.flow)}`);
42
+ for (const key of ['approvePullRequest', 'requirePassing', 'local'])
39
43
  if (o[key] !== undefined && typeof o[key] !== 'boolean')
40
44
  fail(`"${key}" must be true or false`);
41
45
  const config = { ...githubDefaults, ...o };
@@ -45,7 +49,7 @@ export function parseGithubConfig(raw) {
45
49
  }
46
50
  const passingStatuses = new Set(['proved', 'bounded', 'tested']);
47
51
  /** Joins the lock changes with what the check reported for each. */
48
- export function proposalsOf(changes, report) {
52
+ export function proposalsOf(changes, report, project = '') {
49
53
  const results = new Map();
50
54
  for (const result of report?.results ?? [])
51
55
  results.set(`${result.file.replaceAll('\\', '/')}::${result.name}`, result);
@@ -67,11 +71,17 @@ export function proposalsOf(changes, report) {
67
71
  kind: change.kind,
68
72
  reason: change.reason,
69
73
  fingerprint: change.fingerprint,
74
+ project,
70
75
  ...(outcome === undefined ? {} : { outcome }),
71
76
  passes: outcome !== undefined && passingStatuses.has(outcome.status),
72
77
  };
73
78
  });
74
79
  }
80
+ const cleanProject = (dir) => dir
81
+ .replaceAll('\\', '/')
82
+ .replace(/^(\.\/)+/, '')
83
+ .replace(/\/+$/, '')
84
+ .replace(/^\.$/, '');
75
85
  /**
76
86
  * The `/tslaws accept ...` and `/tslaws reject ...` lines of a comment. Names are law names or `file::name` keys,
77
87
  * separated by spaces or commas; a name with spaces is written in backticks or quotes.
@@ -88,30 +98,78 @@ export function parseCommands(body) {
88
98
  const verb = match[1].toLowerCase();
89
99
  const rest = match[2].trim();
90
100
  const names = [];
101
+ const projects = [];
91
102
  const token = /`([^`]+)`|"([^"]+)"|'([^']+)'|([^\s,]+)/g;
92
- for (let m = token.exec(rest); m !== null; m = token.exec(rest))
93
- names.push((m[1] ?? m[2] ?? m[3] ?? m[4]));
103
+ let wantsProject = false;
104
+ let error;
105
+ const noDirectory = '`--project` needs a directory, as in `--project packages/a`; nothing was recorded';
106
+ for (let m = token.exec(rest); m !== null; m = token.exec(rest)) {
107
+ const word = (m[1] ?? m[2] ?? m[3] ?? m[4]);
108
+ if (wantsProject) {
109
+ // `--project --other` is a flag where a directory belongs: refuse it rather than read it as a directory.
110
+ if (word.startsWith('-'))
111
+ error ??= noDirectory;
112
+ else
113
+ projects.push(cleanProject(word));
114
+ wantsProject = false;
115
+ }
116
+ else if (word === '--project')
117
+ wantsProject = true;
118
+ else if (word.startsWith('--project=')) {
119
+ const dir = cleanProject(word.slice('--project='.length));
120
+ if (word === '--project=')
121
+ error ??= noDirectory;
122
+ else
123
+ projects.push(dir);
124
+ }
125
+ else
126
+ names.push(word);
127
+ }
128
+ // A dangling `--project` must not widen the command to every project.
129
+ if (wantsProject)
130
+ error ??= noDirectory;
94
131
  const all = names.length === 0 || names.some((n) => n.toLowerCase() === 'all');
95
- commands.push({ verb, all, names: all ? [] : names });
132
+ commands.push({
133
+ verb,
134
+ all,
135
+ names: all ? [] : names,
136
+ ...(projects.length > 0 ? { projects } : {}),
137
+ ...(error === undefined ? {} : { error }),
138
+ });
96
139
  }
97
140
  return commands;
98
141
  }
99
- /** The proposals a command names: by `file::name` key, or by law name when that name is unambiguous. */
142
+ /** A proposal's key from the repository root: the project's path, then the project-relative `file::name`. */
143
+ export function repoKey(p) {
144
+ return p.project === undefined || p.project === '' ? p.key : `${p.project}/${p.key}`;
145
+ }
146
+ /**
147
+ * The proposals a command names. A name is a key from the repository root (`packages/a/src/x.laws.ts::name`), a
148
+ * project-relative `file::name` key, or a law name; the first form that matches decides, and a name that still matches
149
+ * more than one proposal (in several files or several projects) is ambiguous. `--project` limits the proposals first.
150
+ */
100
151
  export function resolveNames(proposals, command) {
152
+ const scope = command.projects;
153
+ const inScope = scope === undefined ? proposals : proposals.filter((p) => scope.includes(p.project ?? ''));
101
154
  if (command.all)
102
- return { matched: proposals, unknown: [], ambiguous: [] };
155
+ return { matched: inScope, unknown: [], ambiguous: [] };
156
+ // The same law text can be proposed by two projects with one fingerprint, so a proposal is told apart by project too.
103
157
  const matched = new Map();
104
158
  const unknown = [];
105
159
  const ambiguous = [];
106
160
  for (const name of command.names) {
107
- const byKey = proposals.filter((p) => p.key === name);
108
- const byName = byKey.length > 0 ? byKey : proposals.filter((p) => p.name === name);
109
- if (byName.length === 0)
161
+ const forms = [
162
+ inScope.filter((p) => repoKey(p) === name),
163
+ inScope.filter((p) => p.key === name),
164
+ inScope.filter((p) => p.name === name),
165
+ ];
166
+ const found = forms.find((list) => list.length > 0) ?? [];
167
+ if (found.length === 0)
110
168
  unknown.push(name);
111
- else if (byName.length > 1)
169
+ else if (found.length > 1)
112
170
  ambiguous.push(name);
113
171
  else
114
- matched.set(byName[0].fingerprint, byName[0]);
172
+ matched.set(`${found[0].project ?? ''}\0${found[0].fingerprint}`, found[0]);
115
173
  }
116
174
  return { matched: [...matched.values()], unknown, ambiguous };
117
175
  }
@@ -158,6 +216,7 @@ export function readApprovals(comments, bot) {
158
216
  const record = JSON.parse(json);
159
217
  if ((record.verb === 'accept' || record.verb === 'reject') &&
160
218
  typeof record.by === 'string' &&
219
+ (record.project === undefined || typeof record.project === 'string') &&
161
220
  Array.isArray(record.items) &&
162
221
  record.items.every((i) => typeof i?.key === 'string' && typeof i?.fingerprint === 'string'))
163
222
  found.push(record);
@@ -174,18 +233,23 @@ export function readApprovals(comments, bot) {
174
233
  return records;
175
234
  }
176
235
  /**
177
- * The fingerprints accepted by the records, latest word wins: a later reject withdraws an earlier accept of the same
178
- * fingerprint. A fingerprint names one version of a law, so an edit after the accept leaves the new version unaccepted.
236
+ * The fingerprints accepted by the records for one project, latest word wins: a later reject withdraws an earlier
237
+ * accept of the same fingerprint. A fingerprint names one version of a law, so an edit after the accept leaves the new
238
+ * version unaccepted. Only records of `project` count (default the root, which also takes records with no project), so
239
+ * an accept of a law in one project never accepts the same-named or same-fingerprint law of another.
179
240
  */
180
- export function acceptedFingerprints(records) {
241
+ export function acceptedFingerprints(records, project = '') {
181
242
  const accepted = new Map();
182
- for (const record of records)
243
+ for (const record of records) {
244
+ if ((record.project ?? '') !== project)
245
+ continue;
183
246
  for (const item of record.items) {
184
247
  if (record.verb === 'accept')
185
248
  accepted.set(item.fingerprint, record.by);
186
249
  else
187
250
  accepted.delete(item.fingerprint);
188
251
  }
252
+ }
189
253
  return accepted;
190
254
  }
191
255
  /** Which proposals the merge job accepts. */
@@ -253,12 +317,8 @@ function outcomeText(p) {
253
317
  return `${icon} ${p.outcome.status}${extra === '' ? '' : ` — ${extra}`}`;
254
318
  }
255
319
  const what = (p) => p.kind === 'exempt' ? `exemption ${p.reason}` : p.reason === 'level' ? 'level changed' : `law ${p.reason}`;
256
- /** The sticky pull request comment: every proposal, what it did, and whether it will be accepted on merge. */
257
- export function renderSticky(proposals, config, approvals, link, project = '') {
258
- const title = project === '' ? '' : ` for \`${project.replace(/`/g, "'")}\``;
259
- const head = `${stickyMarkerFor(project)}\n### tslaws proposals${title}\n\n`;
260
- if (proposals.length === 0)
261
- return `${head}No law, level or coverage exemption differs from \`tsc-laws.lock\`.\n`;
320
+ /** The table of proposals and the sentence on how they are accepted. */
321
+ function renderTable(proposals, config, approvals, link) {
262
322
  const rows = proposals.map((p) => {
263
323
  const state = !p.passes && config.requirePassing
264
324
  ? '🚫 rejected while it fails'
@@ -272,5 +332,28 @@ export function renderSticky(proposals, config, approvals, link, project = '') {
272
332
  const how = config.accept === 'merge'
273
333
  ? `Proposals that pass are written to \`tsc-laws.lock\` by the tslaws workflow when this pull request merges into \`${config.branch}\`. Failing proposals are never accepted.`
274
334
  : `Reply \`/tslaws accept <law>\` (or \`/tslaws accept all\`) to accept, \`/tslaws reject <law>\` to withdraw. An accept covers the version shown here; a later edit needs a new accept. Accepted proposals are written to \`tsc-laws.lock\` when this pull request merges into \`${config.branch}\`.`;
275
- return `${head}| Law | Change | If accepted | State |\n| --- | --- | --- | --- |\n${rows.join('\n')}\n\n${how}\n`;
335
+ return `| Law | Change | If accepted | State |\n| --- | --- | --- | --- |\n${rows.join('\n')}\n\n${how}\n`;
336
+ }
337
+ /** The sticky pull request comment: every proposal, what it did, and whether it will be accepted on merge. */
338
+ export function renderSticky(proposals, config, approvals, link, project = '') {
339
+ const title = project === '' ? '' : ` for \`${project.replace(/`/g, "'")}\``;
340
+ const head = `${stickyMarkerFor(project)}\n### tslaws proposals${title}\n\n`;
341
+ if (proposals.length === 0)
342
+ return `${head}No law, level or coverage exemption differs from \`tsc-laws.lock\`.\n`;
343
+ return `${head}${renderTable(proposals, config, approvals, link)}`;
344
+ }
345
+ /**
346
+ * One sticky comment for a monorepo root run: a section per project that has proposals, and one line naming the rest.
347
+ * `marker` is the comment's own marker (the root's), so the per-project comments of single-project runs stay separate.
348
+ */
349
+ export function renderStickyAll(sections, marker) {
350
+ const head = `${marker}\n### tslaws proposals\n\n`;
351
+ const proposed = sections.filter((s) => s.proposals.length > 0);
352
+ const quiet = sections
353
+ .filter((s) => s.proposals.length === 0)
354
+ .map((s) => `\`${(s.project || '.').replace(/`/g, "'")}\``);
355
+ if (proposed.length === 0)
356
+ return `${head}No law, level or coverage exemption differs from \`tsc-laws.lock\` in any of ${sections.length} project${sections.length === 1 ? '' : 's'}.\n`;
357
+ const parts = proposed.map((s) => `#### \`${(s.project || '.').replace(/`/g, "'")}\`\n\n${renderTable(s.proposals, s.config, s.approvals, s.link)}`);
358
+ return `${head}${parts.join('\n')}${quiet.length === 0 ? '' : `\nNo change in ${quiet.join(', ')}.\n`}`;
276
359
  }
@@ -16,7 +16,27 @@ export interface GithubContext {
16
16
  err: (text: string) => void;
17
17
  /** For tests: a stand-in for the GitHub REST API. */
18
18
  api?: Api;
19
+ /**
20
+ * Set when the run covers a monorepo root: every selected project, each with its own directory and options. `cwd`
21
+ * and `projectPath` then name the root, and `options` is unused.
22
+ */
23
+ projects?: Unit[];
24
+ /** Every project under the root, standalone ones included (project-relative paths, '' for the root itself). */
25
+ discovered?: string[];
26
+ /** The check options of a discovered project, or undefined when it is not one. */
27
+ optionsFor?: (rel: string) => CheckOptions | undefined;
28
+ }
29
+ /** One project of a run. */
30
+ export interface Unit {
31
+ cwd: string;
32
+ /** Relative to the monorepo root ('' for the root itself). */
33
+ rel: string;
34
+ /** From the repository root, with '/' separators ('' for the repository root). */
35
+ path: string;
36
+ options: CheckOptions;
19
37
  }
38
+ /** Two repo-relative paths joined; either may be ''. */
39
+ export declare function joinRepo(prefix: string, file: string): string;
20
40
  /** `--project-path`, else where `cwd` sits inside its git repository (`git rev-parse --show-prefix`), else ''. */
21
41
  export declare function projectPathOf(cwd: string, flag: string | true | undefined): string;
22
42
  export type Api = (method: string, path: string, body?: unknown) => Promise<unknown>;
@@ -33,10 +53,13 @@ interface PullRequest {
33
53
  ref: string;
34
54
  sha: string;
35
55
  };
56
+ /** How many files the pull request changes, as GitHub counts them (the files list is capped at 3000). */
57
+ changed_files?: number;
36
58
  requested_reviewers?: {
37
59
  login: string;
38
60
  }[];
39
61
  merged?: boolean;
62
+ merged_at?: string | null;
40
63
  }
41
64
  /** The `github` section of a tslaws.json text, with defaults. It must exist: it names the bot every check trusts. */
42
65
  export declare function githubConfigOf(text: string | undefined): GithubConfig;
@@ -47,6 +70,11 @@ interface ProjectState {
47
70
  lock: Lock | undefined;
48
71
  }
49
72
  export declare function projectState(options: CheckOptions): ProjectState;
73
+ /**
74
+ * Whether `login` has `admin` or `maintain` access: the people who may change tsc-laws.lock by hand or run the merge
75
+ * job on demand. A bot never counts, and neither does a login whose permission cannot be read.
76
+ */
77
+ export declare function maintains(api: Api, name: string, login: string, type?: string): Promise<boolean>;
50
78
  /** Why `login` may not accept on this pull request, or undefined when they may. */
51
79
  export declare function refusal(api: Api, name: string, pr: PullRequest, login: string, type: string, config: GithubConfig): Promise<string | undefined>;
52
80
  export declare function githubCommand(action: string | undefined, ctx: GithubContext): Promise<number>;