`@wordpress/no-dom-globals-in-constructor` fires on non-React class constructors (no React-component check)
Description
@wordpress/no-dom-globals-in-constructor flags any use of a DOM global inside any class constructor, with no check for whether the enclosing class is a React component. Its test() only inspects that the scope block's parent is a MethodDefinition of kind === 'constructor'.
This produces false positives on plain (non-React) classes that legitimately run only client-side, where accessing window/document in the constructor is safe. This is inconsistent with the sibling rules added in the same PR (#76508): no-dom-globals-in-react-fc uses a JSX-return heuristic and no-dom-globals-in-react-cc-render targets React class-component render() — both are React-aware, but the constructor rule is not. The rule message even hedges with "or equivalent for non React components", implying non-React classes are in scope, yet for such classes the SSR rationale doesn't apply.
I expect the rule to only flag constructors of React components (or offer an option to), rather than every class constructor.
Step-by-step reproduction instructions
- Enable
@wordpress/no-dom-globals-in-constructor(e.g. via@wordpress/eslint-pluginrecommended config, flat config). - Lint a plain, non-React class whose constructor references a DOM global:
class Gallery { constructor( container ) { window.addEventListener( 'resize', () => this.reflow() ); } reflow() {} } - The rule reports: "Use of DOM global 'window' is forbidden in class constructors…" even though
Galleryis not a React component.
Environment info
@wordpress/eslint-plugin26.0.0, ESLint 10.10.0- Not the block editor — this is a developer-tooling (ESLint rule) bug; theme/plugin/browser fields N/A.
Please confirm that you have searched existing issues in the repo.
- Yes
Source: WordPress/gutenberg