Skip to main content

prefer-stylelint-formatter

Prefer explicit top-level Stylelint formatter configuration in authored Stylelint config files.

Targeted pattern scopeโ€‹

This rule targets Stylelint config modules such as stylelint.config.ts, stylelint.config.mjs, and .stylelintrc.js.

It focuses on top-level exported config objects, including configs wrapped in defineConfig(...).

What this rule reportsโ€‹

This rule reports Stylelint config objects that omit formatter, or configure it with an unusable empty string.

Why this rule existsโ€‹

If your team wants Stylelint output shape to be explicit across runners, an explicit shared formatter setting keeps that policy visible in the config instead of burying it in task wiring.

This is an opinionated rule. It is useful only when consistent formatter selection is part of your repository policy.

This rule is intentionally excluded from stylelint2.configs.recommended because it encodes an opinionated operational default rather than a broadly applicable config-hygiene rule.

โŒ Incorrectโ€‹

export default {
rules: {},
};

โœ… Correctโ€‹

export default {
formatter: "json",
rules: {},
};

Behavior and migration notesโ€‹

  • This rule reports missing or unusable top-level formatter configuration.
  • It does not auto-fix because the correct formatter choice is repository-specific.
  • Non-literal formatter expressions are accepted to avoid false positives in JavaScript-based config modules.

Additional examplesโ€‹

โœ… Correct โ€” formatter belongs in the runnerโ€‹

await stylelint.lint({
code,
formatter: "string",
});

ESLint flat config exampleโ€‹

import stylelint2 from "eslint-plugin-stylelint-2";

export default [
stylelint2.configs.configuration,
];

When not to use itโ€‹

Do not use this rule if your team prefers formatter choice to remain purely at invocation scope.

Package documentationโ€‹

Stylelint package documentation:

Rule catalog ID: R007

Further readingโ€‹