Skip to content

Commit

Permalink
template-indent: Preserve trailing spaces (#1872)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaoumov authored Oct 1, 2022
1 parent 9a1adfa commit e4071f5
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
4 changes: 3 additions & 1 deletion rules/template-indent.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ const create = context => {
}

const dedented = stripIndent(joined);
const trimmed = dedented.replace(new RegExp(`^${eol}|${eol}[ \t]*$`, 'g'), '');

const fixed
= eol
+ indentString(dedented.trim(), 1, {indent: parentMargin + indent})
+ indentString(trimmed, 1, {indent: parentMargin + indent})
+ eol
+ parentMargin;

Expand Down
45 changes: 45 additions & 0 deletions test/template-indent.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,24 @@ test({
\`
`),
},
{
name: 'Lines with whitespaces are kept trimmed',
code: fixInput(`
outdent\`
••Line1
••
••Line2
\`
`),
errors,
output: fixInput(`
outdent\`
••Line1
••Line2
\`
`),
},
],
/** @type {import('eslint').RuleTester.ValidTestCase[]} */
valid: [
Expand Down Expand Up @@ -586,6 +604,33 @@ test({
••after
\`
`),
{
name: 'Trailing spaces in the last line are preserved',
code: fixInput(`
outdent\`
••Line with trailing spaces••••
\`
`),
},
{
name: 'Trailing spaces in non-last line are preserved',
code: fixInput(`
outdent\`
••Line with trailing spaces••••
••Line without trailing spaces
\`
`),
},
{
name: 'Empty lines are preserved',
code: fixInput(`
outdent\`
••Line1
••Line2
\`
`),
},
],
});

Expand Down

0 comments on commit e4071f5

Please sign in to comment.