Skip to main content

export-matching-filename-only

Require filename-matching export to be the only export.

Targeted pattern scopeโ€‹

This rule collects exports in a file and checks whether one export name matches the filename-derived expected name.

If that matching export exists and the file exports anything else, the additional exports are reported.

What this rule reportsโ€‹

Expected export name is derived from filename stem using format (PascalCase by default).

When a matching export is present, all non-matching exports in that file are reported.

Supported extracted named exports include:

  • export { Name }
  • export class Name {}
  • export function name() {}

Default export is tracked as name default.

Why this rule existsโ€‹

This enforces a single-primary-export convention for modules whose main export name mirrors the file name.

โŒ Incorrectโ€‹

// filename: UserService.ts
export class UserService {}
export const helper = 1;
// โŒ helper export is reported

โœ… Correctโ€‹

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

Behavior and migration notesโ€‹

This rule reports only and does not provide an autofix.

If you rely on utility side-exports, move them to dedicated modules before enabling this rule at error.

Optionsโ€‹

type Options = [
{
format?: "camelCase" | "kebab-case" | "PascalCase";
},
];

Default:

{
format: "PascalCase";
}

Additional examplesโ€‹

// filename: user-service.ts
// config: { format: "kebab-case" }
export { userService };
// โœ… filename-matching export and no extra exports

ESLint flat config exampleโ€‹

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

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

When not to use itโ€‹

Disable this rule if modules are designed to export a public surface with multiple related symbols from a single file.

Package documentationโ€‹

Rule catalog ID: R013

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.