question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[array-bracket-newline] doesn't handle inline comments properly

See original GitHub issue

Tell us about your environment

  • ESLint Version: 6.5.1
  • Node Version: 10.15.0
  • npm Version: 6.4.1

What parser (default, Babel-ESLint, etc.) are you using?

@typescript-eslint/parser

Please show your full configuration:

Here it is, just uncomment the “array-bracket-newline” rule.

What did you do? Please include the actual source code causing the issue, as well as the command that you used to run ESLint.

const asd = [ // comment
  123,
  123
];
npx eslint src --ext '.ts, .tsx'

What did you expect to happen?

No error detected.

What actually happened? Please include the actual, raw output from ESLint.

Adding an inline comment like that trips this rule, the array is actually consistently written as far as I’m concerned but the rule is kind of considering the inline comment as an item of the array.

Are you willing to submit a pull request to fix this bug?

I’m not too familiar with the AST, but with some pointers perhaps I can help.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Oct 15, 2019

I’m working on this.

1reaction
mdjermanoviccommented, Oct 14, 2019

This is related to the consistent option only.

Online Demo Link

/*eslint array-bracket-newline: ["error", "consistent"]*/

const asd = [ // comment
  123,
  123
];
3:13 - There should be no linebreak after '['. (array-bracket-newline)
6:1 - There should be no linebreak before ']'. (array-bracket-newline)

This looks like a bug to me. The above example is auto-fixed to:

/*eslint array-bracket-newline: ["error", "consistent"]*/

const asd = [ // comment
  123,
  123];

where the rule still reports the first error. This could be manually fixed to not report the error like this:

/*eslint array-bracket-newline: ["error", "consistent"]*/

const asd = [123, // comment
  123];

or like this:

/*eslint array-bracket-newline: ["error", "consistent"]*/

const asd = [ 
  // comment
  123,
  123
];

I don’t think that the rule intends to enforce neither of those fixes, given how the other options work. In particular, always doesn’t report an error for the same example:

/*eslint array-bracket-newline: ["error", "always"]*/

const asd = [ // comment
  123,
  123
];

It seems inconsistent that always sees the same example as two correct linebreaks after/before the brackets, while consistent doesn’t.

The code for the consistent option checks if there is a linebreak between [ and the first token inside (including comments). Based on the result, it requires/disallows linebreaks between brackets and other tokens (not including comments).

Couldn’t find anything specific about this in #9136 and #9206 where the option is implemented and there are no test cases with the consistent option and some comments.

I think that this should be accepted as a bug.

Bug fix would be to check for a linebreak between the [ and the first non-comment token. I’m not 100% sure but it seems that this fix even can’t produce more errors in other cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

array-bracket-newline - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
JavaScript braces on new line or not? [closed] - Stack Overflow
I'd like to follow them for consistence among other JavaScript projects, but doesn't K&R style look more readable? Note: We know the problem ......
Read more >
PHP Coding Standards - WordPress Developer Resources
Brace Style; Declaring Arrays; Multiline Function Calls; Type declarations ... Also see the PHP Inline Documentation Standards for further guidelines.
Read more >
Configuration · Scalafmt - Scalameta
This parameter controls handling of newlines after the arrow following the parameters of a curly brace lambda or partial function, and whether a...
Read more >
bash multi line command with comments after the continuation ...
Comments end at the first newline (see shell token recognition rule 10), without allowing continuation lines, so this code has foo in a ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found