Skip to content

linters/eslint-rules/prefer-component-ex

ESLint rule: prefer-component-ex

Reports a class that extends Obsidian’s raw Component (import { Component } from 'obsidian', or obsidian.Component through a namespace import) and points it to ComponentEx from @mnaoumov/obsidian-lib. ComponentEx is a Component that also tracks its children, makes an async load awaitable (onloadAsync, loadWithPromises) and collects its load errors, implements Disposable, and refuses a child added after unload instead of leaking it. Every plugin built with this toolkit already depends on obsidian-lib, so a raw Component gives all of that up for nothing.

A class extending another Obsidian Component subclass (Plugin, View, MarkdownRenderChild, Modal…) is not reported: only a direct extends Component is. A genuine exception, such as ComponentEx itself, disables the rule on the line with its reason.

The autofix runs only when the swap is mechanical: the superclass is the named Component import, and the class declares no member that ComponentEx already defines, since a member with such a name would override or clash with it. The fix replaces the superclass, adds the ComponentEx import unless one is there, and removes the Component import specifier when the superclass was its only reference.

Variable Description
COMPONENT_EX_MODULE The module ComponentEx is imported from by the autofix.
MESSAGE_ID Message ID reported for a class that extends Obsidian’s raw Component.
preferComponentEx ESLint rule preferring ComponentEx from @mnaoumov/obsidian-lib over Obsidian’s raw Component as a base class.