Skip to content

Commit

Permalink
Merge pull request #677 from dfreeman/ts-declaration-merging
Browse files Browse the repository at this point in the history
Support TypeScript merging of export default declarations in template colocation
  • Loading branch information
rwjblue authored Mar 7, 2021
2 parents 8e3b2e2 + 52d9a34 commit f0efdd2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/colocated-babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
26 changes: 26 additions & 0 deletions node-tests/colocated-babel-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit f0efdd2

Please sign in to comment.