Skip to content

Rule: main-comp-first #56

Description

@brandongregoryscott

Implement a new rule with restrictions on multiple components in a file.

Multiple components are allowed per file if the primary component appears first

  1. A Component is defined as:

    1. A VariableDeclaration, FunctionDeclaration OR a ClassDeclaration that contains JSXElement children in a return statement
  2. The Primary Component is defined as (in priority order):

    1. The default export
    2. The VariableDeclaration, FunctionDeclaration or ClassDeclaration that matches the filename (i.e. const App might be app.tsx)
    3. The VariableDeclaration, FunctionDeclaration or ClassDeclaration that matches the parent directory name if the filename is index.tsx
    4. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions