-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Triple backticks to allow creation of JavaScript blocks #4357
Changes from 3 commits
27e8a62
107d8af
edd36b5
b9dd310
3200259
0d5449f
7689620
9945e68
621d9d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,32 @@ | ||
# Javascript Literals | ||
# JavaScript Literals | ||
# ------------------- | ||
|
||
# TODO: refactor javascript literal tests | ||
# TODO: add indexing and method invocation tests: `[1]`[0] is 1, `function(){}`.call() | ||
test "inline JavaScript is evaluated", -> | ||
eq '\\`', ` | ||
// Inline JS | ||
"\\\`" | ||
` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With my proposed escaping rules, this test needs to be written like this: eq '\\`', `
// Inline JS
"\\\\\`"
` or like this: eq '\\a`', `
// Inline JS
"\\a\`"
` |
||
|
||
eq '\\`', ` | ||
// Inline JS | ||
"\\\`" | ||
` | ||
test "escaped backticks are output correctly", -> | ||
`var a = 'foo\`bar';` | ||
eq a, 'foo`bar' | ||
|
||
test "block inline JavaScript is evaluated", -> | ||
``` | ||
var a = 1; | ||
var b = 2; | ||
``` | ||
c = 3 | ||
```var d = 4;``` | ||
eq a + b + c + d, 10 | ||
|
||
test "block inline JavaScript containing backticks", -> | ||
``` | ||
// This is a comment with `backticks` | ||
var a = 42; | ||
var b = `foo ${'bar'}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no 'eq' for variable b in the ""block inline JavaScript containing backticks" test. Is this correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, as I didn't want to rely on ES2015 in the 1.x branch. Though I guess I am already for it to parse the |
||
var c = 3; | ||
var d = 'foo`bar`'; | ||
``` | ||
eq a + c, 45 | ||
eq d, 'foo`bar`' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I read the added regex part as "capture everything between starting and end triple backquotes, stopping at first-seen ending triple quotes", which seems sane.
The original regex is rather obtuse. I read it as "greedily read everything between starting and ending single backquotes, and make sure every backslash has a character following it". I'm thinking the backslash+character part exists to stop an escaped backslash preemptively closing the Javascript block. E.g:
The new triple backticks regex will behave differently:
I found this website useful: https://regex101.com/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#3760 is actually complaining about just what you describe: a backslash not followed by a character (in that case, it's followed by a newline). Perhaps the original regex is incorrect here?
What would you use as the regex, if you don't mind taking the time to play with it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parsing this way is inconsistent with heredocs and heregexen, where backslash can be used to escape characters adjacent to the delimeters:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per my last comment, I’m suggesting we convert
\`` to ``` wherever
`` appears inside a single- or triple-backtick block. That should cover this case, yes?