From e4071f56ed667eabe0f3ab69b2ecba0d020e42d1 Mon Sep 17 00:00:00 2001 From: Michael Naumov Date: Sat, 1 Oct 2022 10:06:50 +0300 Subject: [PATCH] `template-indent`: Preserve trailing spaces (#1872) --- rules/template-indent.js | 4 +++- test/template-indent.mjs | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/rules/template-indent.js b/rules/template-indent.js index 9e14ab4330..ec55859963 100644 --- a/rules/template-indent.js +++ b/rules/template-indent.js @@ -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; diff --git a/test/template-indent.mjs b/test/template-indent.mjs index 5b3507ce77..594539acf0 100644 --- a/test/template-indent.mjs +++ b/test/template-indent.mjs @@ -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: [ @@ -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 + \` + `), + }, ], });