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

JSON6

Open brettz9 opened this issue 4 years ago • 8 comments

Looks like a great tool...

Might you add tests and docs indicating support for JSON6 if it may work? https://github.com/d3x0r/JSON6

brettz9 avatar Jul 12 '20 10:07 brettz9

Thank you for this issue!

I didn't know JSON6 so I read the documentation. JSON6 does not seem to be a syntax that extends JSON by incorporating ES syntax.

e.g.

{`a`:123}

https://github.com/d3x0r/JSON6/blob/cce1dd3faf8ba551d36ec7917610c2bbea83a778/test/json6Test.js#L207

If JSON6 has a syntax that is unrelated to ES syntax, I think it difficult to support JSON6 with this plugin.

ota-meshi avatar Jul 13 '20 10:07 ota-meshi

Right, it is a superset, but it could still be useful to lint against the ES-safe subset of that superset. (I've asked at https://github.com/d3x0r/JSON6/issues/44#issuecomment-657477928 whether the ES-safe subset could be enforced at the level of the parser, but even if not, I'd think a caveat that the plugin only supports the safe subset might be enough.)

brettz9 avatar Jul 13 '20 10:07 brettz9

Perhaps supporting some JSON6 syntax can be a useful case even for users who don't use JSON6.

Currently, the parser provided by this plugin treats AST that cannot be parsed by JSON5 as a parsing error. I think it would be useful for people to change the parser to accept static values and add new rules so that they can be autofix.

For example, autofix {foo: `bar`} with the new rule jsonc/no-template-literals will convert it to {foo: "bar"}. More, it is converted to {"foo": "bar"} by the existing jsonc/quote-props rule. This is useful when replacing .xxxrc.js to .xxxrc.json.

I probably need to make the following changes:

  • The parser allows static template literals.
  • The parser allows undefined identifier.
  • The parser allows empty slots in array literals.
  • Add jsonc/no-template-literals rule.
  • Add jsonc/no-undefined rule.
  • Add jsonc/no-sparse-arrays rule.

I will work on these when I have time.

However, the following JSON6 syntax still cannot be parsed:

  • back-tick quoted key.
  • multiple minus signs.
  • underscores separating digits. (current espree cannot parsed it.)

(I'm not familiar with it, so there may be others.)

ota-meshi avatar Jul 13 '20 13:07 ota-meshi

Re: underscores separating digits, just as an FYI, it looks like there is https://babeljs.io/docs/en/babel-plugin-syntax-numeric-separator for the syntax alone, and https://babeljs.io/docs/en/babel-plugin-proposal-numeric-separator if transforming it, so if users applied the plugin with babel-eslint, I'd think they could get that feature. I imagine it should come to espree eventually, however.

brettz9 avatar Jul 14 '20 01:07 brettz9

I know that babel-eslint can parse Stage 3 syntax. However, I'm not understand of the side effects as JSON due to the other Stage3 syntax by making babel-eslint available. So, I still want to support only syntax that espree can parse.

If I have the time to learn a lot of syntax it might support it, but maybe it's faster to change the Numeric Separators to Stage 4 😅

ota-meshi avatar Jul 14 '20 02:07 ota-meshi

FWIW, I've filed https://github.com/d3x0r/JSON6/issues/46 which also indicates a few other features where JSON6 diverges from ES.

brettz9 avatar Jul 14 '20 08:07 brettz9

I changed the parser to allow some syntax, added rules to report the syntax allowed by the parser, and released it. https://github.com/ota-meshi/eslint-plugin-jsonc/releases/tag/v0.2.0

However, I do not yet provide a ruleset for JSON6. Now, additional configuration is required to make this plugin work with .json6. https://ota-meshi.github.io/eslint-plugin-jsonc/user-guide/#faq

I'm learning the syntax of JSON6 while also referring to the issue you posted in JSON6 repo.

ota-meshi avatar Jul 16 '20 03:07 ota-meshi

Excellent, thanks!

If you find any, I'd be most grateful if you could mention on the issue any areas where JSON6 differs from ES (besides those I've mentioned).

brettz9 avatar Jul 16 '20 03:07 brettz9