Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make template-indent preserve trailing spaces #1872

Merged
merged 3 commits into from Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion rules/template-indent.js
Expand Up @@ -67,9 +67,11 @@ const create = context => {
}

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

@fisker fisker Sep 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct.

It should only remove leading/trailing empty new lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker This is the way it is already working and I was not sure if we should alter the existing behavior because it would be a breaking change

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow I missed space in the regexp, it's good.


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
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