@typeonce/oxlint-plugin-ritmo 0.16.0 → 0.17.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 +35 -0
- package/README.md +30 -15
- package/dist/index.js +1 -4
- package/dist/index.js.map +1 -1
- package/dist/rules/handle-runtime-result.d.ts +3 -1
- package/dist/rules/handle-runtime-result.d.ts.map +1 -1
- package/dist/rules/handle-runtime-result.js +24 -1
- package/dist/rules/handle-runtime-result.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -4
- package/src/rules/handle-runtime-result.ts +24 -1
- package/dist/rules/use-cell-value.d.ts +0 -7
- package/dist/rules/use-cell-value.d.ts.map +0 -1
- package/dist/rules/use-cell-value.js +0 -49
- package/dist/rules/use-cell-value.js.map +0 -1
- package/src/rules/use-cell-value.ts +0 -54
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
# @typeonce/oxlint-plugin-ritmo
|
|
2
2
|
|
|
3
|
+
## 0.17.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 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.
|
|
8
|
+
|
|
9
|
+
```diff
|
|
10
|
+
- burning: data.burning.present
|
|
11
|
+
+ burning: data.burning !== undefined
|
|
12
|
+
- const parent = !data.parent.present ? undefined : data.parent.get()
|
|
13
|
+
+ const parent = data.parent?.get()
|
|
14
|
+
- const stride = data.stride.value()
|
|
15
|
+
+ const stride = data.stride?.get()
|
|
16
|
+
- if (data.sprite.present) draw(data.sprite.get())
|
|
17
|
+
+ if (data.sprite !== undefined) draw(data.sprite.get())
|
|
18
|
+
- QueryTypes.OptionalReadCell<T>
|
|
19
|
+
+ QueryTypes.ReadCell<T> | undefined
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
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.
|
|
23
|
+
|
|
24
|
+
`@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.
|
|
25
|
+
|
|
26
|
+
### Patch Changes
|
|
27
|
+
|
|
28
|
+
- 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`.
|
|
29
|
+
|
|
30
|
+
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.
|
|
31
|
+
|
|
32
|
+
## 0.16.1
|
|
33
|
+
|
|
34
|
+
### Patch Changes
|
|
35
|
+
|
|
36
|
+
- 8918c31: `ritmo/handle-runtime-result` no longer reports a `tick`, `tryTick`, `restore`, or `rewind` result passed to a function: that function is a helper that checks it (`expectOk(runtime.tick(update))`, `orCrash(runtime.tick(setup), "Setup")`). It still reports one passed to `JSON.stringify`, `String`, or `console.*`, and any unchecked `snapshot()` or `checksum()` result, which is data on its way to a save or a log. Spirit Call's 180 reports on 0.16.0 were all checking helpers.
|
|
37
|
+
|
|
3
38
|
## 0.16.0
|
|
4
39
|
|
|
5
40
|
### Minor 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`
|
|
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
|
|
@@ -241,7 +239,7 @@ if (saved.ok) persist(saved.value)
|
|
|
241
239
|
void runtime.tick(render) // ignored on purpose
|
|
242
240
|
```
|
|
243
241
|
|
|
244
|
-
Catches these calls on a receiver named like a runtime (`runtime`, `game.runtime`, `worldRuntime`) used as a statement, or passed on unchecked: as a call argument, inside a template string, an object or array literal, a spread, or an operand, also through `??`, `? :`, and `as`. A variable, a field, a `return`, `.ok`, and `void` keep the `Result` for the code to check. A helper that takes
|
|
242
|
+
Catches these calls on a receiver named like a runtime (`runtime`, `game.runtime`, `worldRuntime`) used as a statement, or passed on unchecked: as a call argument, inside a template string, an object or array literal, a spread, or an operand, also through `??`, `? :`, and `as`. A variable, a field, a `return`, `.ok`, and `void` keep the `Result` for the code to check. A `tick`, `tryTick`, `restore`, or `rewind` result passed to a function is left to that function (a test's `expectOk(runtime.tick(update))`), unless it only prints or encodes (`JSON.stringify`, `String`, `console.*`). A `snapshot()` or `checksum()` result is data on its way somewhere, so passing it to any function unchecked is reported; a helper that takes that `Result` itself gets it once it is named: `const checksum = runtime.checksum(); helper(checksum)`. Game wrappers with other names (`town.restore(...)`) are not reported. Option: `{ "runtimePattern": "^(runtime|world)$" }` (a case-insensitive regular expression for the receiver's name; default `runtime$`).
|
|
245
243
|
|
|
246
244
|
### `ritmo/prefix-descriptor-and-system-names`
|
|
247
245
|
|
|
@@ -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/
|
|
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
|
|
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"}
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* The `Result` of `runtime.tick(...)`, `tryTick(...)`, `restore(...)`,
|
|
3
3
|
* `rewind(...)`, `snapshot()`, and `checksum()` is checked: dropping it, or
|
|
4
|
-
* passing it on where any value is accepted, hides the failure.
|
|
4
|
+
* passing it on where any value is accepted, hides the failure. A `tick`,
|
|
5
|
+
* `restore`, or `rewind` result passed to a function is left to that
|
|
6
|
+
* function (a test's `expectOk`), unless it only prints or encodes.
|
|
5
7
|
*/
|
|
6
8
|
declare const _default: import("@oxlint/plugins").Rule;
|
|
7
9
|
export default _default;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle-runtime-result.d.ts","sourceRoot":"","sources":["../../src/rules/handle-runtime-result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"handle-runtime-result.d.ts","sourceRoot":"","sources":["../../src/rules/handle-runtime-result.ts"],"names":[],"mappings":"AAgDA;;;;;;GAMG"}
|
|
@@ -21,10 +21,26 @@ const passesThrough = new Set(["ConditionalExpression", "LogicalExpression", "Pa
|
|
|
21
21
|
* keep it as a `Result` the code still has to look at.
|
|
22
22
|
*/
|
|
23
23
|
const unchecked = new Set(["CallExpression", "NewExpression", "TemplateLiteral", "Property", "ArrayExpression", "SpreadElement", "BinaryExpression", "JSXExpressionContainer"]);
|
|
24
|
+
/**
|
|
25
|
+
* Functions that take any value and do nothing with a `Result` but print or
|
|
26
|
+
* encode it. A `tick`, `restore`, or `rewind` result handed to any other
|
|
27
|
+
* function goes to a helper that checks it (`expectOk(runtime.tick(update))`).
|
|
28
|
+
*/
|
|
29
|
+
const sinks = new Set(["JSON.stringify", "String", "console.log", "console.info", "console.warn", "console.error", "console.debug"]);
|
|
30
|
+
const calleeName = (node) => {
|
|
31
|
+
if (node.type === "Identifier")
|
|
32
|
+
return node.name;
|
|
33
|
+
if (node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.property.type === "Identifier") {
|
|
34
|
+
return `${node.object.name}.${node.property.name}`;
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
};
|
|
24
38
|
/**
|
|
25
39
|
* The `Result` of `runtime.tick(...)`, `tryTick(...)`, `restore(...)`,
|
|
26
40
|
* `rewind(...)`, `snapshot()`, and `checksum()` is checked: dropping it, or
|
|
27
|
-
* passing it on where any value is accepted, hides the failure.
|
|
41
|
+
* passing it on where any value is accepted, hides the failure. A `tick`,
|
|
42
|
+
* `restore`, or `rewind` result passed to a function is left to that
|
|
43
|
+
* function (a test's `expectOk`), unless it only prints or encodes.
|
|
28
44
|
*/
|
|
29
45
|
export default defineRule({
|
|
30
46
|
meta: {
|
|
@@ -94,6 +110,13 @@ export default defineRule({
|
|
|
94
110
|
// A property key is not a use of the value.
|
|
95
111
|
if (parent.type === "Property" && parent.value !== node)
|
|
96
112
|
return;
|
|
113
|
+
// A tick, restore, or rewind handed to a function goes to a helper that checks it, unless the function only prints or encodes.
|
|
114
|
+
// snapshot() and checksum() are data on their way somewhere, so a function that takes them unchecked is reported.
|
|
115
|
+
if ((parent.type === "CallExpression" || parent.type === "NewExpression") && takes === "arguments") {
|
|
116
|
+
const name = calleeName(parent.callee);
|
|
117
|
+
if (name === undefined || !sinks.has(name))
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
97
120
|
context.report({ node: call, messageId: "unchecked", data });
|
|
98
121
|
}
|
|
99
122
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handle-runtime-result.js","sourceRoot":"","sources":["../../src/rules/handle-runtime-result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAMxE,mFAAmF;AACnF,MAAM,OAAO,GAAG,IAAI,GAAG,CAA+B;IACpD,CAAC,MAAM,EAAE,WAAW,CAAC;IACrB,CAAC,SAAS,EAAE,WAAW,CAAC;IACxB,CAAC,SAAS,EAAE,WAA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I,KAAK,KAAK,WAAW,EAAE,CAAC;oBACnG,MAAM,IAAI,GAAG,UAAU,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;oBACtC,IAAI,IAAI,KAAK,SAAS,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC;wBAAE,OAAM;gBACpD,CAAC;gBACD,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,IAAI,EAAE,SAAS,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,CAAA;YAC9D,CAAC;SACF,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
package/package.json
CHANGED
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/
|
|
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
|
})
|
|
@@ -31,10 +31,27 @@ const passesThrough = new Set(["ConditionalExpression", "LogicalExpression", "Pa
|
|
|
31
31
|
*/
|
|
32
32
|
const unchecked = new Set(["CallExpression", "NewExpression", "TemplateLiteral", "Property", "ArrayExpression", "SpreadElement", "BinaryExpression", "JSXExpressionContainer"])
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Functions that take any value and do nothing with a `Result` but print or
|
|
36
|
+
* encode it. A `tick`, `restore`, or `rewind` result handed to any other
|
|
37
|
+
* function goes to a helper that checks it (`expectOk(runtime.tick(update))`).
|
|
38
|
+
*/
|
|
39
|
+
const sinks = new Set(["JSON.stringify", "String", "console.log", "console.info", "console.warn", "console.error", "console.debug"])
|
|
40
|
+
|
|
41
|
+
const calleeName = (node: ESTree.Node): string | undefined => {
|
|
42
|
+
if (node.type === "Identifier") return node.name
|
|
43
|
+
if (node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.property.type === "Identifier") {
|
|
44
|
+
return `${node.object.name}.${node.property.name}`
|
|
45
|
+
}
|
|
46
|
+
return undefined
|
|
47
|
+
}
|
|
48
|
+
|
|
34
49
|
/**
|
|
35
50
|
* The `Result` of `runtime.tick(...)`, `tryTick(...)`, `restore(...)`,
|
|
36
51
|
* `rewind(...)`, `snapshot()`, and `checksum()` is checked: dropping it, or
|
|
37
|
-
* passing it on where any value is accepted, hides the failure.
|
|
52
|
+
* passing it on where any value is accepted, hides the failure. A `tick`,
|
|
53
|
+
* `restore`, or `rewind` result passed to a function is left to that
|
|
54
|
+
* function (a test's `expectOk`), unless it only prints or encodes.
|
|
38
55
|
*/
|
|
39
56
|
export default defineRule({
|
|
40
57
|
meta: {
|
|
@@ -101,6 +118,12 @@ export default defineRule({
|
|
|
101
118
|
if (parent.type === "CallExpression" && parent.callee === node) return
|
|
102
119
|
// A property key is not a use of the value.
|
|
103
120
|
if (parent.type === "Property" && parent.value !== node) return
|
|
121
|
+
// A tick, restore, or rewind handed to a function goes to a helper that checks it, unless the function only prints or encodes.
|
|
122
|
+
// snapshot() and checksum() are data on their way somewhere, so a function that takes them unchecked is reported.
|
|
123
|
+
if ((parent.type === "CallExpression" || parent.type === "NewExpression") && takes === "arguments") {
|
|
124
|
+
const name = calleeName(parent.callee)
|
|
125
|
+
if (name === undefined || !sinks.has(name)) return
|
|
126
|
+
}
|
|
104
127
|
context.report({ node: call, messageId: "unchecked", data })
|
|
105
128
|
}
|
|
106
129
|
}
|
|
@@ -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":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAA;AAG5C,OAAO,EAAE,YAAY,EAAE,OAAO,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAA;AAE3F;;;GAGG;AACH,eAAe,U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C,IAAI;gBACxB,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAA;gBACtB,wCAAwC;gBACxC,IAAI,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,CAAA;gBAC1B,IAAI,IAAI,GAAG,IAAI,CAAC,UAAU,CAAA;gBAC1B,IAAI,MAAM,GAAG,IAAI,CAAC,SAAS,CAAA;gBAC3B,yCAAyC;gBACzC,IAAI,IAAI,KAAK,SAAS,IAAI,IAAI,CAAC,IAAI,KAAK,iBAAiB,IAAI,IAAI,CAAC,QAAQ,KAAK,GAAG,EAAE,CAAC;oBACnF,IAAI,GAAG,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;oBAC/B,IAAI,GAAG,IAAI,CAAC,SAAS,CAAA;oBACrB,MAAM,GAAG,IAAI,CAAC,UAAU,CAAA;gBAC1B,CAAC;gBACD,IAAI,IAAI,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC;oBAAE,OAAM;gBAC9E,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,CAAA;gBACvB,OAAO,CAAC,MAAM,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,GAAG,IAAI,UAAU,CAAC,EAAE,CAAC,CAAA;YAChI,CAAC;SACF,CAAA;IACH,CAAC;CACF,CAAC,CAAA"}
|
|
@@ -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
|
-
})
|