zspecza / common-tags

🔖 Useful template literal tags for dealing with strings in ES2015+

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

source tag doubles newlines in certain circumstances

ibrahima opened this issue · comments

I think there's some kind of issue with interpolation/substitution with the source tag - for instance, if I do something like

const baz = "baz\nquux\ngarply";
source`
  Foo
  Bar

  ${baz}
`

where baz contains newlines, the result will have all the newlines within baz doubled:

Foo
Bar

baz

quux

garply

but I would have expected

Foo
Bar

baz
quux
garply

This only happens if the interpolation is preceded by a newline.

Interestingly, if I do something like

source`${baz}`

the newlines actually get turned into spaces, which is also not what I would have expected.

I think it has something to do with the fact that source blocks attempt to match the outer indentation level when you interpolate a variable? I.e.,

source`
  Foo:
    Bar
    ${baz}
`

becomes

Foo:
  Bar
  baz
  quux
  garply

Wrote up some specs, and a possible fix, in #114

Let me know if that seems good! Unfortunately javascript doesn't have a horizontal whitespace character class so I had to use this trick: https://stackoverflow.com/a/3469155/90551 . Maybe there's a better way though?

This should be fixed now; I also added a regression test for the case: 147a84a