Skip to content

Commit

Permalink
Fix incorrect no-trailing-spaces in embedded templates
Browse files Browse the repository at this point in the history
  • Loading branch information
robinborst95 committed Mar 8, 2023
1 parent d829786 commit 1c84835
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/rules/no-trailing-spaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ export default class NoTrailingSpaces extends Rule {

exit(node) {
let source = this.sourceForNode(node);
let lines = source.split('\n');

for (const [i, line] of source.split('\n').entries()) {
for (const [i, line] of lines.entries()) {
let column = line.length - 1;
if (line[column] === ' ') {
let isLastLine = i === lines.length - 1;

if (line[column] === ' ' && (!isLastLine || column > this.columnOffset)) {
this.log({
message: 'line cannot end with space',
node,
Expand Down
84 changes: 84 additions & 0 deletions test/unit/rules/no-trailing-spaces-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ generateRuleTests({
'test\n' + '\n',
// test the re-entering of yielded content
'{{#my-component}}\n' + ' test\n' + '{{/my-component}}',
{
template: [
"import { hbs } from 'ember-cli-htmlbars';",
'',
"test('it renders', async (assert) => {",
' await render(hbs`',
' <div class="parent">',
' <div class="child"></div>',
' </div>',
' `);',
');',
].join('\n'),
meta: {
filePath: 'layout.js',
},
},
],

bad: [
Expand Down Expand Up @@ -100,5 +116,73 @@ generateRuleTests({
`);
},
},
{
template: [
"import { hbs } from 'ember-cli-htmlbars';",
'',
"test('it renders', async (assert) => {",
' await render(hbs` ',
' <div class="parent">',
' <div class="child"></div>',
' </div>',
' `);',
');',
].join('\n'),
meta: {
filePath: 'layout.js',
},

verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
[
{
"column": 20,
"endColumn": 21,
"endLine": 8,
"filePath": "layout.js",
"line": 4,
"message": "line cannot end with space",
"rule": "no-trailing-spaces",
"severity": 2,
"source": " ",
},
]
`);
},
},
{
template: [
"import { hbs } from 'ember-cli-htmlbars';",
'',
"test('it renders', async (assert) => {",
' await render(hbs`',
' <div></div>',
' ',
' <div></div>',
' `);',
');',
].join('\n'),
meta: {
filePath: 'layout.js',
},

verifyResults(results) {
expect(results).toMatchInlineSnapshot(`
[
{
"column": 1,
"endColumn": 2,
"endLine": 8,
"filePath": "layout.js",
"line": 6,
"message": "line cannot end with space",
"rule": "no-trailing-spaces",
"severity": 2,
"source": " ",
},
]
`);
},
},
],
});

0 comments on commit 1c84835

Please sign in to comment.