Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Rule: Format JSON files as JSON.stringify(x, null, 2) would #903

Closed
kemitchell opened this issue May 31, 2017 · 3 comments

Comments

@kemitchell
Copy link

commented May 31, 2017

I'd be interested in adding a rule to standard that checks the formatting of JSON files. In particular, I'd like to add a rule that checks that JSON files have been formatted with JSON.stringify(x, null, 2).

The most important "target" for the rule will often be package.json. Consistency there would help reduce diff size and awkward patches. I believe npm@5 recently added autodetection of indentation in package.json for much the same reason, as npm install --save and similar could change indentation in prior versions.

The rule would also apply to other JSON files in the working tree. Often, these files are treated exactly like source code, brought in with require('./somefile.json').

The JSON.stringify algorithm is very straightforward and predictable. I believe it is also mostly consistent with standard's rules for object literals, like a single space after comma.

Other JSON formatting algorithms arguably do better in terms of readability. I wrote one, aping LISP style that puts a great deal more data on screen at a time. Others have implemented ML-esque, comma-first style, which trades readability for minimal diffs.

I've always ended up going back to JSON.stringify. It's built into the language. It's intuitive enough that folks can match its output manually if they have to. It strikes a decent compromise between readability and minimal diff. It is, for all those reasons, the de facto "standard". It's also dead simple to "fix" JSON formatted otherwise automatically.

Some maintainers might like to go further, and enforce something like json-stable-stringify, which sorts keys. I'd be tempted to do that in some of my own projects, but it doesn't feel "standard". Moreover, where I'd want that kind of hard consistency, I'd probably also want array sorting and other changes that affect semantics, not just syntax.

@Flet

This comment has been minimized.

Copy link
Member

commented Jun 8, 2017

Formatting JSON files is an interesting idea!

Currently standard is a curated set of eslint rules, so its capabilities are tied to rules that are exposed via eslint (or rules we've built in custom plugins). standard also is hard coded to scan only JS and JSX files currently.

I'm not sure if eslint supports parsing JSON files, and I don't see any built-in rules around JSON in eslint's documentation. These would be significant challenges to overcome to get this to work.

I think a separate formatting tool might make sense! I don't know if it makes sense to build this into standard at the moment.

@kemitchell

This comment has been minimized.

Copy link
Author

commented Jun 8, 2017

@Flet, is this really something I should propose for eslint?

@stale

This comment has been minimized.

Copy link

commented May 10, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

@stale stale bot added the stale label May 10, 2018

@stale stale bot closed this May 17, 2018

@lock lock bot locked as resolved and limited conversation to collaborators Aug 15, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
2 participants
You can’t perform that action at this time.