eslint-plugin-jsonc
eslint-plugin-jsonc copied to clipboard
Explicit error message of jsonc/sort-keys
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 🙏
Thank you for posting this issue. I welcome PR.
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 theprop-one
to before theprop-four
; 2 - Validates the schema; 3 - Schema fails, moves theprop-one
to before theprop-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? 🙏
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?
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
:
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)
Any updates on this?
Please open a pull request.
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!