Repository navigation
Conversation
|
Thanks, I'll take a look soon! |
|
Hey @cpruijsen - this is looking pretty good, although I notice a small slowdown in model creation, which we're already sensitive to in existing code. Some of our other checks are already gated behind dev mode. Would you mind moving this check behind dev mode instead, along with an option to enable it in production if users want to opt in to slower, but more correct checking in prod? Let me know if you need a hand with those suggestions. Thanks! |
…ed with setMemberRedeclarationChecking
|
I moved the check behind dev mode, so production skips it by default. To turn it on in production, call |
What does this PR do and why?
In development mode, or in production after
setMemberRedeclarationChecking(true), MST now throws atcreate()when a model declares the same member name as two different kinds, for example an action and then a view calledanAction. Before, the later declaration silently replaced the earlier one (#2208). The only existing check compared member names against properties.The message follows the existing property-collision wording:
The check lives in
src/types/complex-types/model.tsand also applies through.extend(). Member names and kinds are recorded per instance in a module-levelWeakMapwhile the initializers run, and the entry is deleted once the instance is finalized.Judgement calls:
.actions()or.views()with the same name still overrides the first, because existing tests rely on it (composable actions, twoafterCreatehooks, override through compose) and the issue discussion asks to keep those patterns. If you want same-kind duplicates to throw too, I can change it.types.composekeeps its override semantics. Each composed type starts with an empty name record, sotypes.compose(LoudPerson, QuietPerson)still lets the second type override the first, also across kinds. Duplicates inside one composed type still throw.types.compose(A, B).views(...)) can still override a member ofAof a different kind without an error, as it does today. Fixing it needs per-operand bookkeeping and a decision on the semantics, so it is left out.Fixes #2208
Steps to validate locally
The four new collision tests in
__tests__/core/model.test.ts(action then view, view then action, action then volatile state, volatile state then view through.extend()) fail without the change tomodel.tsand pass with it. Two more tests confirm that same-kind redeclaration and cross-kind override throughtypes.composestill work.