DavidAnson / vscode-markdownlint

Markdown linting and style checking for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=DavidAnson.vscode-markdownlint
MIT License
930 stars 170 forks source link

Parser unnecessarily analyses comments in jsonc files #295

Closed lonix1 closed 1 year ago

lonix1 commented 1 year ago

I have a .markdownlint.jsonc at the project root:

{
  // ...
  "MD012": { "maximum": 2 },      // [1] https://github.com/DavidAnson/markdownlint/blob/main/doc/md012.md
  // ...
}

Errors in the markdownlint log:

Error: Unable to parse '/home/username/project/.markdownlint.jsonc'; Parser 0: Expected double-quoted property name in JSON at position 312; Parser 1: Missing , or : between flow map items at line 3, column 72: … // [1]; "Multiple consecutive blank lines"… ^

(I usually add a [x] to denote the default value (from which I'm changing). It's standard syntax for CLI documentation, so others on the team would know what it means.)

The parser seems to be analysing that bit, even though it's a comment, and it's clearly a jsonc file which allows comments (and vscode identifies the file as "JSON with Comments").

So the conclusion is the parser is doing unnecessary work.

lonix1 commented 1 year ago

Another parser error:

// this file controls markdownlint
// for rules, refer to https://github.com/DavidAnson/markdownlint
// blah blah blah
{
  // ...
}

Error:

Error: Unable to parse ... Parser 0: Expected double-quoted property name in JSON at position 282; Parser 1: Implicit keys need to be on a single line at line 1, column 1:

It's jsonc, so that comment should be allowed.

I assumed markdownlint respects comments because it seems to support jsonc?

DavidAnson commented 1 year ago

Comments in JSONC files are removed by the jsonc-parser library: https://www.npmjs.com/package/jsonc-parser

Like so: https://github.com/DavidAnson/vscode-markdownlint/blob/e2c2da1ab83fe9280407245c6b2320c1c3191d8a/extension.js#L58

I haven't known it to have issues like this, but can investigate.

DavidAnson commented 1 year ago

Your interpretation of the topmost error message is reasonable, but probably wrong. :) Note that it is providing the error message for both parsers. Parser 0 is the JSONC parser and parser 1 is the YAML person. The line you are showing corresponds to the YAML error; you should please find position 312 in order to help us debug the JSONC issue.

DavidAnson commented 1 year ago

Same situation for your second error. Amusingly/annoyingly, those positions are after stripping the comments. If you are able to link to or include the entire JSONC file here, that would be helpful.

lonix1 commented 1 year ago

1st error:

The error led me down the wrong path - it's not the [1] that was the error, it was a trailing slash:

"MD012": { "maximum": 2 },      // [1] https://github.com/DavidAnson/markdownlint/blob/main/doc/md012.md

Note the trailing slash! When I copied-pasted into the issue, I removed it.

VSCode allows it, but maybe the jsonc spec does not? When removed, it works.

But the error clearly didn't help in this case :-)

2nd error:

Same thing. When the trailing slash was removed, it allowed comments outside the hash.

Conclusion:

Thanks, and thanks for a very useful suite of tools.

DavidAnson commented 1 year ago

Glad we got this sorted!