Error parsing strings with curly braces in JavaScript blocks
See original GitHub issueThis is example that break the parser:
str.replace(/\$\{/g, '\\${');
it throws:
Expected code block but "\n" found.
In my language playground.
As a workaround I’ve used escaped value:
str.replace(/\$\x7b/g, '\\$\x7b');
Curly braces in strings and regex confuse the parser. I’ve tried to use this on a Website:
{
function foo(x) {
return x.replace(/\}/g, '}');
}
}
and it throws random errors.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
javascript - Splitting a string with curly braces as delimeters?
Split returns an array based on a delimiter, so if the delimiter is ... The value of the curly blocks are every 2...
Read more >Curly braces in JavaScript code must be balanced #89 - GitHub
Literal string containing curly brace not recognized within code block #398 ... #534 improves the error message for unbalanced braces.
Read more >SyntaxError: missing } after function body - JavaScript | MDN
The JavaScript exception "missing } after function body" occurs when there is a ... Check if any closing curly brackets or parenthesis are...
Read more >Airbnb JavaScript Style Guide()
Why? Per the eslint documentation, unary increment and decrement statements are subject to automatic semicolon insertion and can cause silent errors with ...
Read more >CoffeeScript
Instead of using curly braces { } to surround blocks of code in functions, ... Like JavaScript and many other languages, CoffeeScript supports...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I plan to make an API that will allow plugins replace source code parsing part of the peggy grammar, so the plugins for other languages can implement a minimal subset to correctly parse braces for their languages. Peggy itself will contain only JS parser subset.
When this will be implemented, I’ll make issues/PRs to known plugins.
I think, we can leave this open until proposed solution (pluggable
CodeBlockparsers) will be implemented. This, however, requires some work to design a way for composing grammars, which is also required for import feature. Of course, we can implement a special mechanism just to support this case, but I think it will be better to use a generic solution