Search code examples
javascripteslinttypescript-eslint

How should I configure eslint so that all if and for statements are bracketed and newline


I don't like the following style of code


const flag = Math.random() > 0.5;

if (flag) console.log('A');
else console.log('B');

for (let index = 0; index < 999; index++) console.log(index);

I hope to restrict their behavior through eslint, so that if statements and for statements must have parentheses and newlines

const flag = Math.random() > 0.5;

if (flag) {
  console.log('A');
} else {
  console.log('B');
}

for (let index = 0; index < 999; index++) {
  console.log(index);
}

How should I configure my eslint?


Solution

  • The curly rule is what you are searching for:

    https://eslint.org/docs/latest/rules/curly