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:
- A
VariableDeclaration, FunctionDeclaration OR a ClassDeclaration that contains JSXElement children in a return statement
-
The Primary Component is defined as (in priority order):
- The default export
- The
VariableDeclaration, FunctionDeclaration or ClassDeclaration that matches the filename (i.e. const App might be app.tsx)
- The
VariableDeclaration, FunctionDeclaration or ClassDeclaration that matches the parent directory name if the filename is index.tsx
- The named, exported component (if multiple exist, the first one that appears in the file should be the primary component)
These cases would be valid:
const App = () => {
return <div />;
};
// app.tsx
const App = () => {
return <Row />;
}
const Row = () => {
return <div />;
}
// app/index.tsx
const App = () => {
return <Row />;
}
const Row = () => {
return <div />;
}
const App = () => {
return <Row />;
}
const Row = () => {
return <div />;
}
export default App;
export const App = () => {
return <Row />;
}
const Row = () => {
return <div />;
}
These cases would be invalid:
Component appears before primary component based on filename
// app.tsx
const Row = () => {
return <div />;
}
const App = () => {
return <Row />;
}
Component appears before primary component based on directory structure
// app/index.tsx
const Row = () => {
return <div />;
}
const App = () => {
return <Row />;
}
Component appears before exported component
const Row = () => {
return <div />;
}
const App = () => {
return <Row />;
}
export default App;
Component appears before exported component
const Row = () => {
return <div />;
}
const App = () => {
return <Row />;
}
export { App };
Component appears before exported component
const Row = () => {
return <div />;
}
export const App = () => {
return <Row />;
}
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.
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,FunctionDeclarationOR aClassDeclarationthat containsJSXElementchildren in a return statementThe Primary Component is defined as (in priority order):
VariableDeclaration,FunctionDeclarationorClassDeclarationthat matches the filename (i.e.const Appmight beapp.tsx)VariableDeclaration,FunctionDeclarationorClassDeclarationthat matches the parent directory name if the filename isindex.tsxThese 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.