Skip to content

Commit 1b3e4fc

Browse files
jelhanTurbo87
authored andcommitted
improve formatting template code
* do not intend empty lines * strip leading and trailing newlines
1 parent 39cf4be commit 1b3e4fc

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

lib/__tests__/__snapshots__/transform.js.snap

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ foo
2323
=========="
2424
`;
2525
26+
exports[`classic components empty lines are not intended 1`] = `
27+
"==========
28+
export default Component.extend({});
29+
~~~~~~~~~~
30+
foo
31+
32+
bar
33+
~~~~~~~~~~
34+
=> tagName: div
35+
~~~~~~~~~~
36+
export default Component.extend({
37+
tagName: \\"\\"
38+
});
39+
~~~~~~~~~~
40+
<div ...attributes>
41+
foo
42+
43+
bar
44+
</div>
45+
=========="
46+
`;
47+
2648
exports[`classic components handles \`ariaRole\` correctly 1`] = `
2749
"==========
2850
@@ -191,6 +213,26 @@ foo
191213
=========="
192214
`;
193215
216+
exports[`classic components leading and trailing empty lines are stripped 1`] = `
217+
"==========
218+
export default Component.extend({});
219+
~~~~~~~~~~
220+
221+
foo
222+
223+
~~~~~~~~~~
224+
=> tagName: div
225+
~~~~~~~~~~
226+
export default Component.extend({
227+
tagName: \\"\\"
228+
});
229+
~~~~~~~~~~
230+
<div ...attributes>
231+
foo
232+
</div>
233+
=========="
234+
`;
235+
194236
exports[`classic components multi-line template 1`] = `
195237
"==========
196238
export default Component.extend({});
@@ -511,7 +553,7 @@ foo
511553
}
512554
513555
~~~~~~~~~~
514-
<div class=\\"{{styleNamespace}} foo bar:baz\\" ...attributes>
556+
<div class=\\"{{this.styleNamespace}} foo bar:baz\\" ...attributes>
515557
foo
516558
</div>
517559
=========="

lib/__tests__/transform.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,26 @@ describe('classic components', function() {
231231
expect(generateSnapshot(source, template)).toMatchSnapshot();
232232
});
233233

234+
test('empty lines are not intended', () => {
235+
let source = `export default Component.extend({});`;
236+
237+
let template = `foo
238+
239+
bar`;
240+
241+
expect(generateSnapshot(source, template)).toMatchSnapshot();
242+
});
243+
244+
test('leading and trailing empty lines are stripped', () => {
245+
let source = `export default Component.extend({});`;
246+
247+
let template = `
248+
foo
249+
`;
250+
251+
expect(generateSnapshot(source, template)).toMatchSnapshot();
252+
});
253+
234254
test('handles `hasComponentCSS` option correctly', () => {
235255
let source = `
236256
export default Component.extend({

lib/utils/template.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
function indentLines(content) {
2-
return content
3-
.split('\n')
4-
.map(it => ` ${it}`)
5-
.join('\n');
2+
return (
3+
content
4+
// strip leading and trailing new lines
5+
.trim()
6+
.split('\n')
7+
.map(it => {
8+
// intend non-empty lines with two spaces
9+
return it.trim().length > 0 ? ` ${it}` : '';
10+
})
11+
.join('\n')
12+
);
613
}
714

815
module.exports = {

0 commit comments

Comments
 (0)