redhat-developer / vscode-yaml

YAML support for VS Code with built-in kubernetes syntax support
MIT License
661 stars 221 forks source link

Formatting YAML file inserts ternary operators #255

Open disposedtrolley opened 4 years ago

disposedtrolley commented 4 years ago

Hello!

I'm experiencing a very strange bug that's sure to pique your interest. I have a fairly large YAML file, a section of which contains maps like the following:

/api/v2/users/{user_id}/group_memberships/{group_membership_id}.json:
  $ref: paths/v2/users/users_by_user_id_group_memberships_by_group_membership_id.yaml
/api/v2/users/{user_id}/group_memberships/{group_membership_id}/make_default.json:
  $ref: paths/v2/users/users_by_user_id_group_memberships_by_group_membership_id_make_default.yaml
/api/v2/users/{user_id}/groups.json:
  $ref: paths/v2/users/users_by_user_id_groups.yaml
/api/v2/users/{user_id}/identities.json:
  $ref: paths/v2/users/users_by_user_id_identities.yaml
/api/v2/users/{user_id}/identities/{id}.json:
  $ref: paths/v2/users/users_by_user_id_identities_by_id.yaml
/api/v2/users/{user_id}/merge.json:
  $ref: paths/v2/users/users_by_user_id_merge.yaml
/api/v2/users/{user_id}/organization_memberships.json:
  $ref: paths/v2/users/users_by_user_id_organization_memberships.yaml
/api/v2/users/{user_id}/organization_memberships/{id}.json:
  $ref: paths/v2/users/users_by_user_id_organization_memberships_by_id.yaml
/api/v2/users/{user_id}/organization_memberships/{membership_id}/make_default.json:
  $ref: paths/v2/users/users_by_user_id_organization_memberships_by_membership_id_make_default.yaml

When I attempt to format the YAML document by selecting Format Document in the Command Palette, ternary operators (!) are inserted into various locations:

/api/v2/users/{user_id}/group_memberships/{group_membership_id}.json:
  $ref: paths/v2/users/users_by_user_id_group_memberships_by_group_membership_id.yaml
? /api/v2/users/{user_id}/group_memberships/{group_membership_id}/make_default.json
: $ref: paths/v2/users/users_by_user_id_group_memberships_by_group_membership_id_make_default.yaml
/api/v2/users/{user_id}/groups.json:
  $ref: paths/v2/users/users_by_user_id_groups.yaml
/api/v2/users/{user_id}/identities.json:
  $ref: paths/v2/users/users_by_user_id_identities.yaml
/api/v2/users/{user_id}/identities/{id}.json:
  $ref: paths/v2/users/users_by_user_id_identities_by_id.yaml
/api/v2/users/{user_id}/merge.json:
  $ref: paths/v2/users/users_by_user_id_merge.yaml
/api/v2/users/{user_id}/organization_memberships.json:
  $ref: paths/v2/users/users_by_user_id_organization_memberships.yaml
/api/v2/users/{user_id}/organization_memberships/{id}.json:
  $ref: paths/v2/users/users_by_user_id_organization_memberships_by_id.yaml
? /api/v2/users/{user_id}/organization_memberships/{membership_id}/make_default.json
: $ref: paths/v2/users/users_by_user_id_organization_memberships_by_membership_id_make_default.yaml

You can reproduce this by creating a YAML file with the contents of the first code block above, and attempting to format it.

I'm running version 0.6.1 of the extension and version 1.41 of VS Code, on macOS 10.15.2

JPinkney commented 4 years ago

It looks like this bug will need to be reported to https://github.com/prettier/prettier because I can reproduce it on the prettier playground

thejcannon commented 4 years ago

Looks like line length is what triggers it https://github.com/prettier/prettier/issues/7625

maTraxion commented 2 years ago

@JPinkney The issue within Prettier has been resolved within the following pull request: https://github.com/prettier/prettier/pull/10874

This change is published with version 2.3.1, this extension uses version 2.2.1. If you could upgrade the dependency to at least version 2.3.1 that would be greatly appreciated! 😃

dmtbrk commented 2 years ago

I just experienced this bug today. Can confirm that a long line triggered the bug. Would love to see a fix as I'm not familiar with the VSCode extensions development enough to propose one.

agaudreault commented 8 months ago

This should probably be closed by now, but I ended up hitting the same issue with version v1.14.0. The line is formatted to ternary after 80 char.

this_is_a_short_property: |
  some data

this_is_a_long_propertyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyabc:
  |
  some data

? this_is_a_long_propertyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyabcd
: |
  some data