@typeonce/oxlint-plugin-ritmo 0.12.0 → 0.13.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/CHANGELOG.md +14 -0
- package/README.md +21 -3
- package/dist/index.js +7 -3
- package/dist/index.js.map +1 -1
- package/dist/rules/no-infinity-in-simulation-data.js +1 -1
- package/dist/rules/no-infinity-in-simulation-data.js.map +1 -1
- package/dist/rules/no-state-spread.d.ts +8 -0
- package/dist/rules/no-state-spread.d.ts.map +1 -0
- package/dist/rules/no-state-spread.js +44 -0
- package/dist/rules/no-state-spread.js.map +1 -0
- package/package.json +1 -1
- package/src/index.ts +7 -3
- package/src/rules/no-infinity-in-simulation-data.ts +1 -1
- package/src/rules/no-state-spread.ts +47 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @typeonce/oxlint-plugin-ritmo
|
|
2
2
|
|
|
3
|
+
## 0.13.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 791b52a: **`ritmo/no-state-spread`** reports `_tag` set over a spread of the previous state, such as `{ ...ground, _tag: "air", vy: 2 }`: the old state's fields survive at runtime while the new state's type hides them. Move an entity state with `cell.transition({ from, to, data })`, queue a state machine's move with the new state's fields (`nextMachines.x.set({ _tag, ...fields })`), or write the new state's fields out. Spreads that change fields of the same state are not reported. Turn it on for every file:
|
|
8
|
+
|
|
9
|
+
```jsonc
|
|
10
|
+
"ritmo/no-state-spread": "error"
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- 835f6ba: `ritmo/no-infinity-in-simulation-data` no longer names `Decode`: its fix is a nullable number schema from the game's schema library.
|
|
16
|
+
|
|
3
17
|
## 0.12.0
|
|
4
18
|
|
|
5
19
|
## 0.11.0
|
package/README.md
CHANGED
|
@@ -6,6 +6,7 @@ Ritmo's types reject wrong calls, and its devtools lints reject wrong runs. Thes
|
|
|
6
6
|
|
|
7
7
|
- simulation code that reads the wall clock, `Math.random`, engine-dependent math, or the page;
|
|
8
8
|
- data that cannot be saved;
|
|
9
|
+
- state values that keep a previous state's fields;
|
|
9
10
|
- runtime results that are dropped.
|
|
10
11
|
|
|
11
12
|
Every report says what goes wrong and which Ritmo API to use instead.
|
|
@@ -26,7 +27,8 @@ Register the plugin and turn the rules on in `.oxlintrc.json`. The simulation ru
|
|
|
26
27
|
"rules": {
|
|
27
28
|
"ritmo/handle-tick-result": "error",
|
|
28
29
|
"ritmo/prefix-descriptor-and-system-names": "error",
|
|
29
|
-
"ritmo/use-cell-value": "error"
|
|
30
|
+
"ritmo/use-cell-value": "error",
|
|
31
|
+
"ritmo/no-state-spread": "error"
|
|
30
32
|
},
|
|
31
33
|
"overrides": [
|
|
32
34
|
{
|
|
@@ -63,6 +65,7 @@ const started = performance.now()
|
|
|
63
65
|
| [`handle-tick-result`](#ritmohandle-tick-result) | everywhere | a dropped `runtime.tick(...)` / `restore(...)` result | check `.ok` |
|
|
64
66
|
| [`prefix-descriptor-and-system-names`](#ritmoprefix-descriptor-and-system-names) | everywhere | `"Position"`, `Game.System("Move", ...)` | `"Game/Position"` |
|
|
65
67
|
| [`use-cell-value`](#ritmouse-cell-value) | everywhere | `cell.present ? cell.get() : undefined` | `cell.value()` |
|
|
68
|
+
| [`no-state-spread`](#ritmono-state-spread) | everywhere | `{ ...ground, _tag: "air", vy }` | `cell.transition({ from, to, data })`, `nextMachines.x.set({ _tag, ...fields })`, or the new state's fields written out |
|
|
66
69
|
|
|
67
70
|
## Simulation rules
|
|
68
71
|
|
|
@@ -130,14 +133,14 @@ Options: `{ "packages": ["@my-game/canvas"] }` adds the game's own renderer or U
|
|
|
130
133
|
|
|
131
134
|
### `ritmo/no-infinity-in-simulation-data`
|
|
132
135
|
|
|
133
|
-
`JSON.stringify` turns `Infinity` and `NaN` into `null`, and
|
|
136
|
+
`JSON.stringify` turns `Infinity` and `NaN` into `null`, and a number schema rejects `null`, so a world holding them saves but no longer loads (devtools also warns `non-finite-value`).
|
|
134
137
|
|
|
135
138
|
```ts
|
|
136
139
|
dash: { remaining: 0, since: Infinity } // reported
|
|
137
140
|
{ remaining: Infinity, duration: Infinity, permanent: true } // reported
|
|
138
141
|
|
|
139
142
|
// instead
|
|
140
|
-
dash: { remaining: 0, since: null } // since: number | null,
|
|
143
|
+
dash: { remaining: 0, since: null } // since: number | null, with a nullable number schema
|
|
141
144
|
{ timer: null } // timer: { remaining: number; duration: number } | null
|
|
142
145
|
```
|
|
143
146
|
|
|
@@ -189,4 +192,19 @@ const dash = target.data.dash.value()
|
|
|
189
192
|
|
|
190
193
|
Check `cell.present` alone to branch without reading.
|
|
191
194
|
|
|
195
|
+
### `ritmo/no-state-spread`
|
|
196
|
+
|
|
197
|
+
A tagged value, such as the value of a `Descriptor.EntityState` component or of a `Game.StateMachine`, changes state with the new state's own fields. Setting `_tag` over a spread of the previous state keeps every field of that state at runtime, while the new state's type hides them: they come back when the value is saved, compared, or spread again.
|
|
198
|
+
|
|
199
|
+
```ts
|
|
200
|
+
const popped = { ...ground, _tag: "air", vy: 2 } // reported: `platform` survives in the air
|
|
201
|
+
|
|
202
|
+
// instead
|
|
203
|
+
data.contact.transition({ from: "ground", to: "air", data: { vy: 2, coyote: 0 } })
|
|
204
|
+
nextMachines.mode.set({ _tag: "Exploring" }) // a machine's move, not { ...talking, _tag: "Exploring" }
|
|
205
|
+
const popped = { _tag: "air", vy: 2, coyote: 0 } // in a pure function
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Spreading the previous value to change fields of the same state (`{ ...air, vy: air.vy - 1 }`) is not reported, nor is a `_tag` written before the spread (`{ _tag: to, ...data }`).
|
|
209
|
+
|
|
192
210
|
Source: https://github.com/typeonce-dev/ritmo
|
package/dist/index.js
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* Types catch wrong calls and devtools catch wrong runs; these rules catch
|
|
5
5
|
* the source patterns neither can see: simulation code that reads the wall
|
|
6
6
|
* clock, `Math.random`, engine-dependent math, or the page, data that cannot
|
|
7
|
-
* be saved,
|
|
7
|
+
* be saved, state values that keep a previous state's fields, and runtime
|
|
8
|
+
* results that are dropped. Every report names the
|
|
8
9
|
* Ritmo API to use instead.
|
|
9
10
|
*
|
|
10
11
|
* Register the plugin in `.oxlintrc.json` with `"jsPlugins":
|
|
@@ -17,7 +18,8 @@
|
|
|
17
18
|
* `ritmo/use-random-for-randomness`, `ritmo/use-ops-for-math`,
|
|
18
19
|
* `ritmo/keep-simulation-headless`, `ritmo/no-infinity-in-simulation-data`.
|
|
19
20
|
* - Every file: `ritmo/handle-tick-result`,
|
|
20
|
-
* `ritmo/prefix-descriptor-and-system-names`, `ritmo/use-cell-value
|
|
21
|
+
* `ritmo/prefix-descriptor-and-system-names`, `ritmo/use-cell-value`,
|
|
22
|
+
* `ritmo/no-state-spread`.
|
|
21
23
|
*
|
|
22
24
|
* @module oxlint-plugin-ritmo
|
|
23
25
|
*/
|
|
@@ -25,6 +27,7 @@ import { definePlugin } from "@oxlint/plugins";
|
|
|
25
27
|
import handleTickResult from "./rules/handle-tick-result.js";
|
|
26
28
|
import keepSimulationHeadless from "./rules/keep-simulation-headless.js";
|
|
27
29
|
import noInfinityInSimulationData from "./rules/no-infinity-in-simulation-data.js";
|
|
30
|
+
import noStateSpread from "./rules/no-state-spread.js";
|
|
28
31
|
import prefixDescriptorAndSystemNames from "./rules/prefix-descriptor-and-system-names.js";
|
|
29
32
|
import useCellValue from "./rules/use-cell-value.js";
|
|
30
33
|
import useClockForTime from "./rules/use-clock-for-time.js";
|
|
@@ -40,7 +43,8 @@ export default definePlugin({
|
|
|
40
43
|
"no-infinity-in-simulation-data": noInfinityInSimulationData,
|
|
41
44
|
"handle-tick-result": handleTickResult,
|
|
42
45
|
"prefix-descriptor-and-system-names": prefixDescriptorAndSystemNames,
|
|
43
|
-
"use-cell-value": useCellValue
|
|
46
|
+
"use-cell-value": useCellValue,
|
|
47
|
+
"no-state-spread": noStateSpread
|
|
44
48
|
}
|
|
45
49
|
});
|
|
46
50
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,gBAAgB,MAAM,+BAA+B,CAAA;AAC5D,OAAO,sBAAsB,MAAM,qCAAqC,CAAA;AACxE,OAAO,0BAA0B,MAAM,2CAA2C,CAAA;AAClF,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,8BAA8B,MAAM,+CAA+C,CAAA;AAC1F,OAAO,YAAY,MAAM,2BAA2B,CAAA;AACpD,OAAO,eAAe,MAAM,+BAA+B,CAAA;AAC3D,OAAO,aAAa,MAAM,6BAA6B,CAAA;AACvD,OAAO,sBAAsB,MAAM,sCAAsC,CAAA;AAEzE,eAAe,YAAY,CAAC;IAC1B,IAAI,EAAE,EAAE,IAAI,EAAE,OAAO,EAAE;IACvB,KAAK,EAAE;QACL,oBAAoB,EAAE,eAAe;QACrC,2BAA2B,EAAE,sBAAsB;QACnD,kBAAkB,EAAE,aAAa;QACjC,0BAA0B,EAAE,sBAAsB;QAClD,gCAAgC,EAAE,0BAA0B;QAC5D,oBAAoB,EAAE,gBAAgB;QACtC,oCAAoC,EAAE,8BAA8B;QACpE,gBAAgB,EAAE,YAAY;QAC9B,iBAAiB,EAAE,aAAa;KACjC;CACF,CAAC,CAAA"}
|
|
@@ -24,7 +24,7 @@ export default defineRule({
|
|
|
24
24
|
url: docsUrl("no-infinity-in-simulation-data")
|
|
25
25
|
},
|
|
26
26
|
messages: {
|
|
27
|
-
nonFinite: "`{{key}}: {{value}}` cannot be saved: JSON turns it into null and
|
|
27
|
+
nonFinite: "`{{key}}: {{value}}` cannot be saved: JSON turns it into null and a number schema rejects it, so the save no longer loads (and devtools warns `non-finite-value`). Use `null` for \"never\" or \"no limit\": type the field `number | null` and give it a nullable number schema. For a value that only some entities have (a timer on a non-permanent pool), make that part optional: `timer: { remaining: number; duration: number } | null`."
|
|
28
28
|
},
|
|
29
29
|
schema: []
|
|
30
30
|
},
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"no-infinity-in-simulation-data.js","sourceRoot":"","sources":["../../src/rules/no-infinity-in-simulation-data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAE9E,8EAA8E;AAC9E,MAAM,SAAS,GAAG,CAAC,OAAgB,EAAE,IAAiB,EAAW,EAAE;IACjE,IAAI,IAAI,CAAC,IAAI,KAAK,iBAAiB,IAAI,CAAC,IAAI,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,KAAK,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IACjI,IAAI,kBAAkB,CAAC,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,kBAAkB,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IAC1G,IAAI,IAAI,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;QAClD,OAAO,IAAI,KAAK,mBAAmB,IAAI,IAAI,KAAK,mBAAmB,IAAI,IAAI,KAAK,KAAK,CAAA;IACvF,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED;;;GAGG;AACH,eAAe,UAAU,CAAC;IACxB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,yEAAyE;YACtF,GAAG,EAAE,OAAO,CAAC,gCAAgC,CAAC;SAC/C;QACD,QAAQ,EAAE;YACR,SAAS,EACP,
|
|
1
|
+
{"version":3,"file":"no-infinity-in-simulation-data.js","sourceRoot":"","sources":["../../src/rules/no-infinity-in-simulation-data.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAA;AAE9E,8EAA8E;AAC9E,MAAM,SAAS,GAAG,CAAC,OAAgB,EAAE,IAAiB,EAAW,EAAE;IACjE,IAAI,IAAI,CAAC,IAAI,KAAK,iBAAiB,IAAI,CAAC,IAAI,CAAC,QAAQ,KAAK,GAAG,IAAI,IAAI,CAAC,QAAQ,KAAK,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAA;IACjI,IAAI,kBAAkB,CAAC,OAAO,EAAE,IAAI,EAAE,UAAU,CAAC,IAAI,kBAAkB,CAAC,OAAO,EAAE,IAAI,EAAE,KAAK,CAAC;QAAE,OAAO,IAAI,CAAA;IAC1G,IAAI,IAAI,CAAC,IAAI,KAAK,kBAAkB,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,YAAY,CAAC,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAA;QAClD,OAAO,IAAI,KAAK,mBAAmB,IAAI,IAAI,KAAK,mBAAmB,IAAI,IAAI,KAAK,KAAK,CAAA;IACvF,CAAC;IACD,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED;;;GAGG;AACH,eAAe,UAAU,CAAC;IACxB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,yEAAyE;YACtF,GAAG,EAAE,OAAO,CAAC,gCAAgC,CAAC;SAC/C;QACD,QAAQ,EAAE;YACR,SAAS,EACP,ibAAib;SACpb;QACD,MAAM,EAAE,EAAE;KACX;IACD,MAAM,CAAC,OAAO;QACZ,OAAO;YACL,QAAQ,CAAC,IAAI;gBACX,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC;oBAAE,OAAM;gBACnE,OAAO,CAAC,MAAM,CAAC;oBACb,IAAI;oBACJ,SAAS,EAAE,WAAW;oBACtB,IAAI,EAAE,EAAE,GAAG,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;iBACnG,CAAC,CAAA;YACJ,CAAC;SACF,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A tagged value changes state by writing the new state's fields, never by
|
|
3
|
+
* re-tagging a spread of the old one: the old state's fields would survive at
|
|
4
|
+
* runtime while the type hides them.
|
|
5
|
+
*/
|
|
6
|
+
declare const _default: import("@oxlint/plugins").Rule;
|
|
7
|
+
export default _default;
|
|
8
|
+
//# sourceMappingURL=no-state-spread.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-state-spread.d.ts","sourceRoot":"","sources":["../../src/rules/no-state-spread.ts"],"names":[],"mappings":"AAUA;;;;GAIG"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { docsUrl, stringValue } from "../internal/ast.js";
|
|
3
|
+
/** The `_tag` property of an object literal, if it has one written out. */
|
|
4
|
+
const isTag = (node) => node.type === "Property" && node.kind === "init" &&
|
|
5
|
+
((!node.computed && node.key.type === "Identifier" && node.key.name === "_tag") || stringValue(node.key) === "_tag");
|
|
6
|
+
/**
|
|
7
|
+
* A tagged value changes state by writing the new state's fields, never by
|
|
8
|
+
* re-tagging a spread of the old one: the old state's fields would survive at
|
|
9
|
+
* runtime while the type hides them.
|
|
10
|
+
*/
|
|
11
|
+
export default defineRule({
|
|
12
|
+
meta: {
|
|
13
|
+
type: "problem",
|
|
14
|
+
docs: {
|
|
15
|
+
description: "Build a new state's value from its own fields, not by spreading the previous state and changing `_tag`.",
|
|
16
|
+
url: docsUrl("no-state-spread")
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
spread: "This sets `_tag` over a spread of `{{spread}}`: every field of the previous state survives at runtime, though the type of the new state hides it, so it comes back when the value is saved, compared, or spread again. Move an entity state with `cell.transition({ from, to, data })`, which takes the new state's fields; queue a state machine's move with the new state's fields, `nextMachines.mode.set({ _tag: \"Talking\", speaker, page: 0 })`; anywhere else, write the new state's fields out: `{ _tag: \"air\", vy, coyote }`. Spread the previous value only to change fields of the same state."
|
|
20
|
+
},
|
|
21
|
+
schema: []
|
|
22
|
+
},
|
|
23
|
+
create(context) {
|
|
24
|
+
const text = (node) => context.sourceCode.getText(node);
|
|
25
|
+
return {
|
|
26
|
+
ObjectExpression(node) {
|
|
27
|
+
let spread;
|
|
28
|
+
for (const property of node.properties) {
|
|
29
|
+
if (property.type === "SpreadElement") {
|
|
30
|
+
spread = property;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (spread === undefined || !isTag(property))
|
|
34
|
+
continue;
|
|
35
|
+
// `{ ...value, _tag: value._tag }` keeps the state it spreads.
|
|
36
|
+
if (text(property.value) === `${text(spread.argument)}._tag`)
|
|
37
|
+
continue;
|
|
38
|
+
context.report({ node: property, messageId: "spread", data: { spread: text(spread.argument) } });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
//# sourceMappingURL=no-state-spread.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"no-state-spread.js","sourceRoot":"","sources":["../../src/rules/no-state-spread.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAA;AAEzD,2EAA2E;AAC3E,MAAM,KAAK,GAAG,CAAC,IAAiB,EAAiC,EAAE,CACjE,IAAI,CAAC,IAAI,KAAK,UAAU,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM;IAChD,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,YAAY,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,KAAK,MAAM,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,MAAM,CAAC,CAAA;AAEtH;;;;GAIG;AACH,eAAe,UAAU,CAAC;IACxB,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,IAAI,EAAE;YACJ,WAAW,EAAE,yGAAyG;YACtH,GAAG,EAAE,OAAO,CAAC,iBAAiB,CAAC;SAChC;QACD,QAAQ,EAAE;YACR,MAAM,EACJ,8kBAA8kB;SACjlB;QACD,MAAM,EAAE,EAAE;KACX;IACD,MAAM,CAAC,OAAO;QACZ,MAAM,IAAI,GAAG,CAAC,IAAiB,EAAU,EAAE,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QAC5E,OAAO;YACL,gBAAgB,CAAC,IAAI;gBACnB,IAAI,MAAwC,CAAA;gBAC5C,KAAK,MAAM,QAAQ,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;oBACvC,IAAI,QAAQ,CAAC,IAAI,KAAK,eAAe,EAAE,CAAC;wBACtC,MAAM,GAAG,QAAQ,CAAA;wBACjB,SAAQ;oBACV,CAAC;oBACD,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC;wBAAE,SAAQ;oBACtD,+DAA+D;oBAC/D,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO;wBAAE,SAAQ;oBACtE,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,SAAS,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,CAAA;gBAClG,CAAC;YACH,CAAC;SACF,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -4,7 +4,8 @@
|
|
|
4
4
|
* Types catch wrong calls and devtools catch wrong runs; these rules catch
|
|
5
5
|
* the source patterns neither can see: simulation code that reads the wall
|
|
6
6
|
* clock, `Math.random`, engine-dependent math, or the page, data that cannot
|
|
7
|
-
* be saved,
|
|
7
|
+
* be saved, state values that keep a previous state's fields, and runtime
|
|
8
|
+
* results that are dropped. Every report names the
|
|
8
9
|
* Ritmo API to use instead.
|
|
9
10
|
*
|
|
10
11
|
* Register the plugin in `.oxlintrc.json` with `"jsPlugins":
|
|
@@ -17,7 +18,8 @@
|
|
|
17
18
|
* `ritmo/use-random-for-randomness`, `ritmo/use-ops-for-math`,
|
|
18
19
|
* `ritmo/keep-simulation-headless`, `ritmo/no-infinity-in-simulation-data`.
|
|
19
20
|
* - Every file: `ritmo/handle-tick-result`,
|
|
20
|
-
* `ritmo/prefix-descriptor-and-system-names`, `ritmo/use-cell-value
|
|
21
|
+
* `ritmo/prefix-descriptor-and-system-names`, `ritmo/use-cell-value`,
|
|
22
|
+
* `ritmo/no-state-spread`.
|
|
21
23
|
*
|
|
22
24
|
* @module oxlint-plugin-ritmo
|
|
23
25
|
*/
|
|
@@ -26,6 +28,7 @@ import { definePlugin } from "@oxlint/plugins"
|
|
|
26
28
|
import handleTickResult from "./rules/handle-tick-result.ts"
|
|
27
29
|
import keepSimulationHeadless from "./rules/keep-simulation-headless.ts"
|
|
28
30
|
import noInfinityInSimulationData from "./rules/no-infinity-in-simulation-data.ts"
|
|
31
|
+
import noStateSpread from "./rules/no-state-spread.ts"
|
|
29
32
|
import prefixDescriptorAndSystemNames from "./rules/prefix-descriptor-and-system-names.ts"
|
|
30
33
|
import useCellValue from "./rules/use-cell-value.ts"
|
|
31
34
|
import useClockForTime from "./rules/use-clock-for-time.ts"
|
|
@@ -42,6 +45,7 @@ export default definePlugin({
|
|
|
42
45
|
"no-infinity-in-simulation-data": noInfinityInSimulationData,
|
|
43
46
|
"handle-tick-result": handleTickResult,
|
|
44
47
|
"prefix-descriptor-and-system-names": prefixDescriptorAndSystemNames,
|
|
45
|
-
"use-cell-value": useCellValue
|
|
48
|
+
"use-cell-value": useCellValue,
|
|
49
|
+
"no-state-spread": noStateSpread
|
|
46
50
|
}
|
|
47
51
|
})
|
|
@@ -27,7 +27,7 @@ export default defineRule({
|
|
|
27
27
|
},
|
|
28
28
|
messages: {
|
|
29
29
|
nonFinite:
|
|
30
|
-
"`{{key}}: {{value}}` cannot be saved: JSON turns it into null and
|
|
30
|
+
"`{{key}}: {{value}}` cannot be saved: JSON turns it into null and a number schema rejects it, so the save no longer loads (and devtools warns `non-finite-value`). Use `null` for \"never\" or \"no limit\": type the field `number | null` and give it a nullable number schema. For a value that only some entities have (a timer on a non-permanent pool), make that part optional: `timer: { remaining: number; duration: number } | null`."
|
|
31
31
|
},
|
|
32
32
|
schema: []
|
|
33
33
|
},
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins"
|
|
2
|
+
import type { ESTree } from "@oxlint/plugins"
|
|
3
|
+
|
|
4
|
+
import { docsUrl, stringValue } from "../internal/ast.ts"
|
|
5
|
+
|
|
6
|
+
/** The `_tag` property of an object literal, if it has one written out. */
|
|
7
|
+
const isTag = (node: ESTree.Node): node is ESTree.ObjectProperty =>
|
|
8
|
+
node.type === "Property" && node.kind === "init" &&
|
|
9
|
+
((!node.computed && node.key.type === "Identifier" && node.key.name === "_tag") || stringValue(node.key) === "_tag")
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A tagged value changes state by writing the new state's fields, never by
|
|
13
|
+
* re-tagging a spread of the old one: the old state's fields would survive at
|
|
14
|
+
* runtime while the type hides them.
|
|
15
|
+
*/
|
|
16
|
+
export default defineRule({
|
|
17
|
+
meta: {
|
|
18
|
+
type: "problem",
|
|
19
|
+
docs: {
|
|
20
|
+
description: "Build a new state's value from its own fields, not by spreading the previous state and changing `_tag`.",
|
|
21
|
+
url: docsUrl("no-state-spread")
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
spread:
|
|
25
|
+
"This sets `_tag` over a spread of `{{spread}}`: every field of the previous state survives at runtime, though the type of the new state hides it, so it comes back when the value is saved, compared, or spread again. Move an entity state with `cell.transition({ from, to, data })`, which takes the new state's fields; queue a state machine's move with the new state's fields, `nextMachines.mode.set({ _tag: \"Talking\", speaker, page: 0 })`; anywhere else, write the new state's fields out: `{ _tag: \"air\", vy, coyote }`. Spread the previous value only to change fields of the same state."
|
|
26
|
+
},
|
|
27
|
+
schema: []
|
|
28
|
+
},
|
|
29
|
+
create(context) {
|
|
30
|
+
const text = (node: ESTree.Node): string => context.sourceCode.getText(node)
|
|
31
|
+
return {
|
|
32
|
+
ObjectExpression(node) {
|
|
33
|
+
let spread: ESTree.SpreadElement | undefined
|
|
34
|
+
for (const property of node.properties) {
|
|
35
|
+
if (property.type === "SpreadElement") {
|
|
36
|
+
spread = property
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
if (spread === undefined || !isTag(property)) continue
|
|
40
|
+
// `{ ...value, _tag: value._tag }` keeps the state it spreads.
|
|
41
|
+
if (text(property.value) === `${text(spread.argument)}._tag`) continue
|
|
42
|
+
context.report({ node: property, messageId: "spread", data: { spread: text(spread.argument) } })
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
})
|