Skip to content

Commit c93c5fa

Browse files
authored
Merge pull request #15 from ember-codemods/component-css
Add support for `ember-component-css`
2 parents 78fcf15 + 4f5bcc9 commit c93c5fa

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

lib/__tests__/__snapshots__/transform.js.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,30 @@ foo
119119
=========="
120120
`;
121121
122+
exports[`handles \`hasComponentCSS\` option correctly 1`] = `
123+
"==========
124+
125+
export default Component.extend({
126+
classNames: ['foo', 'bar:baz'],
127+
});
128+
129+
~~~~~~~~~~
130+
foo
131+
~~~~~~~~~~
132+
=> tagName: div
133+
~~~~~~~~~~
134+
135+
export default Component.extend({
136+
tagName: \\"\\",
137+
});
138+
139+
~~~~~~~~~~
140+
<div class=\\"{{styleNamespace}} foo bar:baz\\" ...attributes>
141+
foo
142+
</div>
143+
=========="
144+
`;
145+
122146
exports[`handles single \`classNames\` item correctly 1`] = `
123147
"==========
124148

lib/__tests__/transform.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { transform } = require('../transform');
22

3-
function generateSnapshot(source, template) {
4-
let result = transform(source, template);
3+
function generateSnapshot(source, template, options = {}) {
4+
let result = transform(source, template, options);
55

66
return `==========
77
${source}
@@ -190,3 +190,15 @@ test('multi-line template', () => {
190190

191191
expect(generateSnapshot(source, template)).toMatchSnapshot();
192192
});
193+
194+
test('handles `hasComponentCSS` option correctly', () => {
195+
let source = `
196+
export default Component.extend({
197+
classNames: ['foo', 'bar:baz'],
198+
});
199+
`;
200+
201+
let template = `foo`;
202+
203+
expect(generateSnapshot(source, template, { hasComponentCSS: true })).toMatchSnapshot();
204+
});

lib/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require('fs');
12
const pkgDir = require('pkg-dir');
23
const globby = require('globby');
34
const debug = require('debug')('tagless-ember-components-codemod');
@@ -16,15 +17,18 @@ async function runForPath(path, options = {}) {
1617

1718
let log = options.log || console.log;
1819

19-
// TODO check for ember-component-css dependency
20-
2120
log(` 🔍 Searching for component files...`);
2221
let paths = await globby('app/components/**/*.js', { cwd: path });
2322
debug('componentPaths = %O', paths);
2423

24+
let pkgContent = fs.readFileSync(`${path}/package.json`);
25+
let pkg = JSON.parse(pkgContent);
26+
let hasComponentCSS =
27+
pkg.dependencies['ember-component-css'] || pkg.devDependencies['ember-component-css'];
28+
2529
for (let path of paths) {
2630
try {
27-
let tagName = transformPath(path);
31+
let tagName = transformPath(path, { hasComponentCSS });
2832
if (tagName) {
2933
log(chalk.green(`${chalk.dim(path)}: <${tagName}>...</${tagName}>`));
3034
} else {

lib/transform.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const EVENT_HANDLER_METHODS = [
4848

4949
const PLACEHOLDER = '@@@PLACEHOLDER@@@';
5050

51-
function transformPath(componentPath) {
51+
function transformPath(componentPath, options) {
5252
let debug = (fmt, ...args) => _debug(`${componentPath}: ${fmt}`, ...args);
5353

5454
let templatePath = guessTemplatePath(componentPath);
@@ -60,7 +60,7 @@ function transformPath(componentPath) {
6060
let source = fs.readFileSync(componentPath, 'utf8');
6161
let template = fs.readFileSync(templatePath, 'utf8');
6262

63-
let result = transform(source, template, { debug });
63+
let result = transform(source, template, Object.assign({}, { debug }, options));
6464

6565
if (result.tagName) {
6666
fs.writeFileSync(componentPath, result.source, 'utf8');
@@ -178,6 +178,9 @@ function transform(source, template, options = {}) {
178178

179179
// wrap existing template with root element
180180
let classNodes = [];
181+
if (options.hasComponentCSS) {
182+
classNodes.push(b.mustache('styleNamespace'));
183+
}
181184
for (let className of classNames) {
182185
classNodes.push(b.text(className));
183186
}

0 commit comments

Comments
 (0)