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.

Bug: `key-spacing` with `align: "value"` produces incorrect alignment

See original GitHub issue

Environment

Node version: v19.0.0 npm version: v8.19.2 Local ESLint version: v8.26.0 Global ESLint version: Not found Operating System: darwin 22.1.0

What parser are you using?

Default (Espree)

What did you do?

/* eslint "key-spacing": ["error", { align: "value" }] */
var foo =
{
  code: 1,
  message:
  "some value on the next line",
};

DEMO

What did you expect to happen?

No error.

What actually happened?

Got error

4:9 Missing space before value for key ‘code’. (key-spacing)

Autofix adds three spaces after code: to align the value 1 one space after message:, which doesn’t seem correct since there is no other value to align with.

If the order of the properties is swapped, as expected, no error is reported.

/* eslint "key-spacing": ["error", { align: "value" }] */
var foo =
{
  message:
  "some value on the next line",
  code: 1
};

DEMO

Participation

  • I am willing to submit a pull request for this issue.

Additional comments

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mdjermanoviccommented, Nov 9, 2022

But if we change the logic such that a value on a different line doesn’t continue the group, then the rule would report a problem, and the corrected output would be:

/* eslint "key-spacing": ["error", { align: "colon" }] */
({
  command: "npm",
  arguments:
  [
    "test"
  ]
});

I think that’s fine. Per the documentation (emphasize mine):

"colon" enforces horizontal alignment of both colons and values in object literals.

Since the value starts on another line, values wouldn’t be aligned so this looks similar to the problem with align: "value". I think it’s correct to always consider that such property does not belong to the preceding group.

1reaction
snitin315commented, Nov 6, 2022

@fasttime Thanks for taking this up. Yes, you will have to add corresponding test cases in your PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[key-spacing] Allow for "alignment" spacing configuration #5613
I'm fine with on as the key name for align, "align on colons" or "align on values" makes sense to me. Your proposal...
Read more >
key-spacing - ESLint - Pluggable JavaScript Linter
"align" with an object value allows for fine-grained spacing when values are being aligned in object literals. "singleLine" specifies a spacing style for...
Read more >
Incorrect alignment and formatting of Table of Contents on Word
- Unselected and selected the "right align page numbers" on the Custom ToC box. Anyone can help me fixing this? Disclaimer: I'm not...
Read more >
How to handle symbol not found compile error in java? [closed]
One solution is to add a default return statement at the bottom of the method, but if this were my code, I'd chuck...
Read more >
Aligning equations when the number of columns change - TeX
Use gather* , and nest an aligned enviroinment within it: \documentclass{article} \usepackage{amsmath} \usepackage{amsthm} \begin{document} ...
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