eslint: curly 'multi' autofix produces syntax errors with lexical declarations
Tell us about your environment
- ESLint Version: 6.0.1 (same with 5.16.0)
- Node Version: 10.16.0
- npm Version: 6.9.0
What parser (default, Babel-ESLint, etc.) are you using?
default
Please show your full configuration:
Configuration
module.exports = {
parserOptions: {
ecmaVersion: 6,
},
rules: {
"curly": ["error", "multi"]
}
};
What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.
if (foo) {
let bar;
}
eslint index.js --fix
What did you expect to happen?
No errors, similar to how multi-or-nest
works.
What actually happened? Please include the actual, raw output from ESLint.
if (foo)
let bar; // SyntaxError: Lexical declaration cannot appear in a single-statement context
Are you willing to submit a pull request to fix this bug?
Yes.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (16 by maintainers)
Commits related to this issue
- Fix: curly `multi` reports single lexical declarations (fixes #11908) — committed to eslint/eslint by mdjermanovic 5 years ago
- Fix: curly `multi` reports single lexical declarations (fixes #11908) (#12513) — committed to eslint/eslint by mdjermanovic 5 years ago
It’s most likely a bad practice to have a function declaration anywhere other than directly in the body.
Perhaps ESLint should never modify such code, i.e. never move function declarations to
{}
or out from its existing{}
, because it can produce some unpredictable differences even if it isn’t a syntax error.Ok, I’m working on this.
It might take some time, all options have to be checked and the
consistent
modifier adds some complexity.Agreed - better to play it safe than potentially do something unsafe.
Yes, and
class
andfunction
shouldn’t be fixed as well.So:
const
,let
,class
andfunction
First three are syntax errors.
function
might depend on the parser, I’ll check it more, but it certainly isn’t a good idea to move it up from braces.