@stateward/core 1.0.0 → 1.0.1
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 +80 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,12 +1,88 @@
|
|
|
1
1
|
# @stateward/core
|
|
2
2
|
|
|
3
3
|
The engine of [stateward](https://github.com/khoing1111/ts-stateward#readme) and the API for
|
|
4
|
-
writing rules:
|
|
5
|
-
|
|
4
|
+
writing rules of your own: a convention your team keeps that no pack checks (a limit, a naming
|
|
5
|
+
rule, a call nobody should make). Your rule lives next to your code, loads from
|
|
6
|
+
`stateward.config.ts`, and reports in the terminal, in CI and in VS Code like any bundled rule.
|
|
7
|
+
|
|
8
|
+
`stateward` installs this package; add it yourself only to write a rule, at the same version as
|
|
9
|
+
`stateward`:
|
|
6
10
|
|
|
7
11
|
```bash
|
|
8
12
|
npm install --save-dev @stateward/core
|
|
9
13
|
```
|
|
10
14
|
|
|
11
|
-
|
|
12
|
-
|
|
15
|
+
## What it exports
|
|
16
|
+
|
|
17
|
+
- `defineRule`, `definePlugin` and `option`: a rule, the plugin that holds it, and its options.
|
|
18
|
+
- `@stateward/core/typescript`: the TypeScript that stateward reads with. Import it, never your
|
|
19
|
+
app's `typescript`.
|
|
20
|
+
- `@stateward/core/testing`: `checkFixture` to run a rule on a small project in a test, and
|
|
21
|
+
`expectMessageStandard` to check its messages.
|
|
22
|
+
|
|
23
|
+
## Example
|
|
24
|
+
|
|
25
|
+
A rule that warns when a union has more members than the team allows:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
// stateward-rules/team.ts
|
|
29
|
+
import ts from "@stateward/core/typescript";
|
|
30
|
+
import { definePlugin, defineRule, option } from "@stateward/core";
|
|
31
|
+
|
|
32
|
+
export const maxUnionMembers = defineRule({
|
|
33
|
+
kind: "file", // one file at a time; "project" looks across files
|
|
34
|
+
meta: {
|
|
35
|
+
description: "A type alias whose union has more members than the team's limit.",
|
|
36
|
+
defaultSeverity: "warning",
|
|
37
|
+
tier: "core",
|
|
38
|
+
group: "unions",
|
|
39
|
+
options: {
|
|
40
|
+
max: option.number({ default: 12, min: 1, integer: true, description: "Most members." }),
|
|
41
|
+
},
|
|
42
|
+
explain: "# team/max-union-members\n…", // the page `stateward explain` shows
|
|
43
|
+
},
|
|
44
|
+
check(ctx) {
|
|
45
|
+
const visit = (node: ts.Node): void => {
|
|
46
|
+
if (ts.isTypeAliasDeclaration(node) && ts.isUnionTypeNode(node.type)) {
|
|
47
|
+
const count = node.type.types.length;
|
|
48
|
+
if (count > ctx.options.max) {
|
|
49
|
+
ctx.report({
|
|
50
|
+
node: node.name,
|
|
51
|
+
message: `\`${node.name.text}\` has ${String(count)} members; above ${String(ctx.options.max)}, switches over it get hard to keep complete`,
|
|
52
|
+
hint: "split it into nested unions by kind",
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
ts.forEachChild(node, visit);
|
|
57
|
+
};
|
|
58
|
+
visit(ctx.sourceFile);
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
export const team = definePlugin({
|
|
63
|
+
name: "team",
|
|
64
|
+
description: "This team's own rules.",
|
|
65
|
+
rules: { "max-union-members": maxUnionMembers },
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Load it in `stateward.config.ts`; the rule's id is the plugin's name, a slash, and its key:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
import { defineConfig } from "stateward";
|
|
73
|
+
import { team } from "./stateward-rules/team.js";
|
|
74
|
+
|
|
75
|
+
export default defineConfig({
|
|
76
|
+
plugins: [team],
|
|
77
|
+
rules: { "team/max-union-members": ["warning", { max: 12 }] },
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Then `npx stateward check --rule team/max-union-members`.
|
|
82
|
+
|
|
83
|
+
## Docs
|
|
84
|
+
|
|
85
|
+
- [Write a rule of your own](https://github.com/khoing1111/ts-stateward/blob/main/docs/user/write-your-own-rule.md):
|
|
86
|
+
options, tests, messages, quick fixes, and what to do when the rule cannot tell.
|
|
87
|
+
- [The bundled rules](https://github.com/khoing1111/ts-stateward/blob/main/docs/rules/README.md):
|
|
88
|
+
check them first; what you want may be an option of one.
|
package/package.json
CHANGED