From 4ba91fcb8ef64ab2dfe39aa0cfad3a558a772f98 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 5 Sep 2026 19:26:27 -0400 Subject: [PATCH] fix: AuthStore.availableTeams never becomes observable (#13672) Add an initializer so makeObservable picks up the field, and add a local oxlint rule that flags @observable fields without an initializer in classes that call makeObservable(this). Co-authored-by: Claude Fable 5.1 --- .oxlint/mobx.mjs | 133 ++++++++++++++++++++++++++++++++++++++++ .oxlintrc.json | 2 + app/stores/AuthStore.ts | 2 +- 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 .oxlint/mobx.mjs diff --git a/.oxlint/mobx.mjs b/.oxlint/mobx.mjs new file mode 100644 index 0000000000..ae1dfd14ca --- /dev/null +++ b/.oxlint/mobx.mjs @@ -0,0 +1,133 @@ +/** + * Local oxlint plugin with MobX-specific rules. + */ + +/** Returns true if the decorator is `@observable` or `@observable.`. */ +function isObservableDecorator(decorator) { + const expression = decorator.expression; + + if (expression.type === "Identifier") { + return expression.name === "observable"; + } + + if (expression.type === "MemberExpression") { + return ( + expression.object.type === "Identifier" && + expression.object.name === "observable" + ); + } + + return false; +} + +/** Returns true if the expression is a `makeObservable(this, …)` call. */ +function isMakeObservableCall(expression) { + return ( + expression.type === "CallExpression" && + expression.callee.type === "Identifier" && + expression.callee.name === "makeObservable" && + expression.arguments.length > 0 && + expression.arguments[0].type === "ThisExpression" + ); +} + +/** Returns the property name if the expression is `this. = …`, else null. */ +function assignedThisProperty(expression) { + if ( + expression.type !== "AssignmentExpression" || + expression.left.type !== "MemberExpression" || + expression.left.object.type !== "ThisExpression" || + expression.left.property.type !== "Identifier" + ) { + return null; + } + return expression.left.property.name; +} + +/** + * Walks the constructor body and returns the set of `this.` properties + * assigned before `makeObservable(this)` is called, or null if the constructor + * never calls it. + */ +function propertiesAssignedBeforeMakeObservable(classBody) { + const ctor = classBody.body.find( + (member) => + member.type === "MethodDefinition" && member.kind === "constructor" + ); + if (!ctor?.value?.body) { + return null; + } + + const assigned = new Set(); + for (const statement of ctor.value.body.body) { + if (statement.type !== "ExpressionStatement") { + continue; + } + if (isMakeObservableCall(statement.expression)) { + return assigned; + } + const name = assignedThisProperty(statement.expression); + if (name) { + assigned.add(name); + } + } + + return null; +} + +const plugin = { + meta: { + name: "mobx", + }, + rules: { + "observable-needs-initializer": { + meta: { + type: "problem", + docs: { + description: + "Require an initializer on @observable class fields in classes that call makeObservable(this). A field that does not exist as an own property when makeObservable runs is silently skipped by MobX.", + }, + messages: { + missing: + "@observable field '{{name}}' has no initializer, so makeObservable() will silently skip it. Add `= undefined` or another initial value.", + }, + }, + create(context) { + return { + ClassBody(node) { + const assigned = propertiesAssignedBeforeMakeObservable(node); + if (assigned === null) { + return; + } + + for (const member of node.body) { + if ( + member.type !== "PropertyDefinition" || + member.value !== null || + member.static || + member.declare || + member.key.type !== "Identifier" || + assigned.has(member.key.name) + ) { + continue; + } + + const decorators = member.decorators ?? []; + if (!decorators.some(isObservableDecorator)) { + continue; + } + + context.report({ + node: member, + messageId: "missing", + data: { name: member.key.name }, + }); + } + }, + }; + }, + }, + }, +}; + +export default plugin; diff --git a/.oxlintrc.json b/.oxlintrc.json index 7b2243a1a5..5cc0cbd062 100644 --- a/.oxlintrc.json +++ b/.oxlintrc.json @@ -9,7 +9,9 @@ "patches/**", "*.d.ts" ], + "jsPlugins": ["./.oxlint/mobx.mjs"], "rules": { + "mobx/observable-needs-initializer": "error", "for-direction": "error", "no-async-promise-executor": "error", "no-case-declarations": "error", diff --git a/app/stores/AuthStore.ts b/app/stores/AuthStore.ts index 7136702fcd..0c5bf82142 100644 --- a/app/stores/AuthStore.ts +++ b/app/stores/AuthStore.ts @@ -67,7 +67,7 @@ export default class AuthStore extends Store { avatarUrl: string; url: string; isSignedIn: boolean; - }[]; + }[] = undefined; /* The authentication provider the user signed in with. */ @observable