@typeonce/oxlint-plugin-ritmo 0.16.1 → 0.18.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 CHANGED
@@ -1,5 +1,36 @@
1
1
  # @typeonce/oxlint-plugin-ritmo
2
2
 
3
+ ## 0.18.0
4
+
5
+ ## 0.17.0
6
+
7
+ ### Minor Changes
8
+
9
+ - 39aeaf4: **Breaking: an optional query slot is the read cell, or `undefined` when the entity does not hold the component.** `Game.Query.optional(X)`, `Game.Query.optionalRelation(R)`, and `Game.Query.optionalRelated(R)` (and `select` slots of `RenderSync.system`) gave a slot that was always an object, so `if (data.hero !== undefined)` compiled and was always true, and there were two ways to read it (`present` then `get()`, or `value()`). The slot is now `ReadCell<T> | undefined`: reading it without a check does not compile (`'data.sprite' is possibly 'undefined'`). `present`, `value()`, and the `OptionalReadCell`, `PresentOptionalReadCell`, and `AbsentOptionalReadCell` types are removed. A component whose own value can be `undefined` stays unambiguous: an `undefined` slot means absent, and `get()` returning `undefined` means present.
10
+
11
+ ```diff
12
+ - burning: data.burning.present
13
+ + burning: data.burning !== undefined
14
+ - const parent = !data.parent.present ? undefined : data.parent.get()
15
+ + const parent = data.parent?.get()
16
+ - const stride = data.stride.value()
17
+ + const stride = data.stride?.get()
18
+ - if (data.sprite.present) draw(data.sprite.get())
19
+ + if (data.sprite !== undefined) draw(data.sprite.get())
20
+ - QueryTypes.OptionalReadCell<T>
21
+ + QueryTypes.ReadCell<T> | undefined
22
+ ```
23
+
24
+ To match only entities that hold a component ("is this the player?"), list it in the query's `with: [Player]` filter instead of an optional slot.
25
+
26
+ `@typeonce/oxlint-plugin-ritmo` removes `ritmo/use-cell-value`: with one way to read an optional slot there is nothing to steer between. Delete `"ritmo/use-cell-value"` from `.oxlintrc.json`: oxlint stops with `Rule 'use-cell-value' not found in plugin 'ritmo'` while the config names it.
27
+
28
+ ### Patch Changes
29
+
30
+ - 9346953: The `@typeonce/oxlint-plugin-ritmo` README lists built-in oxlint rules that pair well with Ritmo, each with the Ritmo mistake it catches: a `Result` checked with `if` instead of `.ok`, a switch over a closed set that misses a new member, an asset load or upload that is never awaited, `[object Object]` in HUD text, and oxlint's default `correctness` rules. The type-aware ones need `oxlint-tsgolint` and `oxlint --type-aware`.
31
+
32
+ The packages' sources pass these rules: unnecessary type assertions and default type arguments are gone, unused imports are removed, and switches over closed sets name every member. The published types are the same types, written without their default arguments.
33
+
3
34
  ## 0.16.1
4
35
 
5
36
  ### Patch Changes
package/README.md CHANGED
@@ -29,7 +29,6 @@ Register the plugin and turn the rules on in `.oxlintrc.json`. The simulation ru
29
29
  "rules": {
30
30
  "ritmo/handle-runtime-result": "error",
31
31
  "ritmo/prefix-descriptor-and-system-names": "error",
32
- "ritmo/use-cell-value": "error",
33
32
  "ritmo/no-state-spread": "error"
34
33
  },
35
34
  "overrides": [
@@ -50,7 +49,7 @@ Register the plugin and turn the rules on in `.oxlintrc.json`. The simulation ru
50
49
  }
51
50
  ```
52
51
 
53
- Rules are named `ritmo/<rule>`. `ritmo/use-ops-for-math` and `ritmo/use-cell-value` fix themselves with `oxlint --fix`.
52
+ Rules are named `ritmo/<rule>`. `ritmo/use-ops-for-math` fixes itself with `oxlint --fix`.
54
53
 
55
54
  A deliberate exception, such as a host-time module inside a simulation folder, takes a disable comment with the reason:
56
55
 
@@ -70,7 +69,6 @@ const started = performance.now()
70
69
  | [`no-brand-cast`](#ritmono-brand-cast) | simulation | `raw as UserId`, `1 as Random.State` | the schema or constructor that brands it |
71
70
  | [`handle-runtime-result`](#ritmohandle-runtime-result) | everywhere | a dropped or unchecked `runtime.tick(...)` / `restore(...)` / `rewind(...)` / `snapshot()` / `checksum()` result | check `.ok` |
72
71
  | [`prefix-descriptor-and-system-names`](#ritmoprefix-descriptor-and-system-names) | everywhere | `"Position"`, `Game.System("Move", ...)` | `"Game/Position"` |
73
- | [`use-cell-value`](#ritmouse-cell-value) | everywhere | `cell.present ? cell.get() : undefined` | `cell.value()` |
74
72
  | [`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 |
75
73
 
76
74
  ## Simulation rules
@@ -258,17 +256,6 @@ const Move = Game.System("Town/Move", ...)
258
256
 
259
257
  Checks the names given to `Descriptor.*`, `*.System`, `*.StateMachine`, `*.EntityScope`, `Clock.resource`, and the `name` of `Clock.system`, `InputCapture.system`, and `RenderSync.system` / `interpolate`.
260
258
 
261
- ### `ritmo/use-cell-value`
262
-
263
- An optional query cell has one way to read its value or `undefined`: `cell.value()`.
264
-
265
- ```ts
266
- const dash = target.data.dash.present ? target.data.dash.get() : undefined // reported, fixed to:
267
- const dash = target.data.dash.value()
268
- ```
269
-
270
- Check `cell.present` alone to branch without reading.
271
-
272
259
  ### `ritmo/no-state-spread`
273
260
 
274
261
  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.
@@ -284,4 +271,32 @@ const popped = { _tag: "air", vy: 2, coyote: 0 } // in a pure function
284
271
 
285
272
  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 }`).
286
273
 
274
+ ## Built-in rules that pair well
275
+
276
+ Some of oxlint's own rules catch Ritmo mistakes too. They are optional; turn on the ones the game wants.
277
+
278
+ | Rule | Catches in a Ritmo game |
279
+ |---|---|
280
+ | `typescript/no-unnecessary-condition` | a `Result` checked as a boolean instead of with `.ok` (`if (!runtime.restore(save))` never fires), an object compared to `undefined` |
281
+ | `typescript/switch-exhaustiveness-check` | a switch over a closed set (a role, a kind, a state's `_tag`) that misses a member added later |
282
+ | `typescript/no-floating-promises`, `typescript/no-misused-promises` | host code that never awaits an asset load or a replay upload, or passes an async callback where a plain one is called |
283
+ | `typescript/restrict-template-expressions`, `typescript/no-base-to-string` | `[object Object]` in HUD or log text |
284
+ | the default `correctness` category (with `no-unused-vars`) | dead code and likely bugs; oxlint runs it unless the config turns it off |
285
+
286
+ The `typescript/*` rules above read types: install `oxlint-tsgolint` and turn them on with `"options": { "typeAware": true }` in `.oxlintrc.json` (or `oxlint --type-aware`).
287
+
288
+ ```jsonc
289
+ {
290
+ "options": { "typeAware": true },
291
+ "rules": {
292
+ "typescript/no-unnecessary-condition": "error",
293
+ "typescript/switch-exhaustiveness-check": "error",
294
+ "typescript/no-floating-promises": "error",
295
+ "typescript/no-misused-promises": "error",
296
+ "typescript/restrict-template-expressions": "error",
297
+ "typescript/no-base-to-string": "error"
298
+ }
299
+ }
300
+ ```
301
+
287
302
  Source: https://github.com/typeonce-dev/ritmo
package/dist/index.js CHANGED
@@ -19,8 +19,7 @@
19
19
  * `ritmo/keep-simulation-headless`, `ritmo/no-infinity-in-simulation-data`,
20
20
  * `ritmo/no-float-timers`, `ritmo/no-brand-cast`.
21
21
  * - Every file: `ritmo/handle-runtime-result`,
22
- * `ritmo/prefix-descriptor-and-system-names`, `ritmo/use-cell-value`,
23
- * `ritmo/no-state-spread`.
22
+ * `ritmo/prefix-descriptor-and-system-names`, `ritmo/no-state-spread`.
24
23
  *
25
24
  * @module oxlint-plugin-ritmo
26
25
  */
@@ -32,7 +31,6 @@ import noFloatTimers from "./rules/no-float-timers.js";
32
31
  import noInfinityInSimulationData from "./rules/no-infinity-in-simulation-data.js";
33
32
  import noStateSpread from "./rules/no-state-spread.js";
34
33
  import prefixDescriptorAndSystemNames from "./rules/prefix-descriptor-and-system-names.js";
35
- import useCellValue from "./rules/use-cell-value.js";
36
34
  import useClockForTime from "./rules/use-clock-for-time.js";
37
35
  import useOpsForMath from "./rules/use-ops-for-math.js";
38
36
  import useRandomForRandomness from "./rules/use-random-for-randomness.js";
@@ -48,7 +46,6 @@ export default definePlugin({
48
46
  "no-brand-cast": noBrandCast,
49
47
  "handle-runtime-result": handleRuntimeResult,
50
48
  "prefix-descriptor-and-system-names": prefixDescriptorAndSystemNames,
51
- "use-cell-value": useCellValue,
52
49
  "no-state-spread": noStateSpread
53
50
  }
54
51
  });
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,OAAO,mBAAmB,MAAM,kCAAkC,CAAA;AAClE,OAAO,sBAAsB,MAAM,qCAAqC,CAAA;AACxE,OAAO,WAAW,MAAM,0BAA0B,CAAA;AAClD,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,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,iBAAiB,EAAE,aAAa;QAChC,eAAe,EAAE,WAAW;QAC5B,uBAAuB,EAAE,mBAAmB;QAC5C,oCAAoC,EAAE,8BAA8B;QACpE,gBAAgB,EAAE,YAAY;QAC9B,iBAAiB,EAAE,aAAa;KACjC;CACF,CAAC,CAAA"}
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,mBAAmB,MAAM,kCAAkC,CAAA;AAClE,OAAO,sBAAsB,MAAM,qCAAqC,CAAA;AACxE,OAAO,WAAW,MAAM,0BAA0B,CAAA;AAClD,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,0BAA0B,MAAM,2CAA2C,CAAA;AAClF,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,8BAA8B,MAAM,+CAA+C,CAAA;AAC1F,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,iBAAiB,EAAE,aAAa;QAChC,eAAe,EAAE,WAAW;QAC5B,uBAAuB,EAAE,mBAAmB;QAC5C,oCAAoC,EAAE,8BAA8B;QACpE,iBAAiB,EAAE,aAAa;KACjC;CACF,CAAC,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@typeonce/oxlint-plugin-ritmo",
3
- "version": "0.16.1",
3
+ "version": "0.18.0",
4
4
  "description": "Oxlint rules for Ritmo games: deterministic, headless simulation code and checked runtime results",
5
5
  "author": "Sandro Maglione",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -19,8 +19,7 @@
19
19
  * `ritmo/keep-simulation-headless`, `ritmo/no-infinity-in-simulation-data`,
20
20
  * `ritmo/no-float-timers`, `ritmo/no-brand-cast`.
21
21
  * - Every file: `ritmo/handle-runtime-result`,
22
- * `ritmo/prefix-descriptor-and-system-names`, `ritmo/use-cell-value`,
23
- * `ritmo/no-state-spread`.
22
+ * `ritmo/prefix-descriptor-and-system-names`, `ritmo/no-state-spread`.
24
23
  *
25
24
  * @module oxlint-plugin-ritmo
26
25
  */
@@ -33,7 +32,6 @@ import noFloatTimers from "./rules/no-float-timers.ts"
33
32
  import noInfinityInSimulationData from "./rules/no-infinity-in-simulation-data.ts"
34
33
  import noStateSpread from "./rules/no-state-spread.ts"
35
34
  import prefixDescriptorAndSystemNames from "./rules/prefix-descriptor-and-system-names.ts"
36
- import useCellValue from "./rules/use-cell-value.ts"
37
35
  import useClockForTime from "./rules/use-clock-for-time.ts"
38
36
  import useOpsForMath from "./rules/use-ops-for-math.ts"
39
37
  import useRandomForRandomness from "./rules/use-random-for-randomness.ts"
@@ -50,7 +48,6 @@ export default definePlugin({
50
48
  "no-brand-cast": noBrandCast,
51
49
  "handle-runtime-result": handleRuntimeResult,
52
50
  "prefix-descriptor-and-system-names": prefixDescriptorAndSystemNames,
53
- "use-cell-value": useCellValue,
54
51
  "no-state-spread": noStateSpread
55
52
  }
56
53
  })
@@ -1,7 +0,0 @@
1
- /**
2
- * An optional cell is read with `cell.value()`, the one way to get its value
3
- * or `undefined`.
4
- */
5
- declare const _default: import("@oxlint/plugins").Rule;
6
- export default _default;
7
- //# sourceMappingURL=use-cell-value.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-cell-value.d.ts","sourceRoot":"","sources":["../../src/rules/use-cell-value.ts"],"names":[],"mappings":"AAKA;;;GAGG"}
@@ -1,49 +0,0 @@
1
- import { defineRule } from "@oxlint/plugins";
2
- import { calledMethod, docsUrl, isGlobalReference, propertyName } from "../internal/ast.js";
3
- /**
4
- * An optional cell is read with `cell.value()`, the one way to get its value
5
- * or `undefined`.
6
- */
7
- export default defineRule({
8
- meta: {
9
- type: "suggestion",
10
- docs: {
11
- description: "Read an optional query cell with `cell.value()` instead of `cell.present ? cell.get() : undefined`.",
12
- url: docsUrl("use-cell-value")
13
- },
14
- fixable: "code",
15
- messages: {
16
- value: "Read this optional cell with `{{cell}}.value()`: it returns the component's value, or `undefined` when the entity does not hold the component. Check `{{cell}}.present` only to branch without reading."
17
- },
18
- schema: []
19
- },
20
- create(context) {
21
- const text = (node) => context.sourceCode.getText(node);
22
- /** The cell `node` reads `.present` of, if it does. */
23
- const presentOf = (node) => node.type === "MemberExpression" && propertyName(node) === "present" ? node.object : undefined;
24
- const isGetOf = (node, cell) => node.type === "CallExpression" && node.arguments.length === 0 && calledMethod(node) === "get" &&
25
- node.callee.type === "MemberExpression" && text(node.callee.object) === text(cell);
26
- const isUndefined = (node) => (node.type === "Identifier" && node.name === "undefined" && isGlobalReference(context, node)) ||
27
- (node.type === "UnaryExpression" && node.operator === "void");
28
- return {
29
- ConditionalExpression(node) {
30
- const test = node.test;
31
- // cell.present ? cell.get() : undefined
32
- let cell = presentOf(test);
33
- let read = node.consequent;
34
- let absent = node.alternate;
35
- // !cell.present ? undefined : cell.get()
36
- if (cell === undefined && test.type === "UnaryExpression" && test.operator === "!") {
37
- cell = presentOf(test.argument);
38
- read = node.alternate;
39
- absent = node.consequent;
40
- }
41
- if (cell === undefined || !isGetOf(read, cell) || !isUndefined(absent))
42
- return;
43
- const name = text(cell);
44
- context.report({ node, messageId: "value", data: { cell: name }, fix: (fixer) => fixer.replaceText(node, `${name}.value()`) });
45
- }
46
- };
47
- }
48
- });
49
- //# sourceMappingURL=use-cell-value.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-cell-value.js","sourceRoot":"","sources":["../../src/rules/use-cell-value.ts"],"names":[],"mappings":"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"}
@@ -1,54 +0,0 @@
1
- import { defineRule } from "@oxlint/plugins"
2
- import type { ESTree } from "@oxlint/plugins"
3
-
4
- import { calledMethod, docsUrl, isGlobalReference, propertyName } from "../internal/ast.ts"
5
-
6
- /**
7
- * An optional cell is read with `cell.value()`, the one way to get its value
8
- * or `undefined`.
9
- */
10
- export default defineRule({
11
- meta: {
12
- type: "suggestion",
13
- docs: {
14
- description: "Read an optional query cell with `cell.value()` instead of `cell.present ? cell.get() : undefined`.",
15
- url: docsUrl("use-cell-value")
16
- },
17
- fixable: "code",
18
- messages: {
19
- value:
20
- "Read this optional cell with `{{cell}}.value()`: it returns the component's value, or `undefined` when the entity does not hold the component. Check `{{cell}}.present` only to branch without reading."
21
- },
22
- schema: []
23
- },
24
- create(context) {
25
- const text = (node: ESTree.Node): string => context.sourceCode.getText(node)
26
- /** The cell `node` reads `.present` of, if it does. */
27
- const presentOf = (node: ESTree.Node): ESTree.Node | undefined =>
28
- node.type === "MemberExpression" && propertyName(node) === "present" ? node.object : undefined
29
- const isGetOf = (node: ESTree.Node, cell: ESTree.Node): boolean =>
30
- node.type === "CallExpression" && node.arguments.length === 0 && calledMethod(node) === "get" &&
31
- node.callee.type === "MemberExpression" && text(node.callee.object) === text(cell)
32
- const isUndefined = (node: ESTree.Node): boolean =>
33
- (node.type === "Identifier" && node.name === "undefined" && isGlobalReference(context, node)) ||
34
- (node.type === "UnaryExpression" && node.operator === "void")
35
- return {
36
- ConditionalExpression(node) {
37
- const test = node.test
38
- // cell.present ? cell.get() : undefined
39
- let cell = presentOf(test)
40
- let read = node.consequent
41
- let absent = node.alternate
42
- // !cell.present ? undefined : cell.get()
43
- if (cell === undefined && test.type === "UnaryExpression" && test.operator === "!") {
44
- cell = presentOf(test.argument)
45
- read = node.alternate
46
- absent = node.consequent
47
- }
48
- if (cell === undefined || !isGetOf(read, cell) || !isUndefined(absent)) return
49
- const name = text(cell)
50
- context.report({ node, messageId: "value", data: { cell: name }, fix: (fixer) => fixer.replaceText(node, `${name}.value()`) })
51
- }
52
- }
53
- }
54
- })