Skip to main content

sort-top-comments

Enforce alphabetical sorting of top-of-file comments.

Targeted pattern scopeโ€‹

This rule inspects comments located before the first top-level program node.

Only that top-of-file comment region is considered.

What this rule reportsโ€‹

If there are at least two top-of-file comments, this rule reports when their order is not alphabetical.

An autofix rewrites that comment span in sorted order.

Why this rule existsโ€‹

Sorted file headers reduce churn in shared banner/comment blocks and make them easier to scan.

โŒ Incorrectโ€‹

// zeta
// alpha
const value = 1;

โœ… Correctโ€‹

// alpha
// zeta
const value = 1;

Behavior and migration notesโ€‹

This rule is autofixable.

The fixer only rewrites the contiguous top comment span before the first statement.

Optionsโ€‹

This rule has no options.

Additional examplesโ€‹

/* zeta */
/* alpha */
import { run } from "./run";
// โŒ reported and reordered

ESLint flat config exampleโ€‹

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/sort-top-comments": "error",
},
},
];

When not to use itโ€‹

Disable this rule if top comments intentionally represent ordered execution steps, chronological logs, or legal blocks with fixed sequence.

Package documentationโ€‹

Rule catalog ID: R071

Further readingโ€‹

Adoption resourcesโ€‹

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.