tslaws 0.10.0 → 1.1.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.
- package/README.md +13 -2
- package/bin/tslaws.js +8 -2
- package/dist/badge.d.ts +64 -0
- package/dist/badge.js +121 -0
- package/dist/browser.d.ts +1 -1
- package/dist/browser.js +25 -5
- package/dist/bundled.js +2 -2
- package/dist/bundlerPlugins.d.ts +2 -0
- package/dist/bundlerPlugins.js +7 -0
- package/dist/certificates.d.ts +139 -0
- package/dist/certificates.js +433 -0
- package/dist/chc.d.ts +97 -0
- package/dist/chc.js +307 -0
- package/dist/check.d.ts +13 -1
- package/dist/check.js +218 -31
- package/dist/cli.d.ts +18 -0
- package/dist/cli.js +242 -34
- package/dist/compile.d.ts +15 -5
- package/dist/compile.js +203 -70
- package/dist/coverage.d.ts +38 -1
- package/dist/coverage.js +123 -19
- package/dist/e2eApi.js +3 -1
- package/dist/generators.js +12 -2
- package/dist/github.d.ts +49 -5
- package/dist/github.js +107 -24
- package/dist/githubCli.d.ts +28 -0
- package/dist/githubCli.js +1113 -212
- package/dist/hints.js +2 -1
- package/dist/init.js +42 -0
- package/dist/isolate.d.ts +33 -0
- package/dist/isolate.js +262 -0
- package/dist/isolatedCheck.d.ts +1 -0
- package/dist/isolatedCheck.js +16 -0
- package/dist/lawHost.d.ts +50 -0
- package/dist/lawHost.js +197 -0
- package/dist/lawRuntime.d.ts +45 -0
- package/dist/lawRuntime.js +321 -0
- package/dist/lock.d.ts +49 -9
- package/dist/lock.js +132 -21
- package/dist/mcp.js +28 -7
- package/dist/mutate.d.ts +4 -0
- package/dist/mutate.js +7 -1
- package/dist/plan.d.ts +1 -0
- package/dist/plan.js +4 -0
- package/dist/pluginHooks.d.ts +20 -0
- package/dist/pluginHooks.js +57 -0
- package/dist/pluginHost.d.ts +5 -0
- package/dist/pluginHost.js +43 -0
- package/dist/pluginWorker.d.ts +1 -0
- package/dist/pluginWorker.js +40 -0
- package/dist/plural.d.ts +2 -0
- package/dist/plural.js +4 -0
- package/dist/pool.d.ts +80 -7
- package/dist/pool.js +222 -11
- package/dist/projects.d.ts +43 -1
- package/dist/projects.js +283 -11
- package/dist/promote.d.ts +56 -2
- package/dist/promote.js +429 -13
- package/dist/property.js +5 -1
- package/dist/propertyWorker.js +9 -4
- package/dist/proposals.d.ts +52 -4
- package/dist/proposals.js +484 -21
- package/dist/reporters.js +5 -1
- package/dist/runtimeEnv.d.ts +13 -5
- package/dist/runtimeEnv.js +48 -10
- package/dist/sandbox.d.ts +98 -0
- package/dist/sandbox.js +673 -0
- package/dist/sandboxGuard.d.ts +56 -0
- package/dist/sandboxGuard.js +249 -0
- package/dist/seal.d.ts +9 -0
- package/dist/seal.js +426 -0
- package/dist/security/cli.d.ts +6 -0
- package/dist/security/cli.js +39 -0
- package/dist/security/cwe.d.ts +14 -0
- package/dist/security/cwe.js +40 -0
- package/dist/security/deps/cache.d.ts +28 -0
- package/dist/security/deps/cache.js +107 -0
- package/dist/security/deps/config.d.ts +16 -0
- package/dist/security/deps/config.js +43 -0
- package/dist/security/deps/imports.d.ts +4 -0
- package/dist/security/deps/imports.js +33 -0
- package/dist/security/deps/lockfiles.d.ts +23 -0
- package/dist/security/deps/lockfiles.js +153 -0
- package/dist/security/deps/osv.d.ts +66 -0
- package/dist/security/deps/osv.js +137 -0
- package/dist/security/deps/run.d.ts +54 -0
- package/dist/security/deps/run.js +135 -0
- package/dist/security/deps/severity.d.ts +26 -0
- package/dist/security/deps/severity.js +78 -0
- package/dist/security/generators.d.ts +9 -0
- package/dist/security/generators.js +36 -0
- package/dist/security/index.d.ts +27 -0
- package/dist/security/index.js +18 -0
- package/dist/security/integers.d.ts +31 -0
- package/dist/security/integers.js +105 -0
- package/dist/security/law.d.ts +54 -0
- package/dist/security/law.js +54 -0
- package/dist/security/pathTraversal.d.ts +29 -0
- package/dist/security/pathTraversal.js +146 -0
- package/dist/security/prototypePollution.d.ts +30 -0
- package/dist/security/prototypePollution.js +142 -0
- package/dist/security/redirect.d.ts +21 -0
- package/dist/security/redirect.js +154 -0
- package/dist/security/redos.d.ts +21 -0
- package/dist/security/redos.js +93 -0
- package/dist/security/validation.d.ts +25 -0
- package/dist/security/validation.js +89 -0
- package/dist/security/xss.d.ts +21 -0
- package/dist/security/xss.js +90 -0
- package/dist/settings.d.ts +20 -0
- package/dist/settings.js +32 -0
- package/dist/smt.d.ts +37 -1
- package/dist/smt.js +277 -111
- package/dist/smtWorker.d.ts +1 -0
- package/dist/smtWorker.js +26 -0
- package/dist/solvers.d.ts +9 -0
- package/dist/solvers.js +11 -5
- package/dist/symbolic.d.ts +54 -1
- package/dist/symbolic.js +239 -13
- package/dist/tsconfigResolve.d.ts +28 -0
- package/dist/tsconfigResolve.js +141 -0
- package/dist/types.d.ts +33 -0
- package/hooks/claude-hook.mjs +2 -0
- package/package.json +19 -7
package/dist/compile.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
2
|
import { createHash } from 'node:crypto';
|
|
3
3
|
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
4
|
-
import { dirname, join, resolve } from 'node:path';
|
|
5
|
-
import { fileURLToPath } from 'node:url';
|
|
4
|
+
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
|
|
5
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
6
|
import { transformSync } from 'esbuild';
|
|
7
7
|
import ts from 'typescript';
|
|
8
8
|
import { lowerLawFile, usesLegacySyntax } from "./lower.js";
|
|
9
9
|
import { readConfig } from "./coverage.js";
|
|
10
|
+
import { installLawRuntime, runtimeFile } from "./lawRuntime.js";
|
|
11
|
+
import { commonRoot, readResolution, resolutionCompilerOptions } from "./tsconfigResolve.js";
|
|
10
12
|
import { importMetaEnvGlobal } from "./runtimeEnv.js";
|
|
13
|
+
import { sealLaws } from "./seal.js";
|
|
11
14
|
/**
|
|
12
15
|
* Runs the compiler, trying again when the system could not start it. On Windows a freshly built executable can be
|
|
13
16
|
* briefly unavailable (a scanner holds it, the machine is out of handles), which surfaces as ENOENT or UNKNOWN.
|
|
@@ -22,6 +25,9 @@ function launch(compiler, args, options) {
|
|
|
22
25
|
}
|
|
23
26
|
/** The compiler finds its lib files in TSC_TS_LIB_DIR; without it, use the TypeScript 7 libs scriptc ships. */
|
|
24
27
|
function compilerEnv(cwd, compiler) {
|
|
28
|
+
// A caller outside the CLI (the API, a test) can reach here with no compiler resolved; say so, not a TypeError from dirname.
|
|
29
|
+
if (typeof compiler !== 'string')
|
|
30
|
+
throw new Error(noCompiler('none'));
|
|
25
31
|
if (process.env.TSC_TS_LIB_DIR !== undefined)
|
|
26
32
|
return process.env;
|
|
27
33
|
// The npm compiler package keeps the lib files beside the executable.
|
|
@@ -41,11 +47,12 @@ function compilerEnv(cwd, compiler) {
|
|
|
41
47
|
return process.env;
|
|
42
48
|
}
|
|
43
49
|
}
|
|
50
|
+
const noCompiler = (compiler) => `no tsc-laws compiler found (${compiler}): install the tslaws-compiler package for this platform, or set TSC_LAWS_BIN / --compiler to the compiler executable`;
|
|
44
51
|
/** Lists the laws of `files` with the problems that stop them running. */
|
|
45
52
|
/** Says what to do when the compiler cannot start, in particular when none was found at all. */
|
|
46
53
|
function cannotRun(compiler, error) {
|
|
47
54
|
if (error.code === 'ENOENT')
|
|
48
|
-
return
|
|
55
|
+
return noCompiler(compiler);
|
|
49
56
|
return `cannot run ${compiler}: ${error.message}`;
|
|
50
57
|
}
|
|
51
58
|
/** The compiler name that selects the built-in path: stock TypeScript, with laws written as `law(...)` calls. */
|
|
@@ -101,9 +108,7 @@ function assertOneSyntax(files, cwd) {
|
|
|
101
108
|
}
|
|
102
109
|
export function readManifest(compiler, files, cwd) {
|
|
103
110
|
if (usesBuiltin(compiler, files, cwd))
|
|
104
|
-
return files
|
|
105
|
-
.filter((f) => isLawFile(f, cwd))
|
|
106
|
-
.flatMap((f) => (existsSync(join(cwd, f)) ? lowerFile(f, cwd).laws : []));
|
|
111
|
+
return sealLaws(cwd, files.filter((f) => isLawFile(f, cwd)).flatMap((f) => (existsSync(join(cwd, f)) ? lowerFile(f, cwd).laws : [])));
|
|
107
112
|
assertOneSyntax(files, cwd);
|
|
108
113
|
const run = launch(compiler, ['--laws-manifest', ...files], {
|
|
109
114
|
cwd,
|
|
@@ -116,7 +121,7 @@ export function readManifest(compiler, files, cwd) {
|
|
|
116
121
|
const text = (run.stdout ?? '').trim();
|
|
117
122
|
if (text === '')
|
|
118
123
|
throw new Error(`${compiler} --laws-manifest printed nothing: ${run.stderr ?? ''}`);
|
|
119
|
-
return JSON.parse(text).laws;
|
|
124
|
+
return sealLaws(cwd, JSON.parse(text).laws);
|
|
120
125
|
}
|
|
121
126
|
const stringOptions = ['jsx', 'jsxFactory', 'jsxFragmentFactory', 'jsxImportSource'];
|
|
122
127
|
const booleanOptions = [
|
|
@@ -164,28 +169,81 @@ export function compilerFlags(options) {
|
|
|
164
169
|
}
|
|
165
170
|
return flags;
|
|
166
171
|
}
|
|
172
|
+
/** Turns emitted ES modules (with `import.meta` already rewritten) into CommonJS. */
|
|
173
|
+
function lowerModule(text, sourcefile) {
|
|
174
|
+
return transformSync(text, { format: 'cjs', target: 'es2022', sourcefile, logLevel: 'silent' }).code;
|
|
175
|
+
}
|
|
176
|
+
/** Where a compiled file came from: the source it mirrors under `root`, which is what `import.meta` should name. */
|
|
177
|
+
function sourceOfOutput(file, outDir, root) {
|
|
178
|
+
const mirrored = join(root, relative(outDir, file));
|
|
179
|
+
const endings = file.endsWith('.mjs')
|
|
180
|
+
? ['.mts', '.mjs']
|
|
181
|
+
: file.endsWith('.cjs')
|
|
182
|
+
? ['.cts', '.cjs']
|
|
183
|
+
: ['.ts', '.tsx', '.js', '.jsx'];
|
|
184
|
+
const stem = mirrored.replace(/\.[cm]?js$/, '');
|
|
185
|
+
return endings.map((ending) => `${stem}${ending}`).find((candidate) => existsSync(candidate)) ?? mirrored;
|
|
186
|
+
}
|
|
187
|
+
/** The first top-level `await` of a module (an `await` expression or `for await` outside every function), if any. */
|
|
188
|
+
function topLevelAwait(source) {
|
|
189
|
+
let found;
|
|
190
|
+
const visit = (node) => {
|
|
191
|
+
if (found !== undefined || ts.isFunctionLike(node) || ts.isClassLike(node))
|
|
192
|
+
return;
|
|
193
|
+
if (ts.isAwaitExpression(node) || (ts.isForOfStatement(node) && node.awaitModifier !== undefined))
|
|
194
|
+
found = node;
|
|
195
|
+
else
|
|
196
|
+
ts.forEachChild(node, visit);
|
|
197
|
+
};
|
|
198
|
+
visit(source);
|
|
199
|
+
return found;
|
|
200
|
+
}
|
|
167
201
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
202
|
+
* Laws and the modules they load run as CommonJS (see `build`), where a module cannot wait at its top level. Said once,
|
|
203
|
+
* in terms of the way out, instead of as the loader's syntax error.
|
|
170
204
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
205
|
+
const awaitAdvice = 'top-level await is not supported in a law or in a module a law imports, because laws load as CommonJS; move the ' +
|
|
206
|
+
'await into an async function (or into the law body, which may be async)';
|
|
207
|
+
const awaitProblem = (file) => `${file}: error: ${awaitAdvice}`;
|
|
208
|
+
/** What replaces such a module in the output, so the law that loads it fails with the reason and not a syntax error. */
|
|
209
|
+
const awaitStub = (file) => `throw new Error(${JSON.stringify(`${file}: ${awaitAdvice}`)});\n`;
|
|
210
|
+
/**
|
|
211
|
+
* `import.meta` for a module that is not run as written: `env` is the global the runner sets before any law loads (see
|
|
212
|
+
* runtimeEnv.ts), `url`, `filename` and `dirname` name the source file (not the copy in `.tslaws/out`), and any other
|
|
213
|
+
* use of `import.meta` is an object holding those four. Found as syntax so that a string or comment that says the same
|
|
214
|
+
* words is left alone. Returns the new text and whether the module waits at its top level.
|
|
215
|
+
*/
|
|
216
|
+
function rewriteImportMeta(text, file, source) {
|
|
217
|
+
const parsed = ts.createSourceFile(file, text, ts.ScriptTarget.ES2022, false, ts.ScriptKind.JS);
|
|
218
|
+
const edits = [];
|
|
219
|
+
const env = `globalThis.${importMetaEnvGlobal}`;
|
|
220
|
+
const known = {
|
|
221
|
+
url: JSON.stringify(pathToFileURL(source).href),
|
|
222
|
+
filename: JSON.stringify(source),
|
|
223
|
+
dirname: JSON.stringify(dirname(source)),
|
|
224
|
+
env,
|
|
225
|
+
};
|
|
226
|
+
const isMeta = (node) => ts.isMetaProperty(node) && node.keywordToken === ts.SyntaxKind.ImportKeyword && node.name.text === 'meta';
|
|
227
|
+
const visit = (node) => {
|
|
228
|
+
if (ts.isPropertyAccessExpression(node) && isMeta(node.expression) && node.name.text in known)
|
|
229
|
+
edits.push({ start: node.getStart(parsed), end: node.end, text: known[node.name.text] });
|
|
230
|
+
else if (isMeta(node)) {
|
|
231
|
+
const members = Object.entries(known).map(([key, value]) => `${key}: ${value}`);
|
|
232
|
+
edits.push({ start: node.getStart(parsed), end: node.end, text: `({ ${members.join(', ')} })` });
|
|
233
|
+
}
|
|
234
|
+
else
|
|
235
|
+
ts.forEachChild(node, visit);
|
|
236
|
+
};
|
|
237
|
+
visit(parsed);
|
|
238
|
+
const rewritten = edits.reduceRight((out, e) => out.slice(0, e.start) + e.text + out.slice(e.end), text);
|
|
239
|
+
return { text: rewritten, awaits: topLevelAwait(parsed) !== undefined };
|
|
179
240
|
}
|
|
180
241
|
/**
|
|
181
|
-
* CommonJS for ES module text
|
|
182
|
-
*
|
|
242
|
+
* CommonJS for ES module text, with `import.meta` rewritten as above. TypeScript does the lowering, not esbuild, so the
|
|
243
|
+
* comments stay: the coverage analysis reads `@tslaws-exempt` from this output.
|
|
183
244
|
*/
|
|
184
|
-
function
|
|
185
|
-
|
|
186
|
-
.replace(/\bimport\.meta\.env\b/g, `globalThis.${importMetaEnvGlobal}`)
|
|
187
|
-
.replace(/\bimport\.meta\b/g, '({})');
|
|
188
|
-
return ts.transpileModule(source, {
|
|
245
|
+
function lowerToCommonJs(text, file) {
|
|
246
|
+
return ts.transpileModule(text, {
|
|
189
247
|
fileName: file,
|
|
190
248
|
compilerOptions: {
|
|
191
249
|
module: ts.ModuleKind.CommonJS,
|
|
@@ -195,40 +253,80 @@ function lowerEmitted(text, file) {
|
|
|
195
253
|
},
|
|
196
254
|
}).outputText;
|
|
197
255
|
}
|
|
198
|
-
/**
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
256
|
+
/**
|
|
257
|
+
* Lowers what the native compiler emitted into `dir` as ES modules, which is how `import.meta` is allowed: `.js` becomes
|
|
258
|
+
* CommonJS and the `import.meta` of a `.mjs` (from a `.mts`, kept as an ES module Node can load) is rewritten in place.
|
|
259
|
+
* `outDir` and `root` say where `dir` sits and what it mirrors, so `import.meta` can name the source. Returns one line
|
|
260
|
+
* per module that cannot be lowered.
|
|
261
|
+
*/
|
|
262
|
+
export function lowerEmittedModules(dir, where = {}) {
|
|
263
|
+
const problems = [];
|
|
264
|
+
const outDir = where.outDir ?? dir;
|
|
265
|
+
const root = where.root ?? dir;
|
|
266
|
+
const lowerIn = (current) => {
|
|
267
|
+
if (!existsSync(current))
|
|
268
|
+
return;
|
|
269
|
+
for (const entry of readdirSync(current, { withFileTypes: true })) {
|
|
270
|
+
const path = join(current, entry.name);
|
|
271
|
+
if (entry.isDirectory())
|
|
272
|
+
lowerIn(path);
|
|
273
|
+
else if (/\.m?js$/.test(entry.name)) {
|
|
274
|
+
try {
|
|
275
|
+
const { text, awaits } = rewriteImportMeta(readFileSync(path, 'utf8'), path, sourceOfOutput(path, outDir, root));
|
|
276
|
+
if (awaits) {
|
|
277
|
+
problems.push(awaitProblem(slash(relative(outDir, path))));
|
|
278
|
+
writeFileSync(path, awaitStub(slash(relative(outDir, path))));
|
|
279
|
+
}
|
|
280
|
+
else
|
|
281
|
+
writeFileSync(path, entry.name.endsWith('.mjs') ? text : lowerToCommonJs(text, path));
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
// Output that does not parse (JSX kept because the project sets no `jsx`): the compiler's diagnostics say why.
|
|
285
|
+
}
|
|
212
286
|
}
|
|
213
287
|
}
|
|
214
|
-
}
|
|
288
|
+
};
|
|
289
|
+
lowerIn(dir);
|
|
290
|
+
return problems;
|
|
215
291
|
}
|
|
216
292
|
/** The ambient `ImportMeta.env` declaration that ships with tslaws; also importable as `tslaws/env`. */
|
|
217
293
|
const envTypes = fileURLToPath(new URL('../env.d.ts', import.meta.url));
|
|
218
294
|
/** Whether `text` (compiler output) says `import.meta.env` has no declaration. */
|
|
219
295
|
const lacksEnvTypes = (text) => /TS2339.*'ImportMeta'/.test(text);
|
|
296
|
+
/** Whether `text` (compiler output) says a module waits at its top level while the output is CommonJS (TS1378). */
|
|
297
|
+
const hasTopLevelAwaitError = (text) => /TS1378/.test(text);
|
|
220
298
|
/** Whether `text` (compiler output) says the project uses `import.meta`, which CommonJS output does not allow. */
|
|
221
299
|
const usesImportMeta = (text) => /TS1343/.test(text);
|
|
222
300
|
/**
|
|
223
|
-
* Compiles `files` to CommonJS plus declarations in `.tslaws/out` under `cwd`, so `node_modules` still resolves.
|
|
224
|
-
*
|
|
225
|
-
*
|
|
301
|
+
* Compiles `files` to CommonJS plus declarations in `.tslaws/out` under `cwd`, so `node_modules` still resolves.
|
|
302
|
+
* Module resolution follows the project's tsconfig (see tsconfigResolve.ts), for the compiler and, through the runtime
|
|
303
|
+
* hooks, for Node when it loads the output. The built-in compiler always reads the project as ES modules and lowers the
|
|
304
|
+
* output. The native compiler does so only for a project that uses `import.meta` (it reports TS1343 as CommonJS), so
|
|
305
|
+
* every other project builds exactly as before.
|
|
226
306
|
*/
|
|
227
307
|
export function build(compiler, files, cwd) {
|
|
308
|
+
const resolution = readResolution(cwd);
|
|
309
|
+
const result = compile(compiler, files, cwd, resolution);
|
|
310
|
+
const bundlerPlugins = (readConfig(cwd).bundlerPlugins ?? []).map((path) => resolve(cwd, path));
|
|
311
|
+
const runtime = { cwd, options: resolution?.options, outDir: result.outDir, bundlerPlugins };
|
|
312
|
+
installLawRuntime(runtime);
|
|
313
|
+
try {
|
|
314
|
+
writeFileSync(join(result.outDir, runtimeFile), JSON.stringify(runtime));
|
|
315
|
+
}
|
|
316
|
+
catch {
|
|
317
|
+
// A worker then loads the output without the aliases; the error it reports names the import.
|
|
318
|
+
}
|
|
319
|
+
return result;
|
|
320
|
+
}
|
|
321
|
+
function compile(compiler, files, cwd, resolution) {
|
|
228
322
|
const outDir = join(cwd, '.tslaws', 'out');
|
|
323
|
+
const builtin = usesBuiltin(compiler, files, cwd);
|
|
324
|
+
// The compiler wants every source file under one root, and `paths` can lead out of the project (a sibling package),
|
|
325
|
+
// so the root is the common ancestor and the output mirrors the tree from there. The built-in path writes only what
|
|
326
|
+
// is inside the project, so it keeps the project as the root.
|
|
327
|
+
const root = builtin ? cwd : commonRoot(cwd, resolution?.options);
|
|
229
328
|
const extra = compilerFlags(readConfig(cwd).compilerOptions);
|
|
230
|
-
const
|
|
231
|
-
'--ignoreConfig',
|
|
329
|
+
const flags = [
|
|
232
330
|
'--target',
|
|
233
331
|
'es2022',
|
|
234
332
|
'--module',
|
|
@@ -240,14 +338,15 @@ export function build(compiler, files, cwd) {
|
|
|
240
338
|
'--esModuleInterop',
|
|
241
339
|
'--declaration',
|
|
242
340
|
'--rootDir',
|
|
243
|
-
|
|
341
|
+
root,
|
|
244
342
|
'--outDir',
|
|
245
343
|
outDir,
|
|
246
344
|
...extra,
|
|
247
|
-
...files,
|
|
248
345
|
];
|
|
249
|
-
const
|
|
250
|
-
|
|
346
|
+
const args = ['--ignoreConfig', ...flags, ...files];
|
|
347
|
+
// What the options say is part of what was built.
|
|
348
|
+
const keyArgs = [...args, JSON.stringify(resolution?.options ?? null)];
|
|
349
|
+
const reused = builtin ? cachedBuild(keyArgs, files, cwd, outDir) : undefined;
|
|
251
350
|
if (reused !== undefined)
|
|
252
351
|
return reused;
|
|
253
352
|
rmSync(outDir, { recursive: true, force: true });
|
|
@@ -255,41 +354,59 @@ export function build(compiler, files, cwd) {
|
|
|
255
354
|
// The output is CommonJS whatever the project's own package.json says.
|
|
256
355
|
writeFileSync(join(outDir, 'package.json'), JSON.stringify({ type: 'commonjs' }));
|
|
257
356
|
if (builtin)
|
|
258
|
-
return buildBuiltin(args, files, cwd, outDir);
|
|
259
|
-
|
|
357
|
+
return buildBuiltin(args, keyArgs, files, cwd, outDir, resolution);
|
|
358
|
+
// `paths` is not a command line option, so the compiler gets the resolution options in a generated tsconfig that names
|
|
359
|
+
// the files; everything else stays a flag, and flags win over the file. Without any, the flags alone do as before.
|
|
360
|
+
const project = join(cwd, '.tslaws', 'resolution.tsconfig.json');
|
|
361
|
+
const argsFor = (flagList, more = []) => {
|
|
362
|
+
if (resolution === undefined)
|
|
363
|
+
return ['--ignoreConfig', ...flagList, ...files, ...more];
|
|
364
|
+
writeFileSync(project, JSON.stringify({
|
|
365
|
+
compilerOptions: { ...resolution.options, rootDir: root },
|
|
366
|
+
files: [...files, ...more].map((f) => resolve(cwd, f)),
|
|
367
|
+
}));
|
|
368
|
+
return ['-p', project, ...flagList];
|
|
369
|
+
};
|
|
370
|
+
const given = argsFor(flags);
|
|
371
|
+
let run = launch(compiler, given, { cwd, encoding: 'utf8', maxBuffer: 64 << 20, env: compilerEnv(cwd, compiler) });
|
|
260
372
|
// A compiler that dies without a word (out of memory on a busy machine) is run again; a type error always says so.
|
|
261
373
|
for (let attempt = 0; attempt < 2 &&
|
|
262
374
|
run.error === undefined &&
|
|
263
375
|
run.status !== 0 &&
|
|
264
376
|
`${run.stdout ?? ''}${run.stderr ?? ''}`.trim() === ''; attempt += 1)
|
|
265
|
-
run = launch(compiler,
|
|
377
|
+
run = launch(compiler, given, { cwd, encoding: 'utf8', maxBuffer: 64 << 20, env: compilerEnv(cwd, compiler) });
|
|
266
378
|
if (run.error !== undefined)
|
|
267
379
|
throw new Error(cannotRun(compiler, run.error));
|
|
268
380
|
const output = () => `${run.stdout ?? ''}${run.stderr ?? ''}`;
|
|
269
|
-
const again = (
|
|
270
|
-
const
|
|
381
|
+
const again = (next) => {
|
|
382
|
+
const rerun = launch(compiler, next, {
|
|
271
383
|
cwd,
|
|
272
384
|
encoding: 'utf8',
|
|
273
385
|
maxBuffer: 64 << 20,
|
|
274
386
|
env: compilerEnv(cwd, compiler),
|
|
275
387
|
});
|
|
276
|
-
if (
|
|
277
|
-
run =
|
|
388
|
+
if (rerun.error === undefined)
|
|
389
|
+
run = rerun;
|
|
278
390
|
};
|
|
279
|
-
// CommonJS output does not allow `import.meta
|
|
391
|
+
// CommonJS output does not allow `import.meta` or a top-level await: read the project as ES modules instead, and lower
|
|
392
|
+
// the output below, where a module that waits at its top level is replaced by a stub that says so.
|
|
280
393
|
let lowering = false;
|
|
281
|
-
let used =
|
|
282
|
-
if (usesImportMeta(output())) {
|
|
283
|
-
used =
|
|
394
|
+
let used = flags;
|
|
395
|
+
if (usesImportMeta(output()) || hasTopLevelAwaitError(output())) {
|
|
396
|
+
used = flags.map((flag, i) => (flags[i - 1] === '--module' ? 'preserve' : flag));
|
|
284
397
|
lowering = true;
|
|
285
|
-
again(used);
|
|
398
|
+
again(argsFor(used));
|
|
286
399
|
}
|
|
287
400
|
// A project without an `ImportMeta.env` declaration gets the one that ships with tslaws, and the build runs again.
|
|
288
401
|
if (lacksEnvTypes(output()))
|
|
289
|
-
again(
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
return {
|
|
402
|
+
again(argsFor(used, [envTypes]));
|
|
403
|
+
const problems = lowering ? lowerEmittedModules(outDir, { outDir, root }) : [];
|
|
404
|
+
// The project's own files sit where they do under the root, so its output is that deep in `outDir`.
|
|
405
|
+
return {
|
|
406
|
+
outDir: join(outDir, relative(root, cwd)),
|
|
407
|
+
diagnostics: [output().trim(), ...problems].filter((part) => part !== '').join('\n'),
|
|
408
|
+
ok: run.status === 0 && problems.length === 0,
|
|
409
|
+
};
|
|
293
410
|
}
|
|
294
411
|
/** Every `node_modules/@types` from `cwd` upwards: where the compiler looks for global declarations by default. */
|
|
295
412
|
function typeRootsOf(cwd) {
|
|
@@ -303,11 +420,13 @@ function typeRootsOf(cwd) {
|
|
|
303
420
|
}
|
|
304
421
|
}
|
|
305
422
|
/** Compiles in-process with stock TypeScript: law files are lowered in memory, nothing is written next to them. */
|
|
306
|
-
function buildBuiltin(args, files, cwd, outDir) {
|
|
423
|
+
function buildBuiltin(args, keyArgs, files, cwd, outDir, resolution) {
|
|
307
424
|
const at = args.indexOf('--moduleResolution');
|
|
308
425
|
const parsed = ts.parseCommandLine(at < 0 ? args : [...args.slice(0, at), ...args.slice(at + 2)]);
|
|
309
426
|
// Types resolve the bundler way, so package `exports` (tslaws/browser) work; the ES output is made CommonJS below.
|
|
310
427
|
const options = {
|
|
428
|
+
// The tsconfig's resolution first, so the project's own compilerOptions win over it.
|
|
429
|
+
...resolutionCompilerOptions(resolution?.options),
|
|
311
430
|
...parsed.options,
|
|
312
431
|
module: ts.ModuleKind.ESNext,
|
|
313
432
|
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
@@ -352,11 +471,25 @@ function buildBuiltin(args, files, cwd, outDir) {
|
|
|
352
471
|
program = ts.createProgram([...roots, envTypes], used, host);
|
|
353
472
|
diagnostics = ts.getPreEmitDiagnostics(program);
|
|
354
473
|
}
|
|
474
|
+
const problems = [];
|
|
355
475
|
program.emit(undefined, (name, text) => {
|
|
476
|
+
// A source file outside the project (reached through `paths`) is checked and loaded in place, never written here.
|
|
477
|
+
const under = relative(outDir, name);
|
|
478
|
+
if (under.startsWith('..') || isAbsolute(under))
|
|
479
|
+
return;
|
|
356
480
|
mkdirSync(dirname(name), { recursive: true });
|
|
357
|
-
|
|
358
|
-
|
|
481
|
+
if (!/\.m?js$/.test(name))
|
|
482
|
+
return void writeFileSync(name, text);
|
|
483
|
+
const rewritten = rewriteImportMeta(text, name, sourceOfOutput(name, outDir, cwd));
|
|
484
|
+
if (rewritten.awaits) {
|
|
485
|
+
problems.push(awaitProblem(slash(under)));
|
|
486
|
+
writeFileSync(name, awaitStub(slash(under)));
|
|
487
|
+
}
|
|
488
|
+
else
|
|
489
|
+
writeFileSync(name, name.endsWith('.mjs') ? rewritten.text : lowerModule(rewritten.text, name));
|
|
359
490
|
});
|
|
491
|
+
// Such a file is outside the root directory by design (TS6059).
|
|
492
|
+
diagnostics = diagnostics.filter((d) => d.code !== 6059);
|
|
360
493
|
const text = ts.formatDiagnostics(diagnostics, {
|
|
361
494
|
getCanonicalFileName: (f) => f,
|
|
362
495
|
getCurrentDirectory: () => cwd,
|
|
@@ -364,11 +497,11 @@ function buildBuiltin(args, files, cwd, outDir) {
|
|
|
364
497
|
});
|
|
365
498
|
const result = {
|
|
366
499
|
outDir,
|
|
367
|
-
diagnostics: text.trim(),
|
|
368
|
-
ok: diagnostics.every((d) => d.category !== ts.DiagnosticCategory.Error),
|
|
500
|
+
diagnostics: [text.trim(), ...problems].filter((part) => part !== '').join('\n'),
|
|
501
|
+
ok: problems.length === 0 && diagnostics.every((d) => d.category !== ts.DiagnosticCategory.Error),
|
|
369
502
|
};
|
|
370
503
|
Object.defineProperty(result, 'program', { value: program, enumerable: false });
|
|
371
|
-
saveBuildCache(
|
|
504
|
+
saveBuildCache(keyArgs, files, cwd, result, program
|
|
372
505
|
.getSourceFiles()
|
|
373
506
|
.map((f) => f.fileName)
|
|
374
507
|
.filter((f) => !overrides.has(slash(resolve(f)))));
|
package/dist/coverage.d.ts
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
2
|
import { type E2eConfig } from './e2e.ts';
|
|
3
|
+
import { type CertifyMode } from './certificates.ts';
|
|
3
4
|
import { type ImportMetaEnv } from './runtimeEnv.ts';
|
|
4
5
|
import { type SolverName } from './solvers.ts';
|
|
5
6
|
export interface CoverageConfig {
|
|
6
7
|
cover?: string[];
|
|
7
8
|
/** Compiler options the runner passes to tsc; see `compilerFlags` in compile.ts for the supported keys. */
|
|
8
9
|
compilerOptions?: Record<string, unknown>;
|
|
10
|
+
/**
|
|
11
|
+
* Where laws take their module resolution (`paths`, `baseUrl`, project references) from: a tsconfig path relative to
|
|
12
|
+
* tslaws.json, or `false` for none. Default: the nearest tsconfig.json above the project. See tsconfigResolve.ts.
|
|
13
|
+
*/
|
|
14
|
+
tsconfig?: string | false;
|
|
9
15
|
/** End-to-end laws: the app to drive; see `parseE2eConfig`. */
|
|
10
16
|
e2e?: E2eConfig;
|
|
11
17
|
/**
|
|
@@ -23,8 +29,19 @@ export interface CoverageConfig {
|
|
|
23
29
|
* must also answer `unsat` or the law is `bounded`. Z3 is required. See solvers.ts.
|
|
24
30
|
*/
|
|
25
31
|
solvers?: SolverName[];
|
|
32
|
+
/**
|
|
33
|
+
* Check every proof Z3 finds with cvc5 (an Alethe proof) and Carcara, and record the law as `certified` or
|
|
34
|
+
* `uncertified`. `"require"` makes anything short of `certified` an error. Default off. See certificates.ts.
|
|
35
|
+
*/
|
|
36
|
+
certify?: CertifyMode;
|
|
26
37
|
/** esbuild plugin modules (paths relative to tslaws.json) that teach the browser bundler new source formats. */
|
|
27
38
|
bundlerPlugins?: string[];
|
|
39
|
+
/**
|
|
40
|
+
* Directories outside the project (paths relative to tslaws.json) whose reviewed files the law sandbox may read, besides
|
|
41
|
+
* the workspace packages and the `paths` and `references` targets of the tsconfig. Dotfiles and the home directory are
|
|
42
|
+
* refused whatever is named. See sandbox.ts.
|
|
43
|
+
*/
|
|
44
|
+
sandboxReads?: string[];
|
|
28
45
|
/**
|
|
29
46
|
* `import.meta.env` for code laws reach, on top of `process.env` and the defaults (`MODE` `test`, `DEV`, `PROD`, `SSR`,
|
|
30
47
|
* `BASE_URL`); its entries override both. See runtimeEnv.ts.
|
|
@@ -35,8 +52,12 @@ export interface CoverageConfig {
|
|
|
35
52
|
propertyTimeoutMs?: number;
|
|
36
53
|
proofTimeoutMs?: number;
|
|
37
54
|
bound?: number;
|
|
38
|
-
/**
|
|
55
|
+
/** The most laws, proofs and solvers that run at once (the CPU budget); `--jobs` and `TSLAWS_JOBS` beat it. Not sealed: it is the machine's, not the project's. */
|
|
56
|
+
jobs?: number;
|
|
57
|
+
/** Accepting laws on GitHub: see github.ts. When present, `tslaws accept` refuses to run unless `github.local` is true (the default). */
|
|
39
58
|
github?: unknown;
|
|
59
|
+
/** `tslaws security deps`: the severity that fails the check, offline mode, the cache; see security/deps/config.ts. */
|
|
60
|
+
security?: unknown;
|
|
40
61
|
}
|
|
41
62
|
export interface Uncovered {
|
|
42
63
|
file: string;
|
|
@@ -57,6 +78,22 @@ export declare function projectsProblem(value: unknown): string | undefined;
|
|
|
57
78
|
* already acted on `extends`, and discovery reads `projects` from the file itself.
|
|
58
79
|
*/
|
|
59
80
|
export declare function parseConfig(raw: unknown, source?: string): CoverageConfig;
|
|
81
|
+
/**
|
|
82
|
+
* The text of an `extends` value if it is one this tool will follow: a non-empty relative path with forward slashes only.
|
|
83
|
+
* An absolute path, a drive or UNC path, a backslash, or a NUL is refused, so the on-disk loader and the GitHub checks
|
|
84
|
+
* that read the same files through the API resolve one target and never two.
|
|
85
|
+
*/
|
|
86
|
+
export declare function checkExtendsValue(value: unknown): string;
|
|
87
|
+
/**
|
|
88
|
+
* Where `extends` points, as a repository path: `fromFile` is the repository path of the file that names it. Refuses
|
|
89
|
+
* what `checkExtendsValue` does, and a target that leaves the repository.
|
|
90
|
+
*/
|
|
91
|
+
export declare function extendsTarget(fromFile: string, value: unknown): string;
|
|
92
|
+
/**
|
|
93
|
+
* The directory no `extends` may leave: the nearest enclosing directory with a `.git`, else the highest enclosing one
|
|
94
|
+
* that carries a workspace file (`tslaws.json`, `package.json` or `pnpm-workspace.yaml`), else `cwd` itself.
|
|
95
|
+
*/
|
|
96
|
+
export declare function extendsRoot(cwd: string): string;
|
|
60
97
|
/** The settings of tslaws.json in `cwd`, with every `extends` followed; `{}` when there is no file. */
|
|
61
98
|
export declare function readConfig(cwd: string): CoverageConfig;
|
|
62
99
|
/** The `import.meta.env` of the project's laws: `process.env`, the defaults and the `env` of tslaws.json. */
|