Skip to main content

class-match-filename

Require class declarations to match the current filename.

Targeted pattern scopeโ€‹

This rule checks named class declarations and compares each class name to the current filename stem.

It targets top-level class declarations in these forms:

  • class Name {}
  • export class Name {}
  • export default class Name {}

Class expressions are not targeted.

What this rule reportsโ€‹

This rule reports class declarations whose identifier does not exactly match the source filename stem.

Why this rule existsโ€‹

Aligning class names with file names improves discoverability and reduces rename drift during refactors.

โŒ Incorrectโ€‹

// filename: UserService.ts
export class AccountService {}

โœ… Correctโ€‹

// filename: UserService.ts
export class UserService {}

Behavior and migration notesโ€‹

This rule reports only and does not provide an autofix.

Migration requires renaming either the class identifier or the file stem.

Optionsโ€‹

This rule has no options.

Additional examplesโ€‹

// filename: user-service.ts
export default class UserService {}
// โŒ reported (filename stem is "user-service")

// filename: UserService.ts
export default class UserService {}
// โœ… valid

ESLint flat config exampleโ€‹

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

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/class-match-filename": "error",
},
},
];

When not to use itโ€‹

Disable this rule if your project intentionally allows class names that do not mirror file names.

Package documentationโ€‹

Rule catalog ID: R002

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.