@typeonce/oxlint-plugin-ritmo 0.24.0 → 0.26.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,28 @@
1
1
  # @typeonce/oxlint-plugin-ritmo
2
2
 
3
+ ## 0.26.0
4
+
5
+ No changes in this release.
6
+
7
+ ## 0.25.0
8
+
9
+ ### Minor Changes
10
+
11
+ - 1fb5453: New simulation rule `ritmo/no-locale-in-simulation`: it reports simulation code that asks how a language orders, cases, or writes a value. The answer comes from the machine's language settings and from the language data its engine ships, so it differs between machines and a replay or an online session diverges: `names.sort((a, b) => a.localeCompare(b))` gives "Ärla, Zara" on an English machine and "Zara, Ärla" on a Swedish one, `"FIRE".toLocaleLowerCase()` is "fıre" on a Turkish one, and `coins.toLocaleString()` is "1,234.5" or "1.234,5".
12
+
13
+ It reports every member of `Intl` (found like the other globals: through `globalThis`, an alias, or destructuring), and `localeCompare`, `toLocaleLowerCase`, `toLocaleUpperCase`, `toLocaleString`, `toLocaleDateString`, and `toLocaleTimeString` on any value, also when the call names its language. `toLowerCase()`, `toUpperCase()`, `sort()` with no comparator, and `<` between strings are the same everywhere and are not reported. Each report names the fix: order by a number the world holds or by the strings themselves, use `toLowerCase()` / `toUpperCase()`, and keep the value in the world for view code to write.
14
+
15
+ The rule is off until the game lists it. Add it to the simulation override in `.oxlintrc.json`; view, UI, and host files are not in that override and keep using `Intl`:
16
+
17
+ ```diff
18
+ "rules": {
19
+ "ritmo/no-float-timers": "error",
20
+ - "ritmo/no-brand-cast": "error"
21
+ + "ritmo/no-brand-cast": "error",
22
+ + "ritmo/no-locale-in-simulation": "error"
23
+ }
24
+ ```
25
+
3
26
  ## 0.24.0
4
27
 
5
28
  ### Minor Changes
package/README.md CHANGED
@@ -5,6 +5,7 @@
5
5
  Ritmo's types reject wrong calls, and its devtools lints reject wrong runs. These rules catch the source patterns neither can see:
6
6
 
7
7
  - simulation code that reads the wall clock, `Math.random`, engine-dependent math, or the page, or plays sound;
8
+ - simulation code that asks how a language orders, cases, or writes a value;
8
9
  - timers counted in floats;
9
10
  - data that cannot be saved;
10
11
  - casts that claim a brand's check without running it;
@@ -46,7 +47,8 @@ Register the plugin and turn the rules on in `.oxlintrc.json`. The simulation ru
46
47
  "ritmo/keep-simulation-headless": "error",
47
48
  "ritmo/no-infinity-in-simulation-data": "error",
48
49
  "ritmo/no-float-timers": "error",
49
- "ritmo/no-brand-cast": "error"
50
+ "ritmo/no-brand-cast": "error",
51
+ "ritmo/no-locale-in-simulation": "error"
50
52
  }
51
53
  }
52
54
  ]
@@ -71,6 +73,7 @@ const started = performance.now()
71
73
  | [`no-infinity-in-simulation-data`](#ritmono-infinity-in-simulation-data) | simulation | `Infinity` / `NaN` in object and array literals | `null` |
72
74
  | [`no-float-timers`](#ritmono-float-timers) | simulation | `timer -= dt`, `cooldown = cooldown - time.dt` | `Clock.deadline` / `Clock.reached` |
73
75
  | [`no-brand-cast`](#ritmono-brand-cast) | simulation | `raw as UserId`, `1 as Random.State` | the schema or constructor that brands it |
76
+ | [`no-locale-in-simulation`](#ritmono-locale-in-simulation) | simulation | `a.localeCompare(b)`, `label.toLocaleLowerCase()`, `coins.toLocaleString()`, `Intl.*` | a number the world holds or the strings themselves to order, `toLowerCase()`, and words chosen in view code |
74
77
  | [`handle-runtime-result`](#ritmohandle-runtime-result) | everywhere | a dropped or unchecked `runtime.tick(...)` / `restore(...)` / `rewind(...)` / `snapshot()` / `checksum()` result | check `.ok` |
75
78
  | [`prefix-descriptor-and-system-names`](#ritmoprefix-descriptor-and-system-names) | everywhere | `"Position"`, `Game.System("Move", ...)` | `"Game/Position"` |
76
79
  | [`no-state-spread`](#ritmono-state-spread) | everywhere | `{ ...ground, _tag: "air", vy }` | `data.state.transition(entity, { from, to, data })`, `nextMachines.x.transition({ from, to, data })`, or the new state's fields written out |
@@ -79,7 +82,7 @@ const started = performance.now()
79
82
 
80
83
  ## Simulation rules
81
84
 
82
- The four rules that read globals (`use-clock-for-time`, `use-random-for-randomness`, `use-ops-for-math`, `keep-simulation-headless`) find a global however the file reaches it: by name, as a member of `globalThis`, `self`, or `window` (`globalThis.Date.now()`, `self.Math.sin(x)`), through a `const` that holds it (`const M = Math; M.random()`), or destructured (`const { now } = Date`, `const { Math: { random } } = globalThis`), reported once where the global is read. A local, parameter, or import of the same name, a field of another object (`config.Date.now()`), and a type (`let at: Date`) are not the global. A value passed through a function or another module is not followed.
85
+ The five rules that read globals (`use-clock-for-time`, `use-random-for-randomness`, `use-ops-for-math`, `keep-simulation-headless`, `no-locale-in-simulation`) find a global however the file reaches it: by name, as a member of `globalThis`, `self`, or `window` (`globalThis.Date.now()`, `self.Math.sin(x)`), through a `const` that holds it (`const M = Math; M.random()`), or destructured (`const { now } = Date`, `const { Math: { random } } = globalThis`), reported once where the global is read. A local, parameter, or import of the same name, a field of another object (`config.Date.now()`), and a type (`let at: Date`) are not the global. A value passed through a function or another module is not followed.
83
86
 
84
87
  ### `ritmo/use-clock-for-time`
85
88
 
@@ -233,6 +236,30 @@ Oxlint has no type information, so the rule reads the brand from the code: a cas
233
236
 
234
237
  Make the value through its check: decode at the boundary with the game's schema, or with Ritmo's constructor or guard (`Random.seed` / `Random.result`, `Clock.deadline` / `Clock.isDeadline`, `Game.Entity.handle` / `Entity.isHandle`). `as const`, `satisfies`, and casts to types without a brand are not reported, nor are brands imported from another file of the game (the rule cannot see them).
235
238
 
239
+ ### `ritmo/no-locale-in-simulation`
240
+
241
+ How a language orders, cases, and writes a value comes from two things a replay does not share: the machine's language settings, and the language data its engine ships, which differs between browsers and Node versions. The simulation holds ids and numbers, and view code puts them into words.
242
+
243
+ ```ts
244
+ const order = heroes.sort((a, b) => a.name.localeCompare(b.name)) // reported: "Ärla, Zara" on an English machine, "Zara, Ärla" on a Swedish one
245
+ const kind = kinds[label.toLocaleLowerCase()] // reported: "FIRE" is "fıre" on a Turkish machine, and the lookup misses
246
+ log.lines.push(`Earned ${coins.toLocaleString()} coins`) // reported: "1,234.5" or "1.234,5", saved as written
247
+
248
+ // instead
249
+ const order = heroes.sort((a, b) => a.rank - b.rank) // a number the world holds
250
+ const byName = heroes.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0)) // or the strings themselves
251
+ const kind = kinds[label.toLowerCase()]
252
+ log.lines.push({ _tag: "earned", coins }) // the value; view code words it
253
+ ```
254
+
255
+ Catches every member of `Intl` (`Intl.Collator`, `Intl.NumberFormat`, `Intl.PluralRules`, `Intl.ListFormat`, `Intl.DateTimeFormat`, ...), and `localeCompare`, `toLocaleLowerCase`, `toLocaleUpperCase`, `toLocaleString`, `toLocaleDateString`, and `toLocaleTimeString` on any value. A call that names its language (`a.localeCompare(b, "en")`, `new Intl.NumberFormat("en-US")`) is reported too: the name settles which language, not which version of its data the engine ships.
256
+
257
+ `toLowerCase()`, `toUpperCase()`, `sort()` with no comparator, `<` between strings, `toFixed()`, and `String(n)` answer the same on every engine and are not reported.
258
+
259
+ Oxlint has no type information, so the methods are found by name on any value: a method of the game's own with one of these names is reported where it is called. A method defined (`class Score { toLocaleString() { ... } }`, `score.toLocaleString = ...`), a key of that name in an object, and a type (`let format: Intl.NumberFormat`) are not reported. Neither is a method taken off a value by destructuring and called later (`const { localeCompare } = name`). `navigator.language` is the page, which `keep-simulation-headless` reports.
260
+
261
+ View, UI, and host files are outside the simulation override, so these calls are fine there. For a game in several languages, the `ritmo` skill's [`playbooks/languages.md`](https://github.com/typeonce-dev/ritmo/blob/main/packages/core/skills/ritmo/playbooks/languages.md) has the setup.
262
+
236
263
  ## Rules for every file
237
264
 
238
265
  ### `ritmo/handle-runtime-result`
package/dist/index.js CHANGED
@@ -3,11 +3,12 @@
3
3
  *
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
- * clock, `Math.random`, engine-dependent math, or the page, or plays sound,
7
- * timers counted in floats, data that cannot be saved, casts that skip a
8
- * brand's check, state values that keep a previous state's fields, query
9
- * entities and data kept past their run, runtime results that are
10
- * dropped, and host key listeners that take keys from the page's controls.
6
+ * clock, `Math.random`, engine-dependent math, or the page, plays sound, or
7
+ * asks how a language orders, cases, or writes a value, timers counted in
8
+ * floats, data that cannot be saved, casts that skip a brand's check, state
9
+ * values that keep a previous state's fields, query entities and data kept
10
+ * past their run, runtime results that are dropped, and host key listeners
11
+ * that take keys from the page's controls.
11
12
  * Every report names the Ritmo API to use instead.
12
13
  *
13
14
  * Register the plugin in `.oxlintrc.json` with `"jsPlugins":
@@ -19,7 +20,8 @@
19
20
  * - Simulation files: `ritmo/use-clock-for-time`,
20
21
  * `ritmo/use-random-for-randomness`, `ritmo/use-ops-for-math`,
21
22
  * `ritmo/keep-simulation-headless`, `ritmo/no-infinity-in-simulation-data`,
22
- * `ritmo/no-float-timers`, `ritmo/no-brand-cast`.
23
+ * `ritmo/no-float-timers`, `ritmo/no-brand-cast`,
24
+ * `ritmo/no-locale-in-simulation`.
23
25
  * - Every file: `ritmo/handle-runtime-result`,
24
26
  * `ritmo/prefix-descriptor-and-system-names`, `ritmo/no-state-spread`,
25
27
  * `ritmo/no-kept-query-values`, `ritmo/use-keyboard-for-game-keys`.
@@ -33,6 +35,7 @@ import noBrandCast from "./rules/no-brand-cast.js";
33
35
  import noFloatTimers from "./rules/no-float-timers.js";
34
36
  import noInfinityInSimulationData from "./rules/no-infinity-in-simulation-data.js";
35
37
  import noKeptQueryValues from "./rules/no-kept-query-values.js";
38
+ import noLocaleInSimulation from "./rules/no-locale-in-simulation.js";
36
39
  import noStateSpread from "./rules/no-state-spread.js";
37
40
  import prefixDescriptorAndSystemNames from "./rules/prefix-descriptor-and-system-names.js";
38
41
  import useClockForTime from "./rules/use-clock-for-time.js";
@@ -49,6 +52,7 @@ export default definePlugin({
49
52
  "no-infinity-in-simulation-data": noInfinityInSimulationData,
50
53
  "no-float-timers": noFloatTimers,
51
54
  "no-brand-cast": noBrandCast,
55
+ "no-locale-in-simulation": noLocaleInSimulation,
52
56
  "handle-runtime-result": handleRuntimeResult,
53
57
  "prefix-descriptor-and-system-names": prefixDescriptorAndSystemNames,
54
58
  "no-state-spread": noStateSpread,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;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,iBAAiB,MAAM,iCAAiC,CAAA;AAC/D,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,8BAA8B,MAAM,+CAA+C,CAAA;AAC1F,OAAO,eAAe,MAAM,+BAA+B,CAAA;AAC3D,OAAO,sBAAsB,MAAM,uCAAuC,CAAA;AAC1E,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;QAChC,sBAAsB,EAAE,iBAAiB;QACzC,4BAA4B,EAAE,sBAAsB;KACrD;CACF,CAAC,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;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,iBAAiB,MAAM,iCAAiC,CAAA;AAC/D,OAAO,oBAAoB,MAAM,oCAAoC,CAAA;AACrE,OAAO,aAAa,MAAM,4BAA4B,CAAA;AACtD,OAAO,8BAA8B,MAAM,+CAA+C,CAAA;AAC1F,OAAO,eAAe,MAAM,+BAA+B,CAAA;AAC3D,OAAO,sBAAsB,MAAM,uCAAuC,CAAA;AAC1E,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,yBAAyB,EAAE,oBAAoB;QAC/C,uBAAuB,EAAE,mBAAmB;QAC5C,oCAAoC,EAAE,8BAA8B;QACpE,iBAAiB,EAAE,aAAa;QAChC,sBAAsB,EAAE,iBAAiB;QACzC,4BAA4B,EAAE,sBAAsB;KACrD;CACF,CAAC,CAAA"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Simulation code never asks how a language orders, cases, or writes a
3
+ * value: the answer comes from the machine's language settings and its
4
+ * engine's language data, so it differs between machines and a replay or an
5
+ * online session diverges.
6
+ */
7
+ declare const _default: import("@oxlint/plugins").Rule;
8
+ export default _default;
9
+ //# sourceMappingURL=no-locale-in-simulation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"no-locale-in-simulation.d.ts","sourceRoot":"","sources":["../../src/rules/no-locale-in-simulation.ts"],"names":[],"mappings":"AA8BA;;;;;GAKG"}
@@ -0,0 +1,74 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import { docsUrl, globalRead, propertyName } from "../internal/ast.js";
3
+ /**
4
+ * The methods that answer by a language's rules, by name: the receiver's type
5
+ * is not known to a lint rule. No other built-in shares these names; a method
6
+ * of the game's own that does is reported where it is called.
7
+ */
8
+ const methods = new Map([
9
+ ["localeCompare", "order"],
10
+ ["toLocaleLowerCase", "letterCase"],
11
+ ["toLocaleUpperCase", "letterCase"],
12
+ ["toLocaleString", "format"],
13
+ ["toLocaleDateString", "format"],
14
+ ["toLocaleTimeString", "format"]
15
+ ]);
16
+ /** The members of `Intl` with a message of their own; every other member is `words`. */
17
+ const intl = new Map([
18
+ ["Collator", "order"],
19
+ ["NumberFormat", "format"],
20
+ ["DateTimeFormat", "format"],
21
+ ["RelativeTimeFormat", "format"],
22
+ ["DurationFormat", "format"]
23
+ ]);
24
+ /**
25
+ * Simulation code never asks how a language orders, cases, or writes a
26
+ * value: the answer comes from the machine's language settings and its
27
+ * engine's language data, so it differs between machines and a replay or an
28
+ * online session diverges.
29
+ */
30
+ export default defineRule({
31
+ meta: {
32
+ type: "problem",
33
+ docs: {
34
+ description: "Keep language rules out of simulation code: no Intl, localeCompare, or toLocale* methods.",
35
+ url: docsUrl("no-locale-in-simulation")
36
+ },
37
+ messages: {
38
+ order: "`{{call}}` orders text by a language's rules: the machine's own language unless the call names one, and that language as the engine's own data has it, so two machines sort the same names differently and a replay or an online session diverges. Order by a number the world holds (an index, a rank), or compare the strings themselves, which every engine does the same way: `a < b ? -1 : a > b ? 1 : 0`.",
39
+ letterCase: "`{{call}}` changes case by a language's rules, the machine's own language unless the call names one (in Turkish \"I\" lowers to \"ı\" and \"i\" raises to \"İ\"), so a lookup or a comparison made with it can differ between machines. Use `toLowerCase()` or `toUpperCase()`, which answer the same everywhere. Case as a language writes it is for text the player reads, in view or UI code.",
40
+ format: "`{{call}}` writes a value as a language writes it (\"1,234.5\", \"1.234,5\"): the machine's own language unless the call names one, and that language as the engine's own data has it, so the text differs between machines; kept in the world, it is saved, checksummed, and replayed as written. Keep the value itself in the world, and write it where the world is read into view data (an inspector run with `runtime.inspect(...)`, or a system of a render schedule), outside the simulation's files.",
41
+ words: "`{{call}}` reads the engine's language data, which differs between browsers and Node versions, for the machine's own language unless the call names one, so it does not answer the same on every machine. The simulation holds ids and numbers; their words (a plural, a list, a name) are chosen where the world is read into view data (an inspector run with `runtime.inspect(...)`, or a system of a render schedule), outside the simulation's files."
42
+ },
43
+ schema: []
44
+ },
45
+ create(context) {
46
+ /** Reports a member of the global `Intl`, however it is reached; `true` when `node` reads one. */
47
+ const checkIntl = (node) => {
48
+ const read = globalRead(context, node);
49
+ if (read?.from !== "Intl")
50
+ return false;
51
+ context.report({ node, messageId: intl.get(read.path.slice("Intl.".length)) ?? "words", data: { call: read.path } });
52
+ return true;
53
+ };
54
+ return {
55
+ MemberExpression(node) {
56
+ if (checkIntl(node))
57
+ return;
58
+ const name = propertyName(node);
59
+ const messageId = name === undefined ? undefined : methods.get(name);
60
+ if (messageId === undefined)
61
+ return;
62
+ // `score.toLocaleString = ...` gives an object its own method: nothing is asked of a language there.
63
+ if (node.parent.type === "AssignmentExpression" && node.parent.left === node)
64
+ return;
65
+ context.report({ node, messageId, data: { call: `${name}()` } });
66
+ },
67
+ // `const { Collator } = Intl`
68
+ Property(node) {
69
+ checkIntl(node);
70
+ }
71
+ };
72
+ }
73
+ });
74
+ //# sourceMappingURL=no-locale-in-simulation.js.map
@@ -0,0 +1 @@
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.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
@@ -3,11 +3,12 @@
3
3
  *
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
- * clock, `Math.random`, engine-dependent math, or the page, or plays sound,
7
- * timers counted in floats, data that cannot be saved, casts that skip a
8
- * brand's check, state values that keep a previous state's fields, query
9
- * entities and data kept past their run, runtime results that are
10
- * dropped, and host key listeners that take keys from the page's controls.
6
+ * clock, `Math.random`, engine-dependent math, or the page, plays sound, or
7
+ * asks how a language orders, cases, or writes a value, timers counted in
8
+ * floats, data that cannot be saved, casts that skip a brand's check, state
9
+ * values that keep a previous state's fields, query entities and data kept
10
+ * past their run, runtime results that are dropped, and host key listeners
11
+ * that take keys from the page's controls.
11
12
  * Every report names the Ritmo API to use instead.
12
13
  *
13
14
  * Register the plugin in `.oxlintrc.json` with `"jsPlugins":
@@ -19,7 +20,8 @@
19
20
  * - Simulation files: `ritmo/use-clock-for-time`,
20
21
  * `ritmo/use-random-for-randomness`, `ritmo/use-ops-for-math`,
21
22
  * `ritmo/keep-simulation-headless`, `ritmo/no-infinity-in-simulation-data`,
22
- * `ritmo/no-float-timers`, `ritmo/no-brand-cast`.
23
+ * `ritmo/no-float-timers`, `ritmo/no-brand-cast`,
24
+ * `ritmo/no-locale-in-simulation`.
23
25
  * - Every file: `ritmo/handle-runtime-result`,
24
26
  * `ritmo/prefix-descriptor-and-system-names`, `ritmo/no-state-spread`,
25
27
  * `ritmo/no-kept-query-values`, `ritmo/use-keyboard-for-game-keys`.
@@ -34,6 +36,7 @@ import noBrandCast from "./rules/no-brand-cast.ts"
34
36
  import noFloatTimers from "./rules/no-float-timers.ts"
35
37
  import noInfinityInSimulationData from "./rules/no-infinity-in-simulation-data.ts"
36
38
  import noKeptQueryValues from "./rules/no-kept-query-values.ts"
39
+ import noLocaleInSimulation from "./rules/no-locale-in-simulation.ts"
37
40
  import noStateSpread from "./rules/no-state-spread.ts"
38
41
  import prefixDescriptorAndSystemNames from "./rules/prefix-descriptor-and-system-names.ts"
39
42
  import useClockForTime from "./rules/use-clock-for-time.ts"
@@ -51,6 +54,7 @@ export default definePlugin({
51
54
  "no-infinity-in-simulation-data": noInfinityInSimulationData,
52
55
  "no-float-timers": noFloatTimers,
53
56
  "no-brand-cast": noBrandCast,
57
+ "no-locale-in-simulation": noLocaleInSimulation,
54
58
  "handle-runtime-result": handleRuntimeResult,
55
59
  "prefix-descriptor-and-system-names": prefixDescriptorAndSystemNames,
56
60
  "no-state-spread": noStateSpread,
@@ -0,0 +1,80 @@
1
+ import { defineRule } from "@oxlint/plugins"
2
+ import type { ESTree } from "@oxlint/plugins"
3
+
4
+ import { docsUrl, globalRead, propertyName } from "../internal/ast.ts"
5
+
6
+ type Message = "order" | "letterCase" | "format" | "words"
7
+
8
+ /**
9
+ * The methods that answer by a language's rules, by name: the receiver's type
10
+ * is not known to a lint rule. No other built-in shares these names; a method
11
+ * of the game's own that does is reported where it is called.
12
+ */
13
+ const methods = new Map<string, Message>([
14
+ ["localeCompare", "order"],
15
+ ["toLocaleLowerCase", "letterCase"],
16
+ ["toLocaleUpperCase", "letterCase"],
17
+ ["toLocaleString", "format"],
18
+ ["toLocaleDateString", "format"],
19
+ ["toLocaleTimeString", "format"]
20
+ ])
21
+
22
+ /** The members of `Intl` with a message of their own; every other member is `words`. */
23
+ const intl = new Map<string, Message>([
24
+ ["Collator", "order"],
25
+ ["NumberFormat", "format"],
26
+ ["DateTimeFormat", "format"],
27
+ ["RelativeTimeFormat", "format"],
28
+ ["DurationFormat", "format"]
29
+ ])
30
+
31
+ /**
32
+ * Simulation code never asks how a language orders, cases, or writes a
33
+ * value: the answer comes from the machine's language settings and its
34
+ * engine's language data, so it differs between machines and a replay or an
35
+ * online session diverges.
36
+ */
37
+ export default defineRule({
38
+ meta: {
39
+ type: "problem",
40
+ docs: {
41
+ description: "Keep language rules out of simulation code: no Intl, localeCompare, or toLocale* methods.",
42
+ url: docsUrl("no-locale-in-simulation")
43
+ },
44
+ messages: {
45
+ order:
46
+ "`{{call}}` orders text by a language's rules: the machine's own language unless the call names one, and that language as the engine's own data has it, so two machines sort the same names differently and a replay or an online session diverges. Order by a number the world holds (an index, a rank), or compare the strings themselves, which every engine does the same way: `a < b ? -1 : a > b ? 1 : 0`.",
47
+ letterCase:
48
+ "`{{call}}` changes case by a language's rules, the machine's own language unless the call names one (in Turkish \"I\" lowers to \"ı\" and \"i\" raises to \"İ\"), so a lookup or a comparison made with it can differ between machines. Use `toLowerCase()` or `toUpperCase()`, which answer the same everywhere. Case as a language writes it is for text the player reads, in view or UI code.",
49
+ format:
50
+ "`{{call}}` writes a value as a language writes it (\"1,234.5\", \"1.234,5\"): the machine's own language unless the call names one, and that language as the engine's own data has it, so the text differs between machines; kept in the world, it is saved, checksummed, and replayed as written. Keep the value itself in the world, and write it where the world is read into view data (an inspector run with `runtime.inspect(...)`, or a system of a render schedule), outside the simulation's files.",
51
+ words:
52
+ "`{{call}}` reads the engine's language data, which differs between browsers and Node versions, for the machine's own language unless the call names one, so it does not answer the same on every machine. The simulation holds ids and numbers; their words (a plural, a list, a name) are chosen where the world is read into view data (an inspector run with `runtime.inspect(...)`, or a system of a render schedule), outside the simulation's files."
53
+ },
54
+ schema: []
55
+ },
56
+ create(context) {
57
+ /** Reports a member of the global `Intl`, however it is reached; `true` when `node` reads one. */
58
+ const checkIntl = (node: ESTree.Node): boolean => {
59
+ const read = globalRead(context, node)
60
+ if (read?.from !== "Intl") return false
61
+ context.report({ node, messageId: intl.get(read.path.slice("Intl.".length)) ?? "words", data: { call: read.path } })
62
+ return true
63
+ }
64
+ return {
65
+ MemberExpression(node) {
66
+ if (checkIntl(node)) return
67
+ const name = propertyName(node)
68
+ const messageId = name === undefined ? undefined : methods.get(name)
69
+ if (messageId === undefined) return
70
+ // `score.toLocaleString = ...` gives an object its own method: nothing is asked of a language there.
71
+ if (node.parent.type === "AssignmentExpression" && node.parent.left === node) return
72
+ context.report({ node, messageId, data: { call: `${name}()` } })
73
+ },
74
+ // `const { Collator } = Intl`
75
+ Property(node) {
76
+ checkIntl(node)
77
+ }
78
+ }
79
+ }
80
+ })