markdown: plain object in js code block causing a throw error
See original GitHub issuePrettier 1.15.3 Playground link
--parser markdown
Input:
Will break on second line:
```js
{
whitelist: [], // for example, [".class-1", "#id-1", ".module-*"]
backend: [], // for example, [{ heads: "{{", tails: "}}" }, { heads: "{%", tails: "%}" }]
uglify: false,
removeHTMLComments: true,
doNotRemoveHTMLCommentsWhoseOpeningTagContains: ["[if", "[endif"]
}
```
In real life, it’s markdown file with code block set to “js”, and minimal sample is:
{
a: [], // for example, [".a"]
b: []
}
Output:
SyntaxError: Unexpected token, expected ";" (3:10)
1 | {
2 | whitelist: [], // for example, [".class-1", "#id-1", ".module-*"]
> 3 | backend: [], // for example, [{ heads: "{{", tails: "}}" }, { heads: "{%", tails: "%}" }]
| ^
4 | uglify: false,
5 | removeHTMLComments: true,
6 | doNotRemoveHTMLCommentsWhoseOpeningTagContains: ["[if", "[endif"]
Expected behavior: Playground above is showing a different error message from the real life because in real life it’s a markdown file with JS code block, but code is coming from the same place:
OK, this is technically not valid JavaScript piece, a plain object is not assigned to anything, but I was trying to describe am options’ plain object in my readme. It is still not acceptable that markdown parser should throw an error. Instead, it should gracefully skip the code block, for example:
{
a: [], // for example, [".a"]
b: []
}
I did some research and the error:
readme.md: SyntaxError: Invalid regular expression: /^\s{0,3}**/: Nothing to repeat
is coming from https://github.com/prettier/prettier/blob/952bc0cc039c034c5fe651864db528dcd9800e9e/src/language-markdown/utils.js#L149
where new regex is created in new RegExp(
^\s{0,${leadingSpaceCount}});
Now, maybe we can do something to validate the leadingSpaceCount
before using it in the regex? @ikatyang what do you think?
Issue Analytics
- State:
- Created 5 years ago
- Comments:13 (7 by maintainers)
Top GitHub Comments
I guess it probably comes from here:
https://github.com/prettier/prettier/blob/952bc0cc039c034c5fe651864db528dcd9800e9e/src/language-markdown/utils.js#L160
The
marker
is supposed to be`
or~
but I’m not sure where does the**
come from, and I cannot reproduce the issue locally. Can you share more info?Would it be possible to share the entire
readme.md
?