Methods on an enum's values: error.describe(), turned into calls of the table and checked #151
Loading…
Reference in a new issue
No description provided.
Delete branch "enum-instance-methods"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
You wanted to call an enum's functions on its values (
error.describe()), not on the table. This PR adds that, stacked on #149.How it works. The values stay plain objects, so there's no runtime cost and JSON,
validate<T>, spreads andstructuredClonekeep working. The build follows the value through what the source declares and turns each call into a call of the table: parameters, locals, fields,#fields and parameter properties, items of declared lists and maps (for...of,forEach/map,map.get(k)?.m()), declared return types, constructor results,matchhandlers (destructured too),variant(...), and the valueimpl's own functions get. That covers the ten receiver forms plus the enum-specific forms in the fixture.x?.m()is supported, andf()?.m()goes through std's newcallIfPresent. When only the type is imported, the table gets imported under an alias. Legacy files get the rewrite too.What can't be rewritten is refused. Four rules, all errors and on by default:
enum-method-receiver: the receiver's type isn't declared anywhere the checker follows, the call has an unsupported form (m?.(), a longer optional chain), the call is in a Vue template, or the method is read off its value (.m,["m"],const { m } = x).enum-method-escape: the value is passed where a type naming one of its methods is declared.enum-method-name: a method named like a built-in's or a library's (get,next,map,update,toString,valueOf,toJSON,then). The list is fixed, built from the ECMAScript inventory and the node, browser, ws, vue-router and zod maps, never from the program. Without it, an unresolvedx.get()couldn't be told apart from the enum's.enum-as-text: any enum's value turned into text (${x},String(x),x + "...",xs.join(),x.toString()), which gives "[object Object]". Values whose type the checker can't follow aren't checked: they print "[object Object]" too, but never crash.toStringcan't be a method, because the language calls it out of sight of the build.Adopter check (a scratch copy of the adopter, with a probe enum added): 0 reports from all four rules with distinctive method names (
describe,label,nextAttempt,isRetryable,summary), and 0enum-as-texton its existing enums. Withget,nextandtoString, there are 3enum-method-nameerrors at the declaration and nothing at the call sites. (Before the name rule,getalone gave 836 receiver errors.)Tests (fast profile):
cargo nextest run --workspace: 424/424, including the new fixturesenum_methodsandvue_enum_methods, 7 transform tests, the spelling harness (no new misses) and the work ceilings (unchanged, after two cheap fixes);npm run test:std: 460/460, with runtime tests through the transform;npm run typecheck: passes, including the emittedenum.check.ts;node scripts/docs-typecheck.mjs: 1,126 examples compile;crates/safe_ts_core/tests/docs.rs: passes;npm run test:soundness: unchanged, 16 of 110 classes escape.Known gaps:
enum-method-escapedoesn't see generic constraints (<T extends Describable>), fields or return positions.enum-as-text.Created with Claude Opus 5.5 in Claude Code.
`Enum<V, M>` declares methods every value has (`{ readonly describe: () => string }`); `constructors<E>().impl({ describe: (error) => ... })` implements each, the value first and typed from the declaration, next to the table's own functions and constants. Values stay plain objects: the build turns `error.describe()` into `UploadError.describe(error)`. `Fields` excludes the methods; `Data<E>` is a value without them (what the build checks its literals against); `callIfPresent` is what it writes for `f()?.m()`. `Object.prototype`'s names, `toJSON`, `then`, `impl` and `tag` can't be methods. Co-Authored-By: Claude Opus 5.5 <[email protected]>The build follows the receiver through what the source declares (parameters, locals, fields, declared lists and maps, return types, constructor results, match handlers, variant(...), the value impl's functions get) and writes `error.describe()` as `UploadError.describe(error)`, `x?.m()` guarded, the table imported where only the type is. Legacy files get it too. In TypeScript output, literals of an enum with methods are checked against `Data<E>`, and impl's functions get their declared return types (a call of the table in a body would make the type circular). Rules, on by default as errors: - enum-method-name: a method named like a built-in's or a feature pack library's method (`get`, `next`, `toString`); a fixed list from the ECMAScript inventory and the node, browser, ws, vue-router and zod maps. - enum-method-receiver: a call whose receiver the declarations don't name, a form the rewrite doesn't take, a call in a Vue template, a method taken off its value. - enum-method-escape: a value given where a type naming one of its methods is declared. - enum-as-text: any enum's value turned into text (`${x}`, `String(x)`, `+`, `join`). Co-Authored-By: Claude Opus 5.5 <[email protected]>1bc15376b2c226202f03