52 lines
1.6 KiB
JavaScript
52 lines
1.6 KiB
JavaScript
/**
|
|
* @file no-index-files.js
|
|
* Bans index.ts/index.tsx files - they hide dependencies and make code harder to navigate
|
|
*/
|
|
|
|
module.exports = {
|
|
meta: {
|
|
type: 'problem',
|
|
docs: {
|
|
description: 'Ban index.ts/index.tsx files - use explicit imports instead',
|
|
category: 'Best Practices',
|
|
recommended: true,
|
|
},
|
|
fixable: null,
|
|
schema: [],
|
|
messages: {
|
|
indexFile: 'Index files are banned. Use explicit imports and barrel exports. Example: Instead of "import { foo } from "./", use "import { foo } from "./foo" and export from "./foo" explicitly.',
|
|
},
|
|
},
|
|
|
|
create(context) {
|
|
const filename = context.getFilename();
|
|
|
|
// Check if file is named index.ts or index.tsx
|
|
const isIndexFile = /(^|\/|\\)index\.(ts|tsx)$/.test(filename);
|
|
|
|
// Allow index files in root directories that are required by framework
|
|
const allowedPaths = [
|
|
'apps/website/app/index.ts', // Next.js app router entry
|
|
'apps/website/pages/index.tsx', // Next.js pages router entry
|
|
'apps/website/src/index.ts', // Common entry point
|
|
'apps/website/index.ts', // Root entry
|
|
];
|
|
|
|
// Check if the filename ends with any of the allowed paths
|
|
// The filename is an absolute path, so we need to check if it contains the relative path
|
|
const isAllowedPath = allowedPaths.some(allowedPath => {
|
|
return filename.endsWith(allowedPath) || filename.includes('/' + allowedPath);
|
|
});
|
|
|
|
if (isIndexFile && !isAllowedPath) {
|
|
context.report({
|
|
node: null, // Report on the file level
|
|
loc: { line: 1, column: 0 },
|
|
messageId: 'indexFile',
|
|
});
|
|
}
|
|
|
|
return {};
|
|
},
|
|
};
|