eslint enforce space after the single-line-if parentheses

Viewed 493

I'm quite new to ESLint and I couldn't find a rule that does what I want. Consider the following code:

if(someCondition)i++;

I want to enforce a single space after the parentheses, so that we should have the following instead:

if(someCondition) i++;

However I simply couldn't find such a rule; it's not space-before-function-paren (obviously), keyword-spacing (which only affects the spacing after if) or space-before-blocks (since this is a single-line-if, there's no block). Please help. Thanks!

P.S. Same rule should apply to single-line-while as well.

2 Answers

Simple, but effective:

$ cat input 
  if (condition)i++;
  if (condition)i--
  
  while ( this ^ foo || bar )continue
  for (true ||
         true )z--

Naive solution, but probably ok.

$ cat input | busybox sed -e 's/)\([a-zA-Z_]\)/) \1/'
if (condition) i++;
if (condition) i--

while ( this ^ foo || bar ) continue
for (true ||
       true ) z--

This will put a space between ) and the first letter of a variable name (a-z_).

Consider my answer here (and others') for tips on how to process all files that contain the undesired pattern:

Related