Skip to content

Commit 71fcc6c

Browse files
committed
no longer insert unnecisary empty string at start of template string replacement when there's already a string on other side of placeholder
1 parent 7cf59f1 commit 71fcc6c

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,8 @@ export async function processScript(script: string) {
660660
// replaced with a string of a different length which messes up indexes
661661
const tokens = [ ...tokenize(script, { ecmaVersion: 2015 }) ].reverse().values()
662662

663+
let templateToRightOfPlaceholder = false
664+
663665
for (const token of tokens) {
664666
// we can't replace any tokens before the block statement or we'll break stuff
665667
if (token.start < blockStatementIndex)
@@ -693,6 +695,16 @@ export async function processScript(script: string) {
693695

694696
// there *is* a point in concatenating an empty string at the
695697
// start because foo + bar is not the same thing as "" + foo + bar
698+
// ...but foo + "<template>" + bar *is* the same thing as "" + foo + "<template>" + bar
699+
// so we just need to check if there's a template to the right of the placeholder and skip that case
700+
701+
if (token.value == "" && templateToRightOfPlaceholder) {
702+
templateToRightOfPlaceholder = false
703+
script = stringSplice(script, "((", token.start - 1, token.end + 2)
704+
break
705+
}
706+
707+
templateToRightOfPlaceholder = false
696708

697709
let jsonValueIndex = jsonValues.indexOf(token.value)
698710

@@ -705,10 +717,13 @@ export async function processScript(script: string) {
705717

706718
// no point in concatenating an empty string
707719
if (token.value == "") {
720+
templateToRightOfPlaceholder = false
708721
script = stringSplice(script, ")+(", token.start - 1, token.end + 2)
709722
break
710723
}
711724

725+
templateToRightOfPlaceholder = true
726+
712727
let jsonValueIndex = jsonValues.indexOf(token.value)
713728

714729
if (jsonValueIndex == -1)

0 commit comments

Comments
 (0)