Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule proposal: no-dangling-else - Disallow dangling else ambiuguity #2515

Open
Gr3q opened this issue Dec 23, 2024 · 2 comments
Open

Rule proposal: no-dangling-else - Disallow dangling else ambiuguity #2515

Gr3q opened this issue Dec 23, 2024 · 2 comments

Comments

@Gr3q
Copy link

Gr3q commented Dec 23, 2024

Description

Detect Dangling else ambiguity, especially around if statements without brackets.

Fail

if (statement1)
    if (statement2)
       console.log("statement2")
else
    if (statement3)
        console.log("statement3")
if (statement1)
    if (statement2)
       console.log("statement2")

Pass

if (statement1) {
    if (statement2)
       console.log("statement2")
}
else {
    if (statement3)
        console.log("statement3")
}

if (statement1) {
    if (statement2)
       console.log("statement2")
    else
        if (statement3)
            console.log("statement3")
}
if (statement1) {
    if (statement2)
       console.log("statement2")
}

Proposed rule name

no-dangling-else

Additional Info

The allowed case should be "Disallowing the statement following a "then" to be an "if" itself (it may however be a pair of statement brackets containing only an if-then-clause)." - Taken from Avoiding_ambiguity_by_changing_the_syntax section in the Wikipedia article.

While there are other ways to prevent this problem like enforcing braces or enforcing formatting rules to make the problem obvious, nothing I found helps finding mistakes like these (which cause bugs in logic and needs to be fixed by a human) in an existing codebase.

e.g. Eslint's curly would use the latter accepted form when fixing, while that is how it's interpreted by JavaScript, there was an ambiguity when the code was written.

@sindresorhus
Copy link
Owner

I think not using braces for conditionals is an anti-pattern, which would have avoided situations like this, so I'm 👎 on this.

@Gr3q
Copy link
Author

Gr3q commented Dec 23, 2024

I agree. Still, fixing all cases of this type of issue in an existing codebase is a must before one can adopt stricter brace rules, especially because of the ambiguity it should not be auto-fixed by other tools requiring braces (the auto-fix part is obviously not the scope of this rule).

Nevertheless if you chose not to add this rule, for posterity one can find all cases of this issue with no-restricted-syntax .

{
         selector: "IfStatement[consequent.type='IfStatement'] > [consequent]",
         message: "This statement is ambiguous, please use block statements (brackets)."
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants