Skip to content
Esc

noctcore-architecture/file-naming-pattern

In a folder matched by folders, a file’s basename must match pattern, a regular expression built from the folder’s name.

Many codebases name the files of a folder after the folder: account/account.routes.ts, account/account.schemas.ts. Enforcing that with one no-restricted-syntax entry per folder is long and its messages say little. This rule takes one pattern with a {folder} placeholder, applies it to every folder a glob matches, and the message names the pattern the file should have matched.

Left out of recommended and strict. It does nothing until you set folders and pattern, so a preset entry would read as coverage the project does not have.

For a file whose immediate parent folder matches folders, the rule replaces every {folder} in pattern with that folder’s name (escaped), and reports the file once, on its first line, when the basename does not match the result. Renaming the file is the fix.

Incorrect · src/account/routes.ts
// rule options: {"folders":["**/src/*"],"pattern":"^{folder}(\\.[a-z-]+)?\\.ts$"}
export const routes = [];
Correct · src/account/account.routes.ts
// rule options: {"folders":["**/src/*"],"pattern":"^{folder}(\\.[a-z-]+)?\\.ts$"}
export const routes = [];
Correct · src/account/account.ts
// rule options: {"folders":["**/src/*"],"pattern":"^{folder}(\\.[a-z-]+)?\\.ts$"}
export const account = {};
  • Anything, until both folders and pattern are set.
  • A file whose immediate parent folder does not match folders. Only that one folder is tested, never a deeper ancestor.
  • A file matched by allowIn, such as **/index.ts.
  • A folder name is matched literally: a folder named a.b does not accept aXb.ts.
Option Type Default Meaning
folders string[] [] Globs matched against the folder that directly contains the file. Empty = the rule is off.
pattern string (none) Regular expression source for the basename; every {folder} is replaced by the escaped parent folder name. Unset = the rule is off.
allowIn string[] [] Globs of files exempt even when their folder matches folders.

An invalid pattern is reported as a configuration problem on the first line of each file it would apply to, instead of stopping the lint run.

'noctcore-architecture/file-naming-pattern': ['error', {
folders: ['src/modules/*'],
pattern: '^{folder}(\\.[a-z-]+)?\\.ts$',
allowIn: ['**/index.ts'],
}]

If your folders do not name their files after themselves, or the convention differs per folder, one pattern will not describe your layout. Leave it off.