Cli: Comments in `package.json`: @comment prefix

9

What / Why

As a developer you would like to leave comments in the same context as you write code to note down todo's, leave hints for other devs or other reasons. Most of my code allows for this except for package.json, at least, there is no standard. After reading this blog I think this should be a standard, in a way that more people use the same notation and my IDE or VSC highlights comments as a comment.

When

  • at code-time

Where

package.json

How

The proposed standard (for now) is that above a specific property in an array, you note down the same property prefixed with @comment<space>.

Current Behavior

  • @comment is not a common standard
  • if this is done and agreed in this context, I can follow through and talk to IDE's repos
  • n/a

Expected Behavior

  • @comment is not a common standard
  • n/a

Who

  • n/a

References

this blog

  • n/a
rkristelijn picture rkristelijn  ·  10 Jan 2020

Most helpful comment

11

Douglas Crockford did not add any support for comments in the original specs (only specific JS types) and does not intend to.

I don't think that's accurate. He said:

A JSON encoder MUST NOT output comments. A JSON decoder MAY accept and ignore comments.

Comments were initially allowed, and were only removed to prevent people from adding parsing directives. There's no reason that decoders can't allow and ignore them, and some do.

npm init shouldn't _add_ any comments, but npm install etc don't need to _remove_ ones that a developer has intentionally added either.

There's lots of potential workarounds, but they all suck in various ways. Developers are always going to want to add comments, and fighting that causes a lot of wasted time and frustration.

Of course, if supporting JSON5 is a more practical solution, that'd be just as good.

iandunn picture iandunn  ·  6 Feb 2020

All comments

0

JSON normally does not support that. JSON5 would be an alternative solution.

Personally I don't think this is a good idea in general.

DanielRuf picture DanielRuf  ·  14 Jan 2020
1

JSON normally does not support that. JSON5 would be an alternative solution.

I've looked into JSON5 for package.json and found this discusion discussing: package.yml and package.js. An example of package.yaml and the package.yaml issue. Also hjson is discussed. I realise I've opened the can of worms again, however I also realise I'm not alone requesting comments in package.json.

I will look into json5 and see if I can make something work. Currently I have no new arguments for this issue/change-request.

Can you elaborate and motivate your thoughts why you think it is not a good idea to support comments?

rkristelijn picture rkristelijn  ·  14 Jan 2020
-12

JSON already supports comments:

{
  "foo": "the foo field is for storing valuable information",
  "foo": "the real value of foo"
}
ljharb picture ljharb  ·  14 Jan 2020
0

JSON already supports comments:

Not native / real comments but a workaround which is also shown in the blogpost which the reporter linked afaik.

DanielRuf picture DanielRuf  ·  14 Jan 2020
0

The blog post talks about making parallel keys in a "comments" sub-object. I'm saying that JSON natively allows duplicate keys, so as long as the last one is the actual value, you can use the duplicates for anything you like, including comments.

ljharb picture ljharb  ·  14 Jan 2020
0

Your example would throw linting errors in some cases (duplicate key) and could throw parsing errors in a few cases probably.

Douglas Crockford did not add any support for comments in the original specs (only specific JS types) and does not intend to.

DanielRuf picture DanielRuf  ·  14 Jan 2020
0

As far as I understand the request is very specific (annotation @comment)- or special field.

DanielRuf picture DanielRuf  ·  14 Jan 2020
0

And how it's displayed in IDEs (differently).

DanielRuf picture DanielRuf  ·  14 Jan 2020
0

Anything that errors on duplicate keys in JSON is violating the JSON spec (short of a linter rule that could be disabled, of course). Crockford has not influenced the JSON spec in many years, and TC39 (which does) certainly has no intention of changing it :-)

ljharb picture ljharb  ·  14 Jan 2020
0

True. But I would suggest adding HJSON or JSON5 support as this makes more sense (readability, ecosystem support and features) than such a "workaround". Just my opinion.

DanielRuf picture DanielRuf  ·  14 Jan 2020
0

Can you elaborate and motivate your thoughts why you think it is not a good idea to support comments?

  • if this is done and agreed in this context, I can follow through and talk to IDE's repos

That would be a huge effort.

DanielRuf picture DanielRuf  ·  14 Jan 2020
0

Vv

shawonforhad picture shawonforhad  ·  25 Jan 2020
0

Social-Icon

shawonforhad picture shawonforhad  ·  25 Jan 2020
11

Douglas Crockford did not add any support for comments in the original specs (only specific JS types) and does not intend to.

I don't think that's accurate. He said:

A JSON encoder MUST NOT output comments. A JSON decoder MAY accept and ignore comments.

Comments were initially allowed, and were only removed to prevent people from adding parsing directives. There's no reason that decoders can't allow and ignore them, and some do.

npm init shouldn't _add_ any comments, but npm install etc don't need to _remove_ ones that a developer has intentionally added either.

There's lots of potential workarounds, but they all suck in various ways. Developers are always going to want to add comments, and fighting that causes a lot of wasted time and frustration.

Of course, if supporting JSON5 is a more practical solution, that'd be just as good.

iandunn picture iandunn  ·  6 Feb 2020
1

I wish we had this for Christmas 2020.

Honestly, whether it's JSON 5 or another standard, it'd be better than what we got at the moment.
Comments are so important in anything non-trivial. I don't understand why JSON5 isn't compatible with package.json file already, it's not like this file is going to be fetched from a browser or something alike. It's only used by node.js and it would be possible to use a JSON5 loader for this particular file in a backward-compatible way.

Vadorequest picture Vadorequest  ·  27 Oct 2020
0

The first step would have to be adding native support in node itself for loading another format, so that every tool in the ecosystem could switch to using it to read package.json. Once all of them switched, then it'd be reasonable to suggest npm supporting that format for package manifests.

ljharb picture ljharb  ·  27 Oct 2020