question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Max nesting/indentation

See original GitHub issue

Please describe what the rule should do:

What category of rule is this? (place an “X” next to just one item)

[ ] Warns about a potential error (problem) [ ] Suggests an alternate way of doing something (suggestion) [x] Enforces code style (layout) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

Assuming a configured max setting of 6

function fn() {
    for (;;) { // Nested 1 deep
        while (true) { // Nested 2 deep
            if (true) { // Nested 3 deep
                if (true) { // Nested 4 deep
                    React.createElement('div', {}, // Nested 5 deep
                        React.createElement('div', {}) // Nested 6 deep
                    );
                }
            }
        }
    }
}

function fn() {
    for (;;) { // Nested 1 deep
        while (true) { // Nested 2 deep
            if (true) { // Nested 3 deep
                if (true) { // Nested 4 deep
                    <div> {/* Nested 5 deep */}
                        <div></div> {/* Nested 6 deep */}
                    </div>
                }
            }
        }
    }
}

function fn() {
    for (;;) { // Nested 1 deep
        while (true) { // Nested 2 deep
            <div> {/* Nested 3 deep */}
                <div> {/* Nested 4 deep */}
                    {(() => { // Nested 5 deep
                       'Hello, World!' // Nested 6 deep
                    })}
                </div>
            </div>
        }
    }
}

Why should this rule be included in ESLint (instead of a plugin)?

  • ESLint already has a max-depth rule
  • The React plugin already has a jsx-max-depth rule

However, neither of these will help to prevent nested function calls, where the functions may be a mixture of normal functions, React.createElement, or JSX—as often happens inside React components (although this isn’t really a React specific problem).

Readability is difficult when things are deeply nested. The solution is to extract nested expressions into functions.

Are you willing to submit a pull request to implement this rule? Yes

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kaicataldocommented, Dec 16, 2019

No, that would just be for CallExpressions (so it would catch the React.createElement() calls). If we add support for JSX as well, I think we would need to add that as a separate option.

I actually think it would be best to separate out the discussion of JSX into a separate issue/proposal, because that seems significantly more complex and might require some more discussion.

1reaction
OliverJAshcommented, Dec 16, 2019

Good catch, I meant to call that as a IIFE. Contrived example but hopefully you get the idea

Read more comments on GitHub >

github_iconTop Results From Across the Web

fpt Reference: INDENT EACH LEVEL OF NESTING
This command specifies the number of spaces by which code is indented for each level of nesting. The MAXIMUM INDENT command specifies the...
Read more >
Python: How many levels of nested indentation is your limit?
For me in any programming language an indentation level of about 4 would be the maximum I would use, and even that I...
Read more >
max-nesting-depth - Stylelint
This rule integrates into Stylelint's core the functionality of the (now deprecated) plugin stylelint-statement-max-nesting-depth .
Read more >
Nesting Levels and Indenting - IBM
Indent three columns for each nesting level. Begin all SPMs in a particular structure (at a particular nesting level) in the same column....
Read more >
c++ - Levels of Indentation - Stack Overflow
If a function has more than 4 or 5 nested if/switch/loop/try statements, ... This tends to limit you to maybe 4 levels max...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found