Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
fasttime opened this issue Nov 2, 2022 · 5 comments · Fixed by #16532
Closed
1 task done

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

fasttime opened this issue Nov 2, 2022 · 5 comments · Fixed by #16532
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects

Comments

@fasttime
Copy link
Member

fasttime commented Nov 2, 2022

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

@fasttime fasttime added bug ESLint is working incorrectly repro:needed labels Nov 2, 2022
@eslint-github-bot eslint-github-bot bot added this to Needs Triage in Triage Nov 2, 2022
@mdjermanovic mdjermanovic moved this from Needs Triage to Triaging in Triage Nov 3, 2022
@mdjermanovic
Copy link
Member

This looks like a bug to me.

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

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
};

The different behavior seems to be because in the first case continuesPropertyGroup considers the two properties to be in the same group, while in the second case they are in separate groups.

function continuesPropertyGroup(lastMember, candidate) {
const groupEndLine = lastMember.loc.start.line,
candidateStartLine = candidate.loc.start.line;
if (candidateStartLine - groupEndLine <= 1) {
return true;
}

Perhaps we could change this logic a bit to fix the bug - if value is not on the same line as key, then property doesn't continue the group?

@mdjermanovic mdjermanovic added rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion repro:yes and removed repro:needed labels Nov 3, 2022
@mdjermanovic mdjermanovic moved this from Triaging to Ready to Implement in Triage Nov 3, 2022
@fasttime
Copy link
Member Author

fasttime commented Nov 5, 2022

Thanks @mdjermanovic, I did some debugging and I think your analysis is correct. I can work on a PR to fix this.
It seems that there are currently no unit tests covering the case of a property where the value starts on a different line than the key, so I guess I will add some.

@snitin315
Copy link
Contributor

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

@fasttime
Copy link
Member Author

fasttime commented Nov 8, 2022

Perhaps we could change this logic a bit to fix the bug - if value is not on the same line as key, then property doesn't continue the group?

This should be fine with align: "value". I'm not sure about align: "colon". So, for instance, this code is currently considered ok by the rule:

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

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"
  ]
});

Any thoughts about this @mdjermanovic?

@mdjermanovic
Copy link
Member

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.

@snitin315 snitin315 moved this from Ready to Implement to Pull Request Opened in Triage Nov 11, 2022
Triage automation moved this from Pull Request Opened to Complete Nov 14, 2022
@eslint-github-bot eslint-github-bot bot locked and limited conversation to collaborators May 14, 2023
@eslint-github-bot eslint-github-bot bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label May 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly repro:yes rule Relates to ESLint's core rules
Projects
Archived in project
Triage
Complete
Development

Successfully merging a pull request may close this issue.

3 participants