bennycode / sort-everything

VS Code extension to sort JSON files.
https://marketplace.visualstudio.com/items?itemName=bennycode.sort-everything
MIT License
10 stars 3 forks source link

Comments and space are removed when sorting in yaml #1

Closed ffflorian closed 2 years ago

ffflorian commented 2 years ago

Given I have a yaml file with unsorted lines:

variables:
  MY_VARIABLE: 'true'    # Comment 1
  OTHER_VARIABLE: 'true' # Comment 2
  BEST_VARIABLE: 'true'  # Comment 3

I select them and run Sort Everything, I receive this:

variables:
BEST_VARIABLE: "true"
MY_VARIABLE: "true"
OTHER_VARIABLE: "true"

but I was expecting to receive this:

variables:
  BEST_VARIABLE: 'true'  # Comment 3
  MY_VARIABLE: 'true'    # Comment 1
  OTHER_VARIABLE: 'true' # Comment 2

Whitespace being removed is a bit annoying, too, but nothing serious. But I don't want to lose those precious comments! 🙂

Btw: I am using Sort Everything on a daily basis for the last few weeks and beside this bug it's been working flawlessly!

bennycode commented 2 years ago

Thanks for your nice report!

Sorting YAML files is accomplished using "js-yaml". I just asked the library authors to support preserving comments: https://github.com/nodeca/js-yaml/issues/689

Once this feature is available in "js-yaml" I will update my plugin.

ffflorian commented 2 years ago

Perfect, thanks!

ffflorian commented 2 years ago

Seems like the authors don't have the resources to implement this and we are asked to use https://github.com/eemeli/yaml instead.

bennycode commented 2 years ago

The "yaml" package supports preserving comments thanks to full AST parsing but it doesn't support sorting. Luckily another user built sorting and shared how to make it work using "yaml": https://github.com/eemeli/yaml/issues/44#issuecomment-571696527

EDIT: Looks like it is part of their stable versions now -> https://github.com/eemeli/yaml/blob/6e0071179b36329a4d7c98b006c3f838269756d2/tests/doc/stringify.js#L611-L613

ffflorian commented 2 years ago

I tried using sortMapEntries but to no avail. So I went ahead and used the deep sort algorithm instead and created a fix, see #2 🙂

bennycode commented 2 years ago

@ffflorian are you sure that sortMapEntries is not available? For me it seems to work:

const YAML = require('yaml');

const obj = { b: 2, a: 1, c: 3 };

console.log(YAML.stringify(obj, { sortMapEntries: true })); // "a: 1\nb: 2\nc: 3\n"
ffflorian commented 2 years ago

@ffflorian are you sure that sortMapEntries is not available?

The function is available but I wasn't able to receive a sorted Document when using sortMapEntries: true.

bennycode commented 2 years ago

Indeed. It looks like sortMapEntries only works when being used with plain objects (instead of Document.Parsed or string). This has the side effect that comments are being thrown away (because JSON objects don't have comments):

import {parseDocument, stringify} from 'yaml';

// ...

const document = parseDocument(text);
console.log(stringify(document.toJSON(), {sortMapEntries: true}));