Skip to content

Commit

Permalink
JS: Template strings: Increased bracket count of interpolations (#1845)
Browse files Browse the repository at this point in the history
This increases the number of nested braces which can be matched by JS's template pattern to supported nested objects within interpolation expressions.
  • Loading branch information
RunDevelopment committed Apr 19, 2019
1 parent 9e16493 commit c13d6e7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
4 changes: 2 additions & 2 deletions components/prism-javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {

Prism.languages.insertBefore('javascript', 'string', {
'template-string': {
pattern: /`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,
pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|[^\\`])*`/,
greedy: true,
inside: {
'interpolation': {
pattern: /\${[^}]+}/,
pattern: /\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}/,
inside: {
'interpolation-punctuation': {
pattern: /^\${|}$/,
Expand Down
2 changes: 1 addition & 1 deletion components/prism-javascript.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion examples/prism-javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@ <h2>Known failures</h2>
</p>

<h3>String interpolation containing a closing brace</h3>
<pre><code>`${ {foo:'bar'}.foo }`
<pre><code>`${ /* } */ a + b }`
`${ '}' }`</code></pre>

<h3>String interpolation with deeply nested braces</h3>
<pre><code>`${foo({ a: { b: { c: true } } })}`</code></pre>
4 changes: 2 additions & 2 deletions prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,11 @@ Prism.languages.insertBefore('javascript', 'keyword', {

Prism.languages.insertBefore('javascript', 'string', {
'template-string': {
pattern: /`(?:\\[\s\S]|\${[^}]+}|[^\\`])*`/,
pattern: /`(?:\\[\s\S]|\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}|[^\\`])*`/,
greedy: true,
inside: {
'interpolation': {
pattern: /\${[^}]+}/,
pattern: /\${(?:[^{}]|{(?:[^{}]|{[^}]*})*})+}/,
inside: {
'interpolation-punctuation': {
pattern: /^\${|}$/,
Expand Down
75 changes: 59 additions & 16 deletions tests/languages/javascript/template-string_feature.test
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ bar`
"foo `a` `b` `c` `d` bar"
"test // test" `template`

console.log(`This is ${it.with({ type: false })}!`)
`${ {foo:'bar'}.foo }`

----------------------------------------------------

[
Expand All @@ -27,23 +30,63 @@ bar`
["string", "`"]
]],
["template-string", [
["string", "`"],
["interpolation", [
["interpolation-punctuation", "${"],
["function", "foo"],
["punctuation", "("],
["punctuation", ")"],
["interpolation-punctuation", "}"]
]],
["string", "`"]
]],
["string", "\"foo `a` `b` `c` `d` bar\""],
["string", "\"test // test\""],
["template-string", [
["string", "`template`"]
]]
["string", "`"],
["interpolation", [
["interpolation-punctuation", "${"],
["function", "foo"],
["punctuation", "("],
["punctuation", ")"],
["interpolation-punctuation", "}"]
]],
["string", "`"]
]],
["string", "\"foo `a` `b` `c` `d` bar\""],
["string", "\"test // test\""],
["template-string", [
["string", "`template`"]
]],

"\r\n\r\nconsole",
["punctuation", "."],
["function", "log"],
["punctuation", "("],
["template-string", [
["string", "`This is "],
["interpolation", [
["interpolation-punctuation", "${"],
"it",
["punctuation", "."],
["function", "with"],
["punctuation", "("],
["punctuation", "{"],
" type",
["punctuation", ":"],
["boolean", "false"],
["punctuation", "}"],
["punctuation", ")"],
["interpolation-punctuation", "}"]
]],
["string", "!`"]
]],
["punctuation", ")"],

["template-string", [
["string", "`"],
["interpolation", [
["interpolation-punctuation", "${"],
["punctuation", "{"],
"foo",
["punctuation", ":"],
["string", "'bar'"],
["punctuation", "}"],
["punctuation", "."],
"foo ",
["interpolation-punctuation", "}"]
]],
["string", "`"]
]]
]

----------------------------------------------------

Checks for single-line and multi-line template strings.
Checks for single-line and multi-line template strings.

0 comments on commit c13d6e7

Please sign in to comment.