javascript icon indicating copy to clipboard operation
javascript copied to clipboard

(question) eslint rule / plugin for guideline 7.15

Open shelbyspeegle opened this issue 7 years ago • 11 comments

is there an eslint rule or plugin that enforces 7.15?

shelbyspeegle avatar Feb 13 '18 17:02 shelbyspeegle

Indeed! comma-dangle handles the trailing comma, and function-paren-newline, i believe, handles the rest.

comma-dangle should be linked here already; however, I'm still not 100% sure about function-paren-newline :-)

ljharb avatar Feb 18 '18 05:02 ljharb

awesome! thanks @ljharb.

i can't seem to find the right options for function-paren-newline that adheres to the airbnb eslint guideline 7.15.

in particular, i can't get eslint to bug me about the 'bad' example below.

// bad
console.log(foo,
  bar,
  baz);

// good
console.log(
  foo,
  bar,
  baz,
);

shelbyspeegle avatar Feb 22 '18 17:02 shelbyspeegle

Maybe we need to file something upstream on eslint about it?

ljharb avatar Feb 22 '18 18:02 ljharb

@shelbyspeegle I tried both rules out and I'm getting both errors, the bad example included.

"comma-dangle": "error",
"function-paren-newline": "error"

What have you tried so far?

raunofreiberg avatar Mar 05 '18 20:03 raunofreiberg

thanks @raunofreiberg, that gets me close, unfortunately it allows these scenarios:

// bad?
console.log(
  foo,
  bar, baz,
  yo,
);

// bad?
console.log(
  foo, bar, baz,
  yo,
);

i assumed that this should produce an eslint error, but an example isnt provided for this type of case.

i did see this in the guide language that could suggest that this should error.

with each item on a line by itself

shelbyspeegle avatar Mar 06 '18 23:03 shelbyspeegle

Yes, that should be an error. Either the entire invocation fits on one line, or each argument is on a line by itself.

ljharb avatar Mar 07 '18 03:03 ljharb

Doesn't the "multiline" option on function-paren-newline along with the current "comma-dangle" implement 7.15 correctly? If so, this line should be updated. Edit: I can open a PR, but I want to verify this is correct first. Also, I think it should be "multiline-arguments" not "multiline" so as to allow line breaks before and after argument lists with a single item.

Cosmic-Ryver avatar Sep 10 '21 00:09 Cosmic-Ryver

@GusBuonv That works great for me!

For this test file, eslint errors show: image

... and eslint --fix now properly resolves that to: image

Using your suggested rules in this simple eslintrc.js.

module.exports = {
  extends: ['airbnb'],
  rules: {
    'function-paren-newline': ['error', 'multiline'],
  },
};

Thanks @GusBuonv!

shelbyspeegle avatar Sep 12 '21 22:09 shelbyspeegle

Reading the rule docs, it does seem like multiline-arguments is the option that would most closely match the guide's intent.

It was changed from "multiline" to "consistent" in 2018. eslint itself added multiline-arguments in 2019, so it seems like "consistent" was better than "multiline", but "multiline-arguments" may be better still.

I'm hesitant to make this change, however, until it's at least been verified on airbnb's own code. I'll reach out to folks there and see how it goes.

ljharb avatar Sep 13 '21 05:09 ljharb

Given that this rule is disabled by eslint-config-prettier, Airbnb won't be affected, so I think this is a reasonable semver-minor change to make. @GusBuonv please make that PR :-)

ljharb avatar Sep 13 '21 19:09 ljharb

@ljharb Done! If the PR is merged, it may still be a good idea to keep this issue open or open a new one. I assume 7.15 is also supposed to apply to argument destructuring. If it is meant to apply in this case, it is still not properly enforced, and current core ESLint rules don't provide a way to achieve the desired behavior.

Cosmic-Ryver avatar Sep 15 '21 02:09 Cosmic-Ryver