Skip to main content

match-filename

Require selected declaration names to match filename casing.

Targeted pattern scopeโ€‹

This rule compares selected declaration identifiers to the current filename stem.

Default selectors are:

  • ClassDeclaration > Identifier.id
  • FunctionDeclaration > Identifier.id
  • TSInterfaceDeclaration > Identifier.id
  • TSTypeAliasDeclaration > Identifier.id

You can replace this set with custom selector(s).

What this rule reportsโ€‹

For each matched identifier, the rule derives an expected filename token from the identifier name using format, plus optional prefix/suffix.

It then compares that expected value with the current filename stem.

  • match: true (default) reports when they differ.
  • match: false reports when they are equal.

Why this rule existsโ€‹

This enables strict declaration-to-file naming contracts, which helps keep large codebases predictable.

โŒ Incorrectโ€‹

// filename: user-service.ts
export function buildClient() {}
// โŒ default format derives "build-client", not "user-service"

โœ… Correctโ€‹

// filename: build-client.ts
export function buildClient() {}

Behavior and migration notesโ€‹

This rule reports only and does not provide an autofix.

Adopt safely by first applying it to a narrow selector scope, then broadening once naming conventions are stable.

Optionsโ€‹

type Options = [
{
format?: "camelCase" | "kebab-case" | "PascalCase";
match?: boolean;
prefix?: string;
selector?: string | string[];
suffix?: string;
},
];

Default:

{ format: "kebab-case", match: true }

Additional examplesโ€‹

// filename: use-user-service.ts
// config: { prefix: "use-", format: "kebab-case" }
export function userService() {}
// โœ… expected stem becomes "use-user-service"

ESLint flat config exampleโ€‹

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

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

When not to use itโ€‹

Disable this rule if files intentionally contain multiple unrelated declarations or naming is driven by non-code concerns (routing/layout systems, generated files, etc.).

Package documentationโ€‹

Rule catalog ID: R014

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.