Repository navigation
Quick Start
This page uses examples/basic. The tests run eslint there and compare the output with the committed one, so what is shown is what the tool prints.
eslint.config.js:
import ownership from "@mrzr/eslint-plugin-component-ownership";
import tseslint from "typescript-eslint";
export default [
{
files: ["**/*.tsx"],
languageOptions: {
parser: tseslint.parser,
parserOptions: { ecmaFeatures: { jsx: true } },
},
plugins: { ownership },
},
{
// Screens arrange components; the look belongs to the components in core.
files: ["src/features/**/*.tsx"],
rules: {
"ownership/one-component-per-file": "error",
"ownership/no-external-styling": "error",
},
},
];The first block registers the plugin and the parser for .tsx. The second turns both rules on for src/features. Rules are not turned on by the plugin itself.
src/features/profile.tsx:
function Avatar() {
return <img className="size-8 rounded-full" alt="" />;
}
export function Profile() {
return (
<section className="flex gap-3 bg-card p-4">
<Avatar />
<h2 className={cn("min-w-0 flex-1", active && "text-primary")}>Name</h2>
</section>
);
}npx eslint src
src/features/profile.tsx
2:25 error `rounded-full` styles an element from outside a component. Move it into the component's variants, or make the styled element a component ownership/no-external-styling
5:17 error `Profile` is a second component in this file. Move it into its own file (one component per file) ownership/one-component-per-file
7:24 error `bg-card` styles an element from outside a component. Move it into the component's variants, or make the styled element a component ownership/no-external-styling
7:24 error `p-4` styles an element from outside a component. Move it into the component's variants, or make the styled element a component ownership/no-external-styling
9:53 error `text-primary` styles an element from outside a component. Move it into the component's variants, or make the styled element a component ownership/no-external-styling
✖ 5 problems (5 errors, 0 warnings)
-
Line 2:
rounded-fullis a visual class on an element of a screen.size-8is a sizing class and is allowed. -
Line 5:
Profileis the second component of the file. The first one,Avatar, is not reported. -
Line 7:
bg-cardandp-4are reported,flexandgap-3are not. -
Line 9:
text-primaryinsidecn(...)is found.min-w-0andflex-1are allowed.
The exit code is 1, because the rules are on as "error". Use "warn" to report without failing.
src/core/button.tsx has two components and rounded-md bg-primary classes, and is not reported, because the config does not turn the rules on there. A design system folder is where the look is meant to live.
When you turn a rule on in a project that has files that break it, ESLint lets you use a different severity for a list of files. Put them in a later config block as "warn" and remove each one from the list when it is fixed:
{
files: ["src/features/old-screen.tsx"],
rules: { "ownership/one-component-per-file": "warn" },
},