Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support TypeScript merging of export default declarations in template colocation #677

Merged
merged 3 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/colocated-babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ 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();
dfreeman marked this conversation as resolved.
Show resolved Hide resolved

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