Skip to content

Commit fc48a1c

Browse files
authored
Merge pull request #12 from ember-codemods/wrapper-indent
Improve template wrapper output
2 parents 39ff5fd + 42f074e commit fc48a1c

3 files changed

Lines changed: 72 additions & 9 deletions

File tree

lib/__tests__/__snapshots__/transform.js.snap

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ foo
1717
});
1818
1919
~~~~~~~~~~
20-
<div ...attributes>foo</div>
20+
<div ...attributes>
21+
foo
22+
</div>
2123
=========="
2224
`;
2325
@@ -39,7 +41,9 @@ foo
3941
});
4042
4143
~~~~~~~~~~
42-
<div foo={{this.foo}} bar={{this.baz}} ...attributes>foo</div>
44+
<div foo={{this.foo}} bar={{this.baz}} ...attributes>
45+
foo
46+
</div>
4347
=========="
4448
`;
4549
@@ -61,7 +65,9 @@ foo
6165
});
6266
6367
~~~~~~~~~~
64-
<div class=\\"{{if this.a \\"b\\"}} {{if this.x \\"y\\" \\"z\\"}} {{unless this.foo \\"bar\\"}}\\" ...attributes>foo</div>
68+
<div class=\\"{{if this.a \\"b\\"}} {{if this.x \\"y\\" \\"z\\"}} {{unless this.foo \\"bar\\"}}\\" ...attributes>
69+
foo
70+
</div>
6571
=========="
6672
`;
6773
@@ -83,7 +89,9 @@ foo
8389
});
8490
8591
~~~~~~~~~~
86-
<div class=\\"foo bar:baz\\" ...attributes>foo</div>
92+
<div class=\\"foo bar:baz\\" ...attributes>
93+
foo
94+
</div>
8795
=========="
8896
`;
8997
@@ -105,7 +113,9 @@ foo
105113
});
106114
107115
~~~~~~~~~~
108-
<div id=\\"qux\\" ...attributes>foo</div>
116+
<div id=\\"qux\\" ...attributes>
117+
foo
118+
</div>
109119
=========="
110120
`;
111121
@@ -127,7 +137,35 @@ foo
127137
});
128138
129139
~~~~~~~~~~
130-
<div class=\\"foo\\" ...attributes>foo</div>
140+
<div class=\\"foo\\" ...attributes>
141+
foo
142+
</div>
143+
=========="
144+
`;
145+
146+
exports[`multi-line template 1`] = `
147+
"==========
148+
export default Component.extend({});
149+
~~~~~~~~~~
150+
{{#if this.foo}}
151+
FOO
152+
{{else}}
153+
BAR
154+
{{/if}}
155+
~~~~~~~~~~
156+
=> tagName: div
157+
~~~~~~~~~~
158+
export default Component.extend({
159+
tagName: \\"\\"
160+
});
161+
~~~~~~~~~~
162+
<div ...attributes>
163+
{{#if this.foo}}
164+
FOO
165+
{{else}}
166+
BAR
167+
{{/if}}
168+
</div>
131169
=========="
132170
`;
133171
@@ -149,6 +187,8 @@ foo
149187
});
150188
151189
~~~~~~~~~~
152-
<span ...attributes>foo</span>
190+
<span ...attributes>
191+
foo
192+
</span>
153193
=========="
154194
`;

lib/__tests__/transform.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,17 @@ test('throws if component is using `click()`', () => {
176176
`"Using \`click()\` is not supported in tagless components"`
177177
);
178178
});
179+
180+
test('multi-line template', () => {
181+
let source = `export default Component.extend({});`;
182+
183+
let template = `
184+
{{#if this.foo}}
185+
FOO
186+
{{else}}
187+
BAR
188+
{{/if}}
189+
`.trim();
190+
191+
expect(generateSnapshot(source, template)).toMatchSnapshot();
192+
});

lib/transform.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ const EVENT_HANDLER_METHODS = [
4646
'drop',
4747
];
4848

49+
const PLACEHOLDER = '@@@PLACEHOLDER@@@';
50+
4951
function transformPath(componentPath) {
5052
let debug = (fmt, ...args) => _debug(`${componentPath}: ${fmt}`, ...args);
5153

@@ -210,11 +212,11 @@ function transform(source, template, options = {}) {
210212
templateAST.body = [
211213
b.element(tagName, {
212214
attrs,
213-
children: templateAST.body,
215+
children: [b.text(`\n${PLACEHOLDER}\n`)],
214216
}),
215217
];
216218

217-
let newTemplate = templateRecast.print(templateAST);
219+
let newTemplate = templateRecast.print(templateAST).replace(PLACEHOLDER, indentLines(template));
218220

219221
return { source: newSource, template: newTemplate, tagName };
220222
}
@@ -313,6 +315,13 @@ function guessTemplatePath(componentPath) {
313315
return componentPath.replace('/components/', '/templates/components/').replace(/\.js$/, '.hbs');
314316
}
315317

318+
function indentLines(content) {
319+
return content
320+
.split('\n')
321+
.map(it => ` ${it}`)
322+
.join('\n');
323+
}
324+
316325
module.exports = {
317326
transform,
318327
transformPath,

0 commit comments

Comments
 (0)