linters/eslint-rules/prefer-active-window
ESLint rule: prefer-active-window
Reports a member read off the bare global window (window.getSelection(), window.innerWidth, window['scrollY']) and points it to activeWindow. In Obsidian the global window is always the MAIN window, while code that runs for a popout window - a view, a modal, a command run from the popout - belongs to that popout, which activeWindow names. obsidianmd/prefer-active-doc covers document only and leaves window alone, so a plugin otherwise gets no hint that its selection, focus or size read comes from the wrong window.
The rule is scope-aware: window is reported only when it resolves to the global. A parameter, local variable, import or other binding named window in the file - such as the popout’s own window that workspace.on('window-open') hands its handler - is that binding, not the global, and is left alone.
A few members stay on window, as obsidianmd/prefer-window-timers and obsidianmd/prefer-active-doc want: the timers and animation frames (setTimeout, clearTimeout, setInterval, clearInterval, requestAnimationFrame, cancelAnimationFrame), whose callbacks resolve through their own closure; open; and the listener methods (addEventListener, removeEventListener, dispatchEvent), since a listener a plugin registers for its lifetime belongs on the main window, which outlives every popout. A computed access is checked by its key when that is a static string, and reported otherwise.
There is no autofix, because the rewrite changes which window the code reads: the report offers it as a suggestion, to be applied where the code really runs for the active window. A read that must stay on the main window disables the rule on the line with its reason.
Re-implemented after the bare-window no-restricted-syntax selector of obsidian-plugin-dev-kit’s src/eslint/obsidian-plugin.ts (v0.4.0, MIT), which reports a parameter named window as well; the scope check and the listener exemption are this rule’s own.
Variables
Section titled “Variables”| Variable | Description |
|---|---|
| MESSAGE_ID | Message ID reported for a member read off the bare global window. |
| preferActiveWindow | ESLint rule preferring activeWindow over the bare global window. |
| SUGGESTION_MESSAGE_ID | Message ID of the suggestion that rewrites window to activeWindow. |
| WINDOW_MEMBER_EXCEPTIONS | The window members the rule leaves alone. |