Skip to content

Commit 32a0e6b

Browse files
committed
Move template AST logic for classes/attributes to transformTemplate()
1 parent 30ddfa0 commit 32a0e6b

3 files changed

Lines changed: 69 additions & 51 deletions

File tree

lib/transform.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,32 @@ function transform(source, template, options = {}) {
5252
let type = checkComponentType(root);
5353
let result;
5454

55-
switch (type) {
56-
case 'classic':
57-
result = transformClassicComponent(root, options);
58-
break;
59-
default:
60-
throw new Error(
61-
`Unsupported component type. Only classic components (\`Component.extend({ ... }\`) are supported currently.`
62-
);
55+
if (type === 'classic') {
56+
result = transformClassicComponent(root, options);
57+
} else {
58+
throw new SilentError(
59+
`Unsupported component type. Only classic components (\`Component.extend({ ... }\`) are supported currently.`
60+
);
6361
}
6462

6563
if (result) {
66-
let { newSource, attrs, tagName } = result;
67-
let newTemplate = transformTemplate(template, tagName, attrs);
64+
let {
65+
newSource,
66+
tagName,
67+
elementId,
68+
classNames,
69+
classNameBindings,
70+
attributeBindings,
71+
} = result;
72+
let newTemplate = transformTemplate(
73+
template,
74+
tagName,
75+
elementId,
76+
classNames,
77+
classNameBindings,
78+
attributeBindings,
79+
options
80+
);
6881

6982
return { source: newSource, template: newTemplate, tagName };
7083
}

lib/transform/classic.js

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const j = require('jscodeshift').withParser('ts');
22
const _debug = require('debug')('tagless-ember-components-codemod');
3-
const templateRecast = require('ember-template-recast');
43

54
const SilentError = require('../silent-error');
65
const {
@@ -13,8 +12,6 @@ const {
1312
isProperty,
1413
} = require('../utils');
1514

16-
const b = templateRecast.builders;
17-
1815
const EVENT_HANDLER_METHODS = [
1916
// Touch events
2017
'touchStart',
@@ -154,41 +151,5 @@ module.exports = function transformClassicComponent(root, options) {
154151

155152
let newSource = root.toSource();
156153

157-
// wrap existing template with root element
158-
let classNodes = [];
159-
if (options.hasComponentCSS) {
160-
classNodes.push(b.mustache('styleNamespace'));
161-
}
162-
for (let className of classNames) {
163-
classNodes.push(b.text(className));
164-
}
165-
classNameBindings.forEach(([truthy, falsy], property) => {
166-
if (!truthy) {
167-
classNodes.push(b.mustache(`unless this.${property} "${falsy}"`));
168-
} else {
169-
classNodes.push(b.mustache(`if this.${property} "${truthy}"${falsy ? ` "${falsy}"` : ''}`));
170-
}
171-
});
172-
173-
let attrs = [];
174-
if (elementId) {
175-
attrs.push(b.attr('id', b.text(elementId)));
176-
}
177-
attributeBindings.forEach((value, key) => {
178-
attrs.push(b.attr(key, b.mustache(`this.${value}`)));
179-
});
180-
if (classNodes.length === 1) {
181-
attrs.push(b.attr('class', classNodes[0]));
182-
} else if (classNodes.length !== 0) {
183-
let parts = [];
184-
classNodes.forEach((node, i) => {
185-
if (i !== 0) parts.push(b.text(' '));
186-
parts.push(node);
187-
});
188-
189-
attrs.push(b.attr('class', b.concat(parts)));
190-
}
191-
attrs.push(b.attr('...attributes', b.text('')));
192-
193-
return { newSource, attrs, tagName };
154+
return { newSource, tagName, elementId, classNames, classNameBindings, attributeBindings };
194155
};

lib/transform/template.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,51 @@ const b = templateRecast.builders;
66

77
const PLACEHOLDER = '@@@PLACEHOLDER@@@';
88

9-
module.exports = function transformTemplate(template, tagName, attrs) {
9+
module.exports = function transformTemplate(
10+
template,
11+
tagName,
12+
elementId,
13+
classNames,
14+
classNameBindings,
15+
attributeBindings,
16+
options
17+
) {
18+
// wrap existing template with root element
19+
let classNodes = [];
20+
if (options.hasComponentCSS) {
21+
classNodes.push(b.mustache('styleNamespace'));
22+
}
23+
for (let className of classNames) {
24+
classNodes.push(b.text(className));
25+
}
26+
classNameBindings.forEach(([truthy, falsy], property) => {
27+
if (!truthy) {
28+
classNodes.push(b.mustache(`unless this.${property} "${falsy}"`));
29+
} else {
30+
classNodes.push(b.mustache(`if this.${property} "${truthy}"${falsy ? ` "${falsy}"` : ''}`));
31+
}
32+
});
33+
34+
let attrs = [];
35+
if (elementId) {
36+
attrs.push(b.attr('id', b.text(elementId)));
37+
}
38+
attributeBindings.forEach((value, key) => {
39+
attrs.push(b.attr(key, b.mustache(`this.${value}`)));
40+
});
41+
if (classNodes.length === 1) {
42+
attrs.push(b.attr('class', classNodes[0]));
43+
} else if (classNodes.length !== 0) {
44+
let parts = [];
45+
classNodes.forEach((node, i) => {
46+
if (i !== 0) parts.push(b.text(' '));
47+
parts.push(node);
48+
});
49+
50+
attrs.push(b.attr('class', b.concat(parts)));
51+
}
52+
attrs.push(b.attr('...attributes', b.text('')));
53+
1054
let templateAST = templateRecast.parse(template);
1155

1256
templateAST.body = [

0 commit comments

Comments
 (0)