tslaws 0.0.0-stage → 0.3.2
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/LICENSE +202 -0
- package/README.md +215 -2
- package/bin/tslaws.js +5 -0
- package/dist/adapters/angular.d.ts +42 -0
- package/dist/adapters/angular.js +40 -0
- package/dist/adapters/dom.d.ts +5 -0
- package/dist/adapters/dom.js +17 -0
- package/dist/adapters/element.d.ts +10 -0
- package/dist/adapters/element.js +28 -0
- package/dist/adapters/react.d.ts +24 -0
- package/dist/adapters/react.js +15 -0
- package/dist/api.d.ts +59 -0
- package/dist/api.js +103 -0
- package/dist/browser.d.ts +24 -0
- package/dist/browser.js +252 -0
- package/dist/browserApi.d.ts +62 -0
- package/dist/browserApi.js +139 -0
- package/dist/bundled.d.ts +17 -0
- package/dist/bundled.js +67 -0
- package/dist/check.d.ts +12 -0
- package/dist/check.js +269 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +360 -0
- package/dist/compile.d.ts +15 -0
- package/dist/compile.js +142 -0
- package/dist/component.d.ts +37 -0
- package/dist/component.js +99 -0
- package/dist/coverage.d.ts +40 -0
- package/dist/coverage.js +237 -0
- package/dist/e2e.d.ts +46 -0
- package/dist/e2e.js +322 -0
- package/dist/e2eApi.d.ts +80 -0
- package/dist/e2eApi.js +140 -0
- package/dist/generators.d.ts +18 -0
- package/dist/generators.js +184 -0
- package/dist/hints.d.ts +3 -0
- package/dist/hints.js +37 -0
- package/dist/init.d.ts +23 -0
- package/dist/init.js +139 -0
- package/dist/lock.d.ts +16 -0
- package/dist/lock.js +40 -0
- package/dist/mcp.d.ts +30 -0
- package/dist/mcp.js +301 -0
- package/dist/mutate.d.ts +54 -0
- package/dist/mutate.js +293 -0
- package/dist/promote.d.ts +38 -0
- package/dist/promote.js +217 -0
- package/dist/property.d.ts +37 -0
- package/dist/property.js +172 -0
- package/dist/propertyWorker.d.ts +1 -0
- package/dist/propertyWorker.js +24 -0
- package/dist/queries.d.ts +37 -0
- package/dist/queries.js +297 -0
- package/dist/regressions.d.ts +31 -0
- package/dist/regressions.js +153 -0
- package/dist/reporters.d.ts +22 -0
- package/dist/reporters.js +160 -0
- package/dist/settings.d.ts +16 -0
- package/dist/settings.js +15 -0
- package/dist/smt.d.ts +33 -0
- package/dist/smt.js +396 -0
- package/dist/symbolic.d.ts +200 -0
- package/dist/symbolic.js +1727 -0
- package/dist/types.d.ts +77 -0
- package/dist/types.js +1 -0
- package/dist/ui.d.ts +16 -0
- package/dist/ui.js +326 -0
- package/dist/uiPage.d.ts +1 -0
- package/dist/uiPage.js +4 -0
- package/hooks/AGENTS.snippet.md +10 -0
- package/hooks/claude-hook.mjs +35 -0
- package/hooks/claude-settings.example.json +8 -0
- package/package.json +85 -4
- package/ui/index.html +1132 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,216 @@
|
|
|
1
|
-
#
|
|
1
|
+
# tslaws
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
```sh
|
|
4
|
+
npm i -D tslaws # also installs the compiler for your platform (tslaws-compiler-<platform>)
|
|
5
|
+
npx tslaws init
|
|
6
|
+
```
|
|
7
|
+
|
|
8
|
+
Laws for TypeScript, checked the way Bend 2 checks its `LAWS.bend`: a person writes the laws, an agent writes the code,
|
|
9
|
+
and one command says whether the code satisfies every law and whether any code is left unchecked.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// src/math.laws.ts
|
|
13
|
+
import type { int } from 'tslaws';
|
|
14
|
+
import { assume } from 'tslaws';
|
|
15
|
+
import { clamp, sortNumbers } from './math';
|
|
16
|
+
|
|
17
|
+
export law proved clampWithinBounds(x: number, lo: number, hi: number) {
|
|
18
|
+
assume(lo <= hi);
|
|
19
|
+
const y = clamp(x, lo, hi);
|
|
20
|
+
return y >= lo && y <= hi;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export law sortOrders(xs: int[]) {
|
|
24
|
+
const s = sortNumbers(xs);
|
|
25
|
+
for (let i = 1; i < s.length; i++) if (s[i - 1] > s[i]) return false;
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`law` is syntax of the `tsc-laws` compiler (a fork of [tsc-ts](https://github.com/vercel-labs/tsc-ts)). A law is a function
|
|
31
|
+
declaration the compiler type-checks like any other. It holds for an input when it returns anything but `false` and does not
|
|
32
|
+
throw. `assume(cond)` skips inputs a law does not cover. Everywhere except at the start of a statement, `law` is still an
|
|
33
|
+
ordinary identifier.
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
$ tslaws check
|
|
37
|
+
PROVED clampWithinBounds (src/math.laws.ts:5) [smt]
|
|
38
|
+
BOUNDED sortOrders (src/math.laws.ts:11) [smt]
|
|
39
|
+
holds within bounds: int values stay within ±2^26; arrays hold at most 4 elements; loops run at most 4 times
|
|
40
|
+
FAILED sortKeepsLength (src/math.laws.ts:17) [property]
|
|
41
|
+
returned false
|
|
42
|
+
counterexample: [[3,3]]
|
|
43
|
+
reproduce with --seed 1090953994
|
|
44
|
+
UNCOVERED sub (src/math.ts) [coverage]
|
|
45
|
+
no law reaches this exported function; add a law, or a person can mark it @tslaws-exempt and accept
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## What "checked against the laws" means
|
|
49
|
+
|
|
50
|
+
Two things, both required for exit status 0:
|
|
51
|
+
|
|
52
|
+
1. **Every law holds for the code.** The implementation, not a copy of it, is what runs and what is proved.
|
|
53
|
+
2. **No code escapes the laws.** Every exported function or class in the covered modules is reached by a law, directly or
|
|
54
|
+
through the functions the law calls. Covered modules are the `cover` globs of `tslaws.json`
|
|
55
|
+
(`{ "cover": ["src/**/*.ts", "!src/**/*.test.ts"] }`), or, without that file, every project module the laws import.
|
|
56
|
+
|
|
57
|
+
## Statuses
|
|
58
|
+
|
|
59
|
+
| Status | Meaning |
|
|
60
|
+
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
61
|
+
| `proved` | Z3 shows no input makes the law return `false`, with no bound involved. |
|
|
62
|
+
| `bounded` | Z3 shows it for every input within stated bounds: array length (`--bound`, default 4), loop iterations, recursion depth, `int` values within ±2^26. |
|
|
63
|
+
| `tested` | `fast-check` found no counterexample in `--runs` inputs (default 200); the code does not lower to the prover. Not a proof. |
|
|
64
|
+
| `open` | Declared `law proved …` but not proved for every input (a bounded proof does not count), or `assume` discards nearly every input. Fails the check. |
|
|
65
|
+
| `failed` | A counterexample, shrunk to a minimal input, with the seed to reproduce it. |
|
|
66
|
+
| `uncovered` | Exported code no law reaches. Fails the check. |
|
|
67
|
+
| `tampered` | A law changed, was added or removed since `tsc-laws.lock` was accepted, or a coverage exemption is not in it. Fails the check. |
|
|
68
|
+
| `error` | The law cannot run: not exported, a parameter has no type, no generator for a type, load failure. |
|
|
69
|
+
|
|
70
|
+
`tslaws check` exits 0 only when every law is `proved`, `bounded` or `tested`, nothing is `uncovered`, the files compile, and
|
|
71
|
+
the lock matches.
|
|
72
|
+
|
|
73
|
+
## Inputs come from types
|
|
74
|
+
|
|
75
|
+
The compiler emits declarations and `tslaws` reads each parameter's type from them, so the full type system drives input
|
|
76
|
+
generation: primitives and literals, unions, enums, template literals, arrays, tuples, interfaces and type literals (optional
|
|
77
|
+
properties included), index signatures, `Date`, and type parameters through their constraints. Parameters typed `number` are
|
|
78
|
+
finite doubles (no NaN, no infinities). Register a generator for a type the runner cannot derive, by its name:
|
|
79
|
+
|
|
80
|
+
```ts
|
|
81
|
+
import { arbitrary, fc } from 'tslaws';
|
|
82
|
+
arbitrary('UserId', fc.string({ minLength: 8, maxLength: 8 }));
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## The prover
|
|
86
|
+
|
|
87
|
+
`tslaws` symbolically executes the compiled JavaScript of a law, inlining the project functions it calls, and asks Z3 for an
|
|
88
|
+
input that returns `false` or breaks an obligation. A model is replayed in JavaScript, and only a reproduced failure is
|
|
89
|
+
reported as `failed`.
|
|
90
|
+
|
|
91
|
+
- **Numbers are IEEE doubles** (SMT-LIB floating point), so rounding, overflow and `-0` are real, and `(a + b) + c ===
|
|
92
|
+
a + (b + c)` is refuted. Z3 cannot decide most double arithmetic, so those laws stay `tested`; commutative `+` and `*` are
|
|
93
|
+
normalised so `a + b === b + a` is proved.
|
|
94
|
+
- **`int` is exact.** `import type { int } from 'tslaws'` marks a whole number (`number & brand`, an ordinary number at
|
|
95
|
+
runtime). Integer arithmetic carries the obligation that results stay within ±(2^53 − 1); when the full range fails only on
|
|
96
|
+
that obligation, the proof is retried with `int` values in ±2^26 and reported `bounded`. `bigint` is exact with no bound.
|
|
97
|
+
- **Arrays, loops, recursion** are unrolled up to `--bound` (default 4), making the result `bounded`. Supported: `length`,
|
|
98
|
+
indexing, literals and spread, `push`, `map`, `filter`, `every`, `some`, `reduce`, `includes`, `indexOf`, `slice(n)`,
|
|
99
|
+
`concat`, `reverse`, `sort(comparator)` (a stable sorting network), `[...new Set(xs)]`, `for`, `while`, `for…of`, `break`,
|
|
100
|
+
`continue`. Mutation is allowed only on arrays no second name can reach.
|
|
101
|
+
- **Strings** are unbounded SMT strings restricted to the BMP: `+`, templates, `length`, `charAt`, indexing, `slice`,
|
|
102
|
+
`substring`, `includes`, `startsWith`, `endsWith`, `indexOf`, comparisons.
|
|
103
|
+
- **Records** of any of these, and `typeof`, `Math.abs/min/max/floor/ceil/trunc`, ternaries, `const`/`let`, `assume`.
|
|
104
|
+
- **`forall(xs, (x, i) => cond)` and `exists(...)`** (from `tslaws`) quantify over arrays in `assume`, law results and
|
|
105
|
+
`invariant`s. The prover lowers them over the array slots, so a claim that uses them is `bounded` by `--bound`.
|
|
106
|
+
|
|
107
|
+
- **Loop invariants give proofs for every number of iterations.** Put `invariant(cond)` first in a loop body (`import {
|
|
108
|
+
invariant } from 'tslaws'`; it throws at runtime when false, so property tests check it on every iteration). The prover
|
|
109
|
+
proves the invariant on entry, assumes it at the top of an arbitrary iteration, proves it again at the end, and gives the
|
|
110
|
+
code after the loop an arbitrary state where the invariant holds and the condition is false. No unrolling, so no bound:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
export function sumTo(n: int): int {
|
|
114
|
+
let s = 0,
|
|
115
|
+
i = 0;
|
|
116
|
+
while (i < n) {
|
|
117
|
+
invariant(0 <= i && i <= n);
|
|
118
|
+
invariant(2 * s === i * (i - 1));
|
|
119
|
+
s += i;
|
|
120
|
+
i += 1;
|
|
121
|
+
}
|
|
122
|
+
return s as int;
|
|
123
|
+
}
|
|
124
|
+
// law proved: 2 * sumTo(n) === n * (n - 1), for every n >= 0
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Invariants live in the implementation, so the agent writes them; the laws stay the person's. Restrictions: the loop may
|
|
128
|
+
change numbers, booleans and strings but only read arrays and records, and may not `return`, `break` or `continue`.
|
|
129
|
+
This is partial correctness (a loop that never ends is not flagged).
|
|
130
|
+
|
|
131
|
+
Anything else leaves the law `tested`, or a `law proved` `open`, with the reason. Z3 runs in a worker thread that is terminated if a query outlives its timeout, so a hard query cannot hang the check; property tests run in a worker too, and a law that outlives `--property-timeout` (default 30000 ms) is reported `error`: the law or the code it calls may not terminate. `TSLAWS_DEBUG=1` keeps each query as an
|
|
132
|
+
`.smt2` file under `.tslaws/out/smt` to replay in `z3`.
|
|
133
|
+
|
|
134
|
+
## Browser laws
|
|
135
|
+
|
|
136
|
+
A law file named `*.browser.laws.ts` runs in a real browser (Chrome, Edge or Chromium; `TSLAWS_BROWSER` names another
|
|
137
|
+
executable). Node generates and shrinks the inputs; each one is sent to a page where the compiled law and your code run
|
|
138
|
+
against a live DOM. The law may be `async`. `tslaws/browser` has `mount`, `settle`, `click`, `type` and `fire`, and works with
|
|
139
|
+
any framework or none: `mount(el => render(el))` accepts whatever renders into an element and may return its unmount.
|
|
140
|
+
`a11y` and `accessible` audit a mounted component with the optional `axe-core` peer dependency, and `--browser firefox|webkit` picks another engine (`npx playwright install firefox`).
|
|
141
|
+
`mountComponent` with the React, Angular, DOM and custom-element adapters tests one component in isolation with props generated from its props type (function-typed props included), plus `spy`, `rendersSame` and Testing Library style queries.
|
|
142
|
+
Browser laws are `tested`, never `proved`. See [docs/laws.md](../../docs/laws.md#browser-laws).
|
|
143
|
+
|
|
144
|
+
A `*.browser.laws.ts` file that imports `tslaws/e2e` instead is an end-to-end law: it runs in Node and drives your whole running app through a real browser (`page()`, `open`, `newPage`, `route`, `settle`, `accessible`). Configure `e2e.server` in `tslaws.json` and the runner starts and stops your dev server. See [End-to-end laws](../../docs/laws.md#end-to-end-laws).
|
|
145
|
+
|
|
146
|
+
- **Frameworks:** `tslaws.json` `compilerOptions` (`jsx`, `jsxFactory`, `jsxFragmentFactory`, `jsxImportSource`, `experimentalDecorators`,
|
|
147
|
+
`emitDecoratorMetadata`, `useDefineForClassFields`, `lib`, `types`, `strict`, ...) and `.tsx` sources let React, Angular and any DOM
|
|
148
|
+
library be tested. See [docs/laws.md](../../docs/laws.md#frameworks).
|
|
149
|
+
- **Stateful laws:** a law over `ops: Op[]` (a union type) can use `runModel` or `runModelAsync` to apply the operations to the
|
|
150
|
+
real thing and to a simple model and compare them after every step. Failing sequences shrink to the shortest one. See
|
|
151
|
+
[docs/laws.md](../../docs/laws.md#stateful-laws).
|
|
152
|
+
|
|
153
|
+
## Dashboard
|
|
154
|
+
|
|
155
|
+
`tslaws ui [files...] [--port N] [--no-open]` serves a local dashboard (127.0.0.1 only): a tree of files and laws,
|
|
156
|
+
live progress, a live view of the browser page during browser laws, status filters, search, a report with a copyable reproduce command, the law's source, and live reruns on save.
|
|
157
|
+
Laws are timed (the five slowest are listed), the last 20 runs show as a row of coloured squares, laws that flip between passing and failing are marked `flaky`, and any single law can be rerun.
|
|
158
|
+
See [docs/laws.md](../../docs/laws.md#the-dashboard).
|
|
159
|
+
|
|
160
|
+
`tslaws mutate [files...] [--max N] [--seed N] [--json]` mutation-tests the laws: it mutates the covered code, re-runs the laws against each mutant, and lists the survivors no law caught. It handles `.tsx`/`.jsx` and projects with a lock, and takes the run flags of `check`. See [docs/laws.md](../../docs/laws.md#mutation-testing).
|
|
161
|
+
|
|
162
|
+
- **Settings:** `tslaws.json` `runs`, `bound`, `proofTimeoutMs` and `propertyTimeoutMs` set the run defaults; a command line flag beats them. See [docs/laws.md](../../docs/laws.md#settings).
|
|
163
|
+
|
|
164
|
+
## Agents cannot move the goalposts
|
|
165
|
+
|
|
166
|
+
`tsc-laws.lock` holds a hash of every law declaration and the list of coverage exemptions. A change, an added law, a removed
|
|
167
|
+
law or a new `@tslaws-exempt` fails the check until a person runs `tslaws accept`, which refuses to run without a terminal,
|
|
168
|
+
under `CI`, or with `TSLAWS_AGENT` set. Agents propose laws in `*.proposed.laws.ts` and try them with `tslaws check
|
|
169
|
+
--proposed` (no lock, no coverage gate); a person moves them into a `*.laws.ts` file and accepts.
|
|
170
|
+
|
|
171
|
+
`hooks/claude-hook.mjs` is a Claude Code hook that blocks `git commit` and sends an agent back to work when the check fails;
|
|
172
|
+
`hooks/claude-settings.example.json` wires it up and `hooks/AGENTS.snippet.md` tells the agent the rules.
|
|
173
|
+
|
|
174
|
+
## Commands
|
|
175
|
+
|
|
176
|
+
```
|
|
177
|
+
tslaws init [--force]
|
|
178
|
+
tslaws accept [files...]
|
|
179
|
+
tslaws check [files...] [--json] [--summary] [--watch] [--runs N] [--seed N] [--no-prove] [--bound N]
|
|
180
|
+
[--proof-timeout MS] [--property-timeout MS] [--proposed] [--compiler PATH]
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
- `init` scaffolds `tslaws.json`, the `.tslaws` ignore entry, the agent rules in `AGENTS.md`, the Claude Code hook in
|
|
184
|
+
`.claude/settings.json`, and a starter law if none exists. It never writes the lock; a person then runs `accept`.
|
|
185
|
+
- `accept` records the laws and the `@tslaws-exempt` functions in `tsc-laws.lock`. It refuses to run without a terminal,
|
|
186
|
+
under `CI`, or with `TSLAWS_AGENT` set.
|
|
187
|
+
- `check` exits 0 only when every law is `proved`, `bounded` or `tested`, nothing is `uncovered`, the files compile and the
|
|
188
|
+
lock matches. `--json` adds a `hint` to each result, the concrete next action for an agent. `--summary` prints only the
|
|
189
|
+
results that do not pass, plus the totals. `--watch` re-runs on every `.ts` change.
|
|
190
|
+
- `check --reporter text|json|junit|github` picks the output: `junit` is JUnit XML for CI test reports, `github` prints workflow
|
|
191
|
+
annotations and appends a table to `GITHUB_STEP_SUMMARY`. See "CI reporters" in `docs/laws.md`.
|
|
192
|
+
|
|
193
|
+
- `tslaws mcp` serves `tslaws_check`, `tslaws_check_proposed`, `tslaws_explain` and `tslaws_propose` over MCP (stdio) for
|
|
194
|
+
agents; it cannot accept laws or touch the lock. `tslaws promote` is the person's step that moves proposed laws into the
|
|
195
|
+
accepted file. See [docs/laws.md](../../docs/laws.md#agent-integration-mcp).
|
|
196
|
+
- Counterexamples are kept in `.tslaws/regressions` (committable) and replayed before the random search, so a fixed bug cannot
|
|
197
|
+
return unnoticed. `check --no-regressions` skips the corpus; `tslaws regressions [list|clear]` shows or deletes it.
|
|
198
|
+
|
|
199
|
+
Without files, every `*.laws.ts` under the current directory is used. The compiler is `--compiler`, `TSC_LAWS_BIN`, or
|
|
200
|
+
`tsc-laws` on `PATH`; it finds its lib files through `TSC_TS_LIB_DIR`, or the TypeScript 7 libs scriptc installs in
|
|
201
|
+
`node_modules/@scriptc/*`.
|
|
202
|
+
|
|
203
|
+
See [docs/laws.md](../../docs/laws.md) for the language, how it compares with Bend 2, and the limits.
|
|
204
|
+
|
|
205
|
+
## Build and test
|
|
206
|
+
|
|
207
|
+
```
|
|
208
|
+
npm ci
|
|
209
|
+
npm run fetch:upstream && npm run build:native:dev -w tsc-ts # needs clang (and zig on Windows)
|
|
210
|
+
npm run build -w tslaws
|
|
211
|
+
TSC_LAWS_BIN=packages/tsc-ts/dist/tsc npm test -w tslaws
|
|
212
|
+
TSC_TS_BIN=packages/tsc-ts/dist/tsc node --test packages/tsc-ts/tests/laws.test.mjs
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`tsc-ts --laws-manifest <files>` lists the laws of files as JSON (name, parameters, line, declaration text, and the problems
|
|
216
|
+
that stop a law running); `tslaws` uses it for the lock and for reporting.
|
package/bin/tslaws.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { Adapter } from '../component.ts';
|
|
2
|
+
/** The part of Angular's `ApplicationRef` the adapter uses. */
|
|
3
|
+
export interface AngularAppRefLike {
|
|
4
|
+
injector: unknown;
|
|
5
|
+
attachView(view: any): void;
|
|
6
|
+
tick(): void;
|
|
7
|
+
whenStable?(): Promise<unknown>;
|
|
8
|
+
destroy(): void;
|
|
9
|
+
}
|
|
10
|
+
/** The part of Angular's `ComponentRef` the adapter uses. */
|
|
11
|
+
export interface AngularComponentRefLike {
|
|
12
|
+
setInput(name: string, value: unknown): void;
|
|
13
|
+
hostView: unknown;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* The functions to pass, all from Angular itself:
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* import { createComponent, provideZonelessChangeDetection } from '@angular/core';
|
|
20
|
+
* import { createApplication } from '@angular/platform-browser';
|
|
21
|
+
* angularAdapter({ createApplication, createComponent, provideZonelessChangeDetection });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Without `provideZonelessChangeDetection` the application needs zone.js (import it in the law's module).
|
|
25
|
+
*/
|
|
26
|
+
export interface AngularFunctions {
|
|
27
|
+
/** `createApplication` from `@angular/platform-browser`. */
|
|
28
|
+
createApplication(options?: any): Promise<AngularAppRefLike>;
|
|
29
|
+
/** `createComponent` from `@angular/core`. */
|
|
30
|
+
createComponent(component: any, options: any): AngularComponentRefLike;
|
|
31
|
+
/** `provideZonelessChangeDetection` from `@angular/core`; when given it is added to the application's providers. */
|
|
32
|
+
provideZonelessChangeDetection?(): unknown;
|
|
33
|
+
/** Replaces `ref.setInput(name, value)`, for example to also assign outputs. */
|
|
34
|
+
setInput?(ref: AngularComponentRefLike, name: string, value: unknown): void;
|
|
35
|
+
/** Extra providers for the application. */
|
|
36
|
+
providers?: unknown[];
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Renders a standalone Angular component. Props are set with `ref.setInput`; a rerender sets them again and runs change
|
|
40
|
+
* detection; unmount destroys the application.
|
|
41
|
+
*/
|
|
42
|
+
export declare function angularAdapter<P extends object = any>(angular: AngularFunctions): Adapter<unknown, P>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a standalone Angular component. Props are set with `ref.setInput`; a rerender sets them again and runs change
|
|
3
|
+
* detection; unmount destroys the application.
|
|
4
|
+
*/
|
|
5
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
6
|
+
export function angularAdapter(angular) {
|
|
7
|
+
return {
|
|
8
|
+
async render(container, component, props) {
|
|
9
|
+
const providers = [...(angular.providers ?? [])];
|
|
10
|
+
if (angular.provideZonelessChangeDetection !== undefined)
|
|
11
|
+
providers.push(angular.provideZonelessChangeDetection());
|
|
12
|
+
const app = await angular.createApplication({ providers });
|
|
13
|
+
const host = document.createElement('div');
|
|
14
|
+
container.appendChild(host);
|
|
15
|
+
const ref = angular.createComponent(component, { environmentInjector: app.injector, hostElement: host });
|
|
16
|
+
const set = (next) => {
|
|
17
|
+
for (const [name, value] of Object.entries(next)) {
|
|
18
|
+
if (angular.setInput !== undefined)
|
|
19
|
+
angular.setInput(ref, name, value);
|
|
20
|
+
else
|
|
21
|
+
ref.setInput(name, value);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
const detect = async () => {
|
|
25
|
+
app.tick();
|
|
26
|
+
await app.whenStable?.();
|
|
27
|
+
};
|
|
28
|
+
set(props);
|
|
29
|
+
app.attachView(ref.hostView);
|
|
30
|
+
await detect();
|
|
31
|
+
return {
|
|
32
|
+
update: async (next) => {
|
|
33
|
+
set(next);
|
|
34
|
+
await detect();
|
|
35
|
+
},
|
|
36
|
+
unmount: () => app.destroy(),
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Adapter } from '../component.ts';
|
|
2
|
+
/** A vanilla component: a function from props to a node or markup. */
|
|
3
|
+
export type DomComponent<P = any> = (props: P) => Node | string;
|
|
4
|
+
/** Renders a `(props) => Node | string` function into the container; a rerender replaces the content. */
|
|
5
|
+
export declare const domAdapter: Adapter<DomComponent, any>;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function fill(container, content) {
|
|
2
|
+
if (typeof content === 'string')
|
|
3
|
+
container.innerHTML = content;
|
|
4
|
+
else
|
|
5
|
+
container.replaceChildren(content);
|
|
6
|
+
}
|
|
7
|
+
/** Renders a `(props) => Node | string` function into the container; a rerender replaces the content. */
|
|
8
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
9
|
+
export const domAdapter = {
|
|
10
|
+
render(container, component, props) {
|
|
11
|
+
fill(container, component(props));
|
|
12
|
+
return {
|
|
13
|
+
update: (next) => fill(container, component(next)),
|
|
14
|
+
unmount: () => container.replaceChildren(),
|
|
15
|
+
};
|
|
16
|
+
},
|
|
17
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Adapter } from '../component.ts';
|
|
2
|
+
export interface ElementAdapterOptions {
|
|
3
|
+
/** Also set each string, number or boolean prop as an attribute (default: properties only). */
|
|
4
|
+
attributes?: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Renders a custom element: props are assigned as properties (and optionally attributes). The `component` argument may
|
|
8
|
+
* be the element's class, defined under `tagName` when it is not yet, or `undefined` when it is already registered.
|
|
9
|
+
*/
|
|
10
|
+
export declare function elementAdapter<P extends object = any>(tagName: string, options?: ElementAdapterOptions): Adapter<CustomElementConstructor | undefined, P>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a custom element: props are assigned as properties (and optionally attributes). The `component` argument may
|
|
3
|
+
* be the element's class, defined under `tagName` when it is not yet, or `undefined` when it is already registered.
|
|
4
|
+
*/
|
|
5
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
6
|
+
export function elementAdapter(tagName, options = {}) {
|
|
7
|
+
const assign = (el, props) => {
|
|
8
|
+
for (const [key, value] of Object.entries(props)) {
|
|
9
|
+
el[key] = value;
|
|
10
|
+
if (options.attributes && ['string', 'number', 'boolean'].includes(typeof value)) {
|
|
11
|
+
if (value === false)
|
|
12
|
+
el.removeAttribute(key);
|
|
13
|
+
else
|
|
14
|
+
el.setAttribute(key, value === true ? '' : String(value));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
return {
|
|
19
|
+
render(container, component, props) {
|
|
20
|
+
if (component !== undefined && customElements.get(tagName) === undefined)
|
|
21
|
+
customElements.define(tagName, component);
|
|
22
|
+
const el = document.createElement(tagName);
|
|
23
|
+
container.appendChild(el);
|
|
24
|
+
assign(el, props);
|
|
25
|
+
return { update: (next) => assign(el, next), unmount: () => el.remove() };
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { Adapter } from '../component.ts';
|
|
2
|
+
/** The part of React's `createRoot` result the adapter uses. */
|
|
3
|
+
export interface ReactRootLike {
|
|
4
|
+
render(node: unknown): void;
|
|
5
|
+
unmount(): void;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The functions to pass, all from React itself:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* import { createElement } from 'react';
|
|
12
|
+
* import { createRoot } from 'react-dom/client';
|
|
13
|
+
* import { flushSync } from 'react-dom';
|
|
14
|
+
* reactAdapter({ createRoot, createElement, flushSync });
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export interface ReactFunctions {
|
|
18
|
+
createRoot(container: any): ReactRootLike;
|
|
19
|
+
createElement(type: any, props: any): unknown;
|
|
20
|
+
/** When given, renders are committed synchronously. */
|
|
21
|
+
flushSync?(callback: () => void): void;
|
|
22
|
+
}
|
|
23
|
+
/** Renders a React component with `createRoot`; a rerender calls `root.render`, unmount calls `root.unmount`. */
|
|
24
|
+
export declare function reactAdapter<P extends object = any>(react: ReactFunctions): Adapter<unknown, P>;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Renders a React component with `createRoot`; a rerender calls `root.render`, unmount calls `root.unmount`. */
|
|
2
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
3
|
+
export function reactAdapter(react) {
|
|
4
|
+
const sync = (work) => (react.flushSync !== undefined ? react.flushSync(work) : work());
|
|
5
|
+
return {
|
|
6
|
+
render(container, component, props) {
|
|
7
|
+
const root = react.createRoot(container);
|
|
8
|
+
sync(() => root.render(react.createElement(component, props)));
|
|
9
|
+
return {
|
|
10
|
+
update: (next) => sync(() => root.render(react.createElement(component, next))),
|
|
11
|
+
unmount: () => sync(() => root.unmount()),
|
|
12
|
+
};
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|