diff --git a/lib/colocated-babel-plugin.js b/lib/colocated-babel-plugin.js index 221128ff..544c2cb0 100644 --- a/lib/colocated-babel-plugin.js +++ b/lib/colocated-babel-plugin.js @@ -22,7 +22,17 @@ module.exports = function (babel) { }, ExportDefaultDeclaration(path, state) { - if (state.colocatedTemplateFound !== true || state.setComponentTemplateInjected === true) { + let defaultExportDeclarationPath = path.get('declaration'); + let defaultExportIsExpressionOrClass = + defaultExportDeclarationPath.isClass() || + defaultExportDeclarationPath.isExpression() || + defaultExportDeclarationPath.isFunction(); + + if ( + state.colocatedTemplateFound !== true || + state.setComponentTemplateInjected === true || + !defaultExportIsExpressionOrClass + ) { return; } diff --git a/node-tests/colocated-babel-plugin-test.js b/node-tests/colocated-babel-plugin-test.js index 391d3132..01b8ce78 100644 --- a/node-tests/colocated-babel-plugin-test.js +++ b/node-tests/colocated-babel-plugin-test.js @@ -5,6 +5,7 @@ const babel = require('@babel/core'); const { stripIndent } = require('common-tags'); const ColocatedBabelPlugin = require('../lib/colocated-babel-plugin'); const DecoratorsPlugin = [require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }]; +const TypeScriptPlugin = [require.resolve('@babel/plugin-transform-typescript')]; const ClassPropertiesPlugin = [ require.resolve('@babel/plugin-proposal-class-properties'), { loose: true }, @@ -68,6 +69,31 @@ describe('ColocatedBabelPlugin', function () { ); }); + it('can be used with TypeScript merged declarations', function () { + let { code } = babel.transformSync( + stripIndent` + import Component from 'somewhere'; + const __COLOCATED_TEMPLATE__ = 'ok'; + type MyArgs = { required: string; optional?: number }; + + export default interface MyComponent extends MyArgs {} + export default class MyComponent extends Component {} + `, + { plugins: [ColocatedBabelPlugin, TypeScriptPlugin] } + ); + + assert.strictEqual( + code, + stripIndent` + import Component from 'somewhere'; + const __COLOCATED_TEMPLATE__ = 'ok'; + export default class MyComponent extends Component {} + + Ember._setComponentTemplate(__COLOCATED_TEMPLATE__, MyComponent); + ` + ); + }); + it('sets the template for non-class default exports', function () { let { code } = babel.transformSync( stripIndent`