@typeonce/ritmo-devtools 0.9.0 → 0.11.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.
Files changed (70) hide show
  1. package/CHANGELOG.md +274 -0
  2. package/README.md +33 -9
  3. package/dist/Format.d.ts +1 -0
  4. package/dist/Format.d.ts.map +1 -1
  5. package/dist/Format.js.map +1 -1
  6. package/dist/Invariant.d.ts +2 -1
  7. package/dist/Invariant.d.ts.map +1 -1
  8. package/dist/Invariant.js.map +1 -1
  9. package/dist/Lint.d.ts +1 -0
  10. package/dist/Lint.d.ts.map +1 -1
  11. package/dist/Lint.js.map +1 -1
  12. package/dist/Monitor.d.ts +2 -0
  13. package/dist/Monitor.d.ts.map +1 -1
  14. package/dist/Monitor.js.map +1 -1
  15. package/dist/Rendered.d.ts +2 -0
  16. package/dist/Rendered.d.ts.map +1 -1
  17. package/dist/Rendered.js +2 -0
  18. package/dist/Rendered.js.map +1 -1
  19. package/dist/Replay.d.ts +8 -2
  20. package/dist/Replay.d.ts.map +1 -1
  21. package/dist/Replay.js.map +1 -1
  22. package/dist/ReplayNodeSink.d.ts +1 -0
  23. package/dist/ReplayNodeSink.d.ts.map +1 -1
  24. package/dist/ReplayNodeSink.js +1 -0
  25. package/dist/ReplayNodeSink.js.map +1 -1
  26. package/dist/ReplayVitePlugin.d.ts +5 -1
  27. package/dist/ReplayVitePlugin.d.ts.map +1 -1
  28. package/dist/ReplayVitePlugin.js +4 -0
  29. package/dist/ReplayVitePlugin.js.map +1 -1
  30. package/dist/ReplayViteSink.d.ts +5 -0
  31. package/dist/ReplayViteSink.d.ts.map +1 -1
  32. package/dist/ReplayViteSink.js +4 -0
  33. package/dist/ReplayViteSink.js.map +1 -1
  34. package/dist/Session.d.ts +38 -6
  35. package/dist/Session.d.ts.map +1 -1
  36. package/dist/Session.js +12 -4
  37. package/dist/Session.js.map +1 -1
  38. package/dist/cli.js +28 -1
  39. package/dist/cli.js.map +1 -1
  40. package/dist/index.d.ts +1 -0
  41. package/dist/index.d.ts.map +1 -1
  42. package/dist/index.js +1 -0
  43. package/dist/index.js.map +1 -1
  44. package/dist/internal/monitor.d.ts +3 -1
  45. package/dist/internal/monitor.d.ts.map +1 -1
  46. package/dist/internal/monitor.js.map +1 -1
  47. package/dist/internal/simulationCoverage.d.ts +18 -0
  48. package/dist/internal/simulationCoverage.d.ts.map +1 -0
  49. package/dist/internal/simulationCoverage.js +135 -0
  50. package/dist/internal/simulationCoverage.js.map +1 -0
  51. package/dist/internal/version.d.ts +1 -1
  52. package/dist/internal/version.d.ts.map +1 -1
  53. package/dist/internal/version.js +1 -1
  54. package/dist/internal/version.js.map +1 -1
  55. package/package.json +4 -3
  56. package/src/Format.ts +1 -0
  57. package/src/Invariant.ts +2 -1
  58. package/src/Lint.ts +1 -0
  59. package/src/Monitor.ts +2 -0
  60. package/src/Rendered.ts +2 -0
  61. package/src/Replay.ts +7 -1
  62. package/src/ReplayNodeSink.ts +1 -0
  63. package/src/ReplayVitePlugin.ts +5 -1
  64. package/src/ReplayViteSink.ts +5 -0
  65. package/src/Session.ts +38 -6
  66. package/src/cli.ts +27 -1
  67. package/src/index.ts +1 -0
  68. package/src/internal/monitor.ts +3 -1
  69. package/src/internal/simulationCoverage.ts +136 -0
  70. package/src/internal/version.ts +1 -1
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Simulation coverage for the `ritmo-devtools lint` CLI (Node only): which
3
+ * modules the simulation loads that the oxlint simulation rules
4
+ * (`ritmo/keep-simulation-headless` and its neighbours) do not cover.
5
+ *
6
+ * @internal
7
+ */
8
+ import { readFileSync, realpathSync } from "node:fs"
9
+ import * as Module from "node:module"
10
+ import { dirname, isAbsolute, join, matchesGlob, relative, sep } from "node:path"
11
+ import { fileURLToPath } from "node:url"
12
+
13
+ const configName = ".oxlintrc.json"
14
+ const rule = "ritmo/keep-simulation-headless"
15
+
16
+ /** Starts recording every `file:` module Node loads from now on. `undefined` when this Node has no `module.registerHooks` (before 22.15). */
17
+ export const recordLoads = (): (() => ReadonlyArray<string>) | undefined => {
18
+ if (typeof Module.registerHooks !== "function") return undefined
19
+ const urls = new Set<string>()
20
+ Module.registerHooks({
21
+ load: (url, context, nextLoad) => {
22
+ if (url.startsWith("file:")) urls.add(url)
23
+ return nextLoad(url, context)
24
+ }
25
+ })
26
+ return () => [...urls]
27
+ }
28
+
29
+ export type Coverage =
30
+ | { readonly checked: false; readonly reason: string }
31
+ | { readonly checked: true; readonly uncovered: ReadonlyArray<string> }
32
+
33
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
34
+ typeof value === "object" && value !== null && !Array.isArray(value)
35
+
36
+ /** Removes `//` and `/* *\/` comments outside strings, then trailing commas before `}` or `]`. */
37
+ const stripJsonc = (text: string): string => {
38
+ let out = ""
39
+ let index = 0
40
+ while (index < text.length) {
41
+ const char = text[index]!
42
+ if (char === "\"") {
43
+ let end = index + 1
44
+ while (end < text.length && text[end] !== "\"") end += text[end] === "\\" ? 2 : 1
45
+ out += text.slice(index, end + 1)
46
+ index = end + 1
47
+ } else if (char === "/" && text[index + 1] === "/") {
48
+ while (index < text.length && text[index] !== "\n") index++
49
+ } else if (char === "/" && text[index + 1] === "*") {
50
+ const end = text.indexOf("*/", index + 2)
51
+ index = end === -1 ? text.length : end + 2
52
+ } else {
53
+ out += char
54
+ index++
55
+ }
56
+ }
57
+ return out.replace(/("(?:[^"\\]|\\.)*")|,(\s*[}\]])/g, (match, string: string | undefined, close: string | undefined) =>
58
+ string !== undefined ? match : close!)
59
+ }
60
+
61
+ /** `true` on, `false` off, `undefined` when the value does not set the rule. */
62
+ const severity = (value: unknown): boolean | undefined => {
63
+ const level = Array.isArray(value) ? value[0] : value
64
+ if (level === "error" || level === "warn" || level === 1 || level === 2) return true
65
+ if (level === "off" || level === 0) return false
66
+ return undefined
67
+ }
68
+
69
+ const globs = (value: unknown): ReadonlyArray<string> =>
70
+ Array.isArray(value) ? value.filter((glob): glob is string => typeof glob === "string") : []
71
+
72
+ const matches = (file: string, patterns: ReadonlyArray<string>): boolean =>
73
+ patterns.some((pattern) => matchesGlob(file, pattern))
74
+
75
+ /**
76
+ * Reads `.oxlintrc.json` in `cwd` and returns the loaded modules inside its
77
+ * directory (outside `node_modules`, not the entry) where `ritmo/keep-simulation-headless`
78
+ * is not on, as posix paths relative to the config, sorted.
79
+ */
80
+ export const check = (cwd: string, entry: string, loaded: ReadonlyArray<string>): Coverage => {
81
+ const configPath = join(cwd, configName)
82
+ let text: string
83
+ try {
84
+ text = readFileSync(configPath, "utf8")
85
+ } catch {
86
+ return { checked: false, reason: `no ${configName} in ${cwd}` }
87
+ }
88
+ let config: unknown
89
+ try {
90
+ config = JSON.parse(stripJsonc(text))
91
+ } catch {
92
+ return { checked: false, reason: `${configName} is not valid JSON with comments` }
93
+ }
94
+ if (!isRecord(config)) return { checked: false, reason: `${configName} is not an object` }
95
+ if (config["extends"] !== undefined) return { checked: false, reason: `${configName} uses "extends", which this check does not follow` }
96
+ const topLevel = isRecord(config["rules"]) ? severity(config["rules"][rule]) : undefined
97
+ const overrides = (Array.isArray(config["overrides"]) ? config["overrides"] : []).filter(isRecord).map((override) => ({
98
+ files: globs(override["files"]),
99
+ setting: isRecord(override["rules"]) ? severity(override["rules"][rule]) : undefined
100
+ }))
101
+ if (topLevel !== true && !overrides.some((override) => override.setting === true)) {
102
+ return { checked: false, reason: `no rules in ${configName} turn on ${rule}` }
103
+ }
104
+ const ignored = globs(config["ignorePatterns"])
105
+ const root = realpathSync(dirname(configPath))
106
+ const entryPath = realpathSync(entry)
107
+ const uncovered = new Set<string>()
108
+ for (const url of loaded) {
109
+ let file: string
110
+ try {
111
+ file = realpathSync(fileURLToPath(url))
112
+ } catch {
113
+ continue
114
+ }
115
+ const path = relative(root, file)
116
+ if (file === entryPath || path === "" || path === ".." || path.startsWith(`..${sep}`) || isAbsolute(path)) continue
117
+ const posix = path.split(sep).join("/")
118
+ if (posix.split("/").includes("node_modules")) continue
119
+ let on = topLevel === true
120
+ for (const override of overrides) {
121
+ if (override.setting !== undefined && matches(posix, override.files)) on = override.setting
122
+ }
123
+ if (!on || matches(posix, ignored)) uncovered.add(posix)
124
+ }
125
+ return { checked: true, uncovered: [...uncovered].sort() }
126
+ }
127
+
128
+ /** The CLI text for a coverage result: a "not checked" line, or one block per uncovered module. */
129
+ export const format = (coverage: Coverage): string =>
130
+ !coverage.checked
131
+ ? `simulation coverage not checked: ${coverage.reason}`
132
+ : coverage.uncovered.map((file) =>
133
+ `uncovered-simulation-module ${file}\n`
134
+ + ` The simulation loads this file, but the simulation rules in ${configName} do not cover it.\n`
135
+ + ` Add it to the simulation override's "files", or stop importing values from it.`
136
+ ).join("\n")
@@ -3,4 +3,4 @@
3
3
  * `package.json` by `scripts/sync-version.ts` (run by \`pnpm version-packages\`)
4
4
  * and checked by a test.
5
5
  */
6
- export const libraryVersion = "0.9.0"
6
+ export const libraryVersion = "0.11.0"