Open brandongregoryscott opened 2 years ago
According to the ESLint Architecture documentation, accessing the file-system is frowned upon. For the basic rule (i.e. restricting 1 file <> 1 component), I'm not sure what else an autofix could do, and this would be equivalent to react/no-multi-comp
.
For the variation of the rule that enforces the primary component appears before any other components, we could still provide an autofix that would move that node after the primary component. Some of that logic is in progress in this branch: https://github.com/brandongregoryscott/eslint-plugin-collation/tree/rule-multi-comp but I think I'd like to rename the rule and focus it more toward that behavior, if moving forward with it
Implement a new rule with restrictions on multiple components in a file.
Multiple components are allowed per file if the primary component appears first
A Component is defined as:
VariableDeclaration
,FunctionDeclaration
OR aClassDeclaration
that containsJSXElement
children in a return statementThe Primary Component is defined as (in priority order):
VariableDeclaration
,FunctionDeclaration
orClassDeclaration
that matches the filename (i.e.const App
might beapp.tsx
)VariableDeclaration
,FunctionDeclaration
orClassDeclaration
that matches the parent directory name if the filename isindex.tsx
These cases would be valid:
These cases would be invalid:
Component appears before primary component based on filename
Component appears before primary component based on directory structure
Component appears before exported component
Component appears before exported component
Component appears before exported component
Autofix behavior Still need to figure out if it's possible to extract out a node into a new file via ESLint, or if that would be too heavy-handed.