question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

markdown: plain object in js code block causing a throw error

See original GitHub issue

Prettier 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:

error_in_use

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:closed
  • Created 5 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
ikatyangcommented, Dec 10, 2018

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?

1reaction
j-f1commented, Dec 10, 2018

Would it be possible to share the entire readme.md?

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between `throw new Error` and `throw ...
The throw someObject will throw the object as is and will not allow any further code execution from the try block, ie same...
Read more >
Don't throw an exception for unknown code block languages
In my source content, I have: ```HTTP stuff ``` This results in a Javascript error: example.html:28 Uncaught Error: Unknown language: "HTTP" Please report ......
Read more >
Markdown Syntax Documentation - Daring Fireball
A Markdown-formatted document should be publishable as-is, as plain text, without looking like it's been marked up with tags or formatting instructions.
Read more >
Stack information of uncaught Error object should be available ...
I'm adding my support for a stack argument to be passed to window.onerror. The lack of a stack trace means that errors in...
Read more >
Google JavaScript Style Guide
1 Introduction. This document serves as the complete definition of Google's coding standards for source code in the JavaScript programming language.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found