Gudahtt / prettier-plugin-sort-json

A plugin for Prettier that sorts JSON files by property name.
MIT License
97 stars 12 forks source link

`jsonRecursiveSort` doesn't work / isn't recognized #193

Closed Deckluhm closed 6 months ago

Deckluhm commented 7 months ago

Hello 👋

I'm trying to use this plugin with Prettier 3 and I can't make jsonRecursiveSort option work.

I'm trying to use it in a programmatic way but I also tried with the CLI and I got the following error:

# Error while passing the option as a CLI flag
[warn] Ignored unknown option --json-recursive-sort=true.

# Error while defining option in .prettierrc
[warn] Ignored unknown option { jsonRecursiveSort: true }.

Here is a minimal reproduction using:

https://github.com/Deckluhm/json-recursive-sort-bug

Thank you!

Gudahtt commented 6 months ago

Thanks for the report!

It looks like the problem may be that the plugin has not been loaded. See these instructions on how to load the plugin: https://prettier.io/docs/en/plugins#using-plugins

In older versions of Prettier, plugins would be loaded automatically, but it's explicit from v3 onwards.

Deckluhm commented 6 months ago

It looks like the problem may be that the plugin has not been loaded. See these instructions on how to load the plugin: https://prettier.io/docs/en/plugins#using-plugins

I forgot to import it for CLI but even after adding "plugins": ["prettier-plugin-sort-json"] to .prettierrc it still doesn't work.

It is correctly imported for programmatic usage: https://github.com/Deckluhm/json-recursive-sort-bug/blob/main/index.js#L14

Gudahtt commented 6 months ago

In that example, you're calling format without the jsonRecursiveSort option. This should work:

import { format } from 'prettier'

const formattedJson = await format(
  JSON.stringify({
    hello: 'world',
    nested: {
      b: 'Sunt duis ipsum labore tempor exercitation non.',
      a: 'Officia sit esse nulla aute esse excepteur ad.',
      c: 'Consectetur fugiat ea esse Lorem velit.'
    },
    another: 'one'
  }), {
    parser: 'json',
    plugins: ['prettier-plugin-sort-json'],
    jsonRecursiveSort: true,
  }
)

console.log(formattedJson)

When calling format, it doesn't seem to read from the configuration. You need to specify options manually.

In the repository you shared, the format npm script does work correctly, but there is no --check or --write flag passed in. You need to pass in --check to get it to fail for unsorted files, or --write to get it to sort the files for you.

Gudahtt commented 6 months ago

I'll close this for now, as it does seem to work when I've tested it. Let me know if you are still having trouble with this

Deckluhm commented 6 months ago

Thanks for looking into it and sorry!

I was expecting .prettierrc to work with programmatic usage but it doesn't seem to be the case :shrug:

Gudahtt commented 6 months ago

No problem at all! I appreciate the clear reproduction, really helpful report, even if it didn't find a bug