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.