Skip to content

Commit 8e9dce7

Browse files
committed
Support TypeScript components
1 parent 84ad639 commit 8e9dce7

4 files changed

Lines changed: 58 additions & 2 deletions

File tree

lib/__tests__/__snapshots__/transform.js.snap

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,38 @@ foo
559559
=========="
560560
`;
561561
562+
exports[`native components handles TypeScript components correctly 1`] = `
563+
"==========
564+
565+
import Component from '@ember/component';
566+
import { classNames } from '@ember-decorators/component';
567+
568+
@classNameBindings('foo:bar')
569+
export default class extends Component {
570+
foo: boolean = true;
571+
}
572+
573+
~~~~~~~~~~
574+
foo
575+
~~~~~~~~~~
576+
=> tagName: div
577+
~~~~~~~~~~
578+
579+
import Component from '@ember/component';
580+
import { classNames, tagName } from '@ember-decorators/component';
581+
582+
@tagName(\\"\\")
583+
export default class extends Component {
584+
foo: boolean = true;
585+
}
586+
587+
~~~~~~~~~~
588+
<div class={{if this.foo \\"bar\\"}} ...attributes>
589+
foo
590+
</div>
591+
=========="
592+
`;
593+
562594
exports[`native components handles single \`@classNames\` item correctly 1`] = `
563595
"==========
564596

lib/__tests__/guess-template.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ describe('guessTemplatePath()', () => {
88
'app/components/nested/sub/component/component.js',
99
'app/components/nested/sub/component/template.hbs',
1010
],
11+
['app/components/checkbox.ts', 'app/templates/components/checkbox.hbs'],
12+
['app/components/checkbox/component.ts', 'app/components/checkbox/template.hbs'],
13+
[
14+
'app/components/nested/sub/component/component.ts',
15+
'app/components/nested/sub/component/template.hbs',
16+
],
1117
];
1218

1319
for (let [input, expected] of TESTS) {

lib/__tests__/transform.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,4 +602,20 @@ describe('native components', function() {
602602

603603
expect(generateSnapshot(source, template, { hasComponentCSS: true })).toMatchSnapshot();
604604
});
605+
606+
test('handles TypeScript components correctly', () => {
607+
let source = `
608+
import Component from '@ember/component';
609+
import { classNames } from '@ember-decorators/component';
610+
611+
@classNameBindings('foo:bar')
612+
export default class extends Component {
613+
foo: boolean = true;
614+
}
615+
`;
616+
617+
let template = `foo`;
618+
619+
expect(generateSnapshot(source, template)).toMatchSnapshot();
620+
});
605621
});

lib/transform.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ function transform(source, template, options = {}) {
100100
}
101101

102102
function guessTemplatePath(componentPath) {
103-
let isPods = path.basename(componentPath) === 'component.js';
103+
let isPods = path.basename(componentPath, path.extname(componentPath)) === 'component';
104104
if (isPods) {
105105
return path.dirname(componentPath) + '/template.hbs';
106106
}
107107

108-
return componentPath.replace('/components/', '/templates/components/').replace(/\.js$/, '.hbs');
108+
return componentPath
109+
.replace('/components/', '/templates/components/')
110+
.replace(/\.(js|ts)$/, '.hbs');
109111
}
110112

111113
module.exports = {

0 commit comments

Comments
 (0)