Commit 06b77010 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Fix trim-and-dedent for interpolated vars with text after

parent 317b2d87
......@@ -53,6 +53,33 @@ lines
with no indentation
`,
],
[
() => {
const name = `Jane`;
const secondVar = `
multiple
lines
with no indentation
`;
return trimAndDedent`
Hello, ${name}!
Indented line
Goodbye, John!
text${secondVar}more text
`;
},
`Hello, Jane!
Indented line
Goodbye, John!
text
multiple
lines
with no indentation
more text`,
],
].forEach(([getResult, expectedResult]) => {
it('normalizes strings with multiple lines', () => {
assert.equal(getResult(), expectedResult);
......
......@@ -19,16 +19,14 @@ function dedentStr(str: string, indent: number) {
* params verbatim.
*/
function dedent(strings: string[], ...params: any[]) {
// Match the smallest indentation among all strings
const indents = strings
.map(str => {
const match = str.match(/^[ \t]*(?=\S)/gm);
return match ? Math.min(...match.map(el => el.length)) : -1;
})
// Exclude lines where indentation could not be matched
.filter(num => num > -1);
const smallestIndent = indents.length > 0 ? Math.min(...indents) : 0;
// Match the smallest indentation among all lines of the full string before
// interpolating params
const match = strings.join('').match(/^[ \t]*(?=\S)/gm);
const smallestIndent = match ? Math.min(...match.map(el => el.length)) : 0;
// Dedent every individual string while interpolating params
// Those strings which are not the beginning of a line will have zero-indent
// and dedenting will have no effect
let result = '';
for (const [i, param] of params.entries()) {
result += dedentStr(strings[i], smallestIndent);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment