eslint-plugin-jsonc icon indicating copy to clipboard operation
eslint-plugin-jsonc copied to clipboard

Explicit error message of jsonc/sort-keys

Open joseh29 opened this issue 2 years ago • 6 comments

Hello 👋

When using the rule jsonc/sort-keys with more than 2 properties the error message is not clear where the correct placement of the property should be, for instance, imagine the following scenario:

.eslintrc

"jsonc/sort-keys": [
  "error",
  {
    "pathPattern": "^$",
    "order": [
      "prop-one",
      "prop-two",
      "prop-three",
      "prop-four"
    ]
  }
]

some.json

{
  "prop-two": { /* data */ },
  "prop-three": { /* data */ },
  "prop-four": { /* data */ },
  "prop-one": { /* data */ }
}

With the file above, this would yield the following error message:

L:C  error  Expected object keys to be in specified order. 'prop-one' should be before 'prop-four'  jsonc/sort-keys

Which is not incorrect, but it would be much better, to either provide the user the order that should be followed or instead:

(...) 'prop-one' should be before 'prop-two'  jsonc/sort-keys

Otherwise the user will just assume that has to move the property prop-one before the prop-four and the error will be fixed, which is not the case.

Thanks for your time 🙏

joseh29 avatar Sep 16 '22 16:09 joseh29

Thank you for posting this issue. I welcome PR.

ota-meshi avatar Sep 16 '22 18:09 ota-meshi

Thank you for replying.

I forked the repository and used debug to try and find an approach to solve this problem. Here are my conclusions (which may or may not be right):

  • Inside a specific rule there is no information whatsoever about what schema was defined. This means that, there is no access to the order defined by the user in the schema.
  • From what I saw, the auto-fix function just keeps shifting the property and validating over and over until the property is in the right place. In the case presented above, it: 1 - Moves the prop-one to before the prop-four; 2 - Validates the schema; 3 - Schema fails, moves the prop-one to before the prop-three; 4 - Validates the schema; 5 - ...

Are my conclusions about right? My solution would be to pass the schema information to each script rule, in order to see what was defined by the user and have direct access to the order array. Is this possible? 🙏

joseh29 avatar Sep 19 '22 08:09 joseh29

I'm still not quite sure what you're trying to do. So I'm not sure if your changes are right or not. Can you open a PR and share your code?

ota-meshi avatar Sep 20 '22 01:09 ota-meshi

I don't know what changes to make in order to open a PR. I'll try to rephrase it. On the file lib/rules/sort-keys.ts, I wanted to change the function verifyProperty to have access to the User defined schema. In the previous example, I wanted to have access to this object, inside the verifyProperty: image So that I can extrapolate the correct order in which the properties should be in and use this context in the error message. For e.g (this would then be possible, not necessary, but possible):

> (...) 'prop-one' should be before 'prop-two' (...) The order should be 'prop-one' > 'prop-two' > 'prop-three' > 'prop-four'  jsonc/sort-keys

Does this make sense? 🤔

(I believe that, with this type of information, the *fix would be much easier to implement)

joseh29 avatar Sep 20 '22 08:09 joseh29

Any updates on this?

joseh29 avatar Oct 18 '22 09:10 joseh29

Please open a pull request.

ota-meshi avatar Oct 24 '22 06:10 ota-meshi

I aslo had same situation,then i try typing some config to settings.json

"eslint.validate": [
    "javascript",
    "javascriptreact",
    "typescript",
    "typescriptreact",
    "html",
    "vue",
    "markdown",
    "json",
    "jsonc",
    "json5"
  ]

then it's work now!

Hisioni avatar Nov 11 '22 08:11 Hisioni