Skip to content
This repository was archived by the owner on Aug 20, 2026. It is now read-only.

Commit 75e1bf2

Browse files
committed
chore(lint): ban index.* filenames outside the src/index.ts barrel
1 parent 38842ce commit 75e1bf2

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

eslint.config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,31 @@
11
import js from '@eslint/js';
22
import tseslint from 'typescript-eslint';
33
import globals from 'globals';
4+
import type { Rule } from 'eslint';
5+
6+
// Forward guard: only src/index.ts may use an index.* basename. Any future module named index.ts/cts/mts/js/cjs/mjs outside the public convenience barrel is a mistake (a hidden entry point the exports map does not advertise). ESLint 9+ guarantees context.filename; the optional getFilename fallback covers an older runtime only.
7+
const noNonBarrelIndex: Rule.RuleModule = {
8+
meta: {
9+
type: 'problem',
10+
schema: [],
11+
messages: {
12+
barrel:
13+
'Only src/index.ts may be named index.* (the public convenience barrel); give any other module a descriptive filename.',
14+
},
15+
},
16+
create(context: Rule.RuleContext & { getFilename?: () => string }) {
17+
const filename = context.filename ?? context.getFilename?.() ?? '';
18+
const lastSlash = filename.lastIndexOf('/');
19+
const basename = lastSlash === -1 ? filename : filename.slice(lastSlash + 1);
20+
if (!/^index\.[cm]?[tj]s$/.test(basename)) return {};
21+
if (filename.endsWith('/src/index.ts')) return {};
22+
return {
23+
Program(node) {
24+
context.report({ node, messageId: 'barrel' });
25+
},
26+
};
27+
},
28+
};
429

530
export default tseslint.config(
631
js.configs.recommended,
@@ -13,6 +38,10 @@ export default tseslint.config(
1338
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
1439
},
1540
},
41+
{
42+
plugins: { local: { rules: { 'no-non-barrel-index': noNonBarrelIndex } } },
43+
rules: { 'local/no-non-barrel-index': 'error' },
44+
},
1645
{
1746
ignores: ['dist/**', 'node_modules/**'],
1847
},

0 commit comments

Comments
 (0)