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

Adjust error message for re-exported class when it has a co-located template #773

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/colocated-broccoli-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@ module.exports = class ColocatedTemplateProcessor extends Plugin {
}
);

if (hasTemplate && !jsContents.includes('export default')) {
let message = `\`${relativePath}\` does not contain a \`default export\`. Did you forget to export the component class?`;
if (hasTemplate && jsContents.includes('export { default }')) {
let message = `\`${backingClassPath}\` contains an \`export { default }\` re-export, but it has a co-located template. You must explicitly extend the component to assign it a different template.`;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this message is beautiful!

jsContents = `${jsContents}\nthrow new Error(${JSON.stringify(message)});`;
prefix = '';
} else if (hasTemplate && !jsContents.includes('export default')) {
let message = `\`${backingClassPath}\` does not contain a \`default export\`. Did you forget to export the component class?`;
jsContents = `${jsContents}\nthrow new Error(${JSON.stringify(message)});`;
prefix = '';
}
Expand Down
54 changes: 53 additions & 1 deletion node-tests/colocated-broccoli-plugin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,58 @@ describe('ColocatedTemplateCompiler', function () {
);
});

it('emits an error for re-exported components with a different template', async function () {
input.write({
'app-name-here': {
'router.js': '// stuff here',
components: {
'foo.hbs': `{{yield}}`,
'foo.js': `export { default } from 'some-place';`,
},
templates: {
'application.hbs': `{{outlet}}`,
},
},
});

let tree = new ColocatedTemplateCompiler(input.path());

output = createBuilder(tree);
await output.build();

assert.deepStrictEqual(output.read(), {
'app-name-here': {
'router.js': '// stuff here',
components: {
'foo.js': stripIndent`
export { default } from 'some-place';\nthrow new Error(\"\`app-name-here/components/foo.js\` contains an \`export { default }\` re-export, but it has a co-located template. You must explicitly extend the component to assign it a different template.\");
`,
},
templates: {
'application.hbs': '{{outlet}}',
},
},
});

await output.build();

assert.deepStrictEqual(output.changes(), {}, 'NOOP update has no changes');

input.write({
'app-name-here': {
'router.js': '// other stuff here',
},
});

await output.build();

assert.deepStrictEqual(
output.changes(),
{ 'app-name-here/router.js': 'change' },
'has only related changes'
);
});

it('works for typescript component class with template', async function () {
input.write({
'app-name-here': {
Expand Down Expand Up @@ -500,7 +552,7 @@ describe('ColocatedTemplateCompiler', function () {
'app-name-here': {
components: {
'foo.js': stripIndent`
export function whatever() {}\nthrow new Error("\`app-name-here/components/foo.hbs\` does not contain a \`default export\`. Did you forget to export the component class?");
export function whatever() {}\nthrow new Error("\`app-name-here/components/foo.js\` does not contain a \`default export\`. Did you forget to export the component class?");
`,
},
},
Expand Down
4 changes: 2 additions & 2 deletions node-tests/colocated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ describe('Colocation - Broccoli + Babel Integration (modules API: true)', functi
'app-name-here': {
components: {
'foo.js': stripIndent`
export function whatever() {}\nthrow new Error("\`app-name-here/components/foo.hbs\` does not contain a \`default export\`. Did you forget to export the component class?");
export function whatever() {}\nthrow new Error("\`app-name-here/components/foo.js\` does not contain a \`default export\`. Did you forget to export the component class?");
`,
},
},
Expand Down Expand Up @@ -797,7 +797,7 @@ describe('Colocation - Broccoli + Babel Integration (modules API: false)', funct
'app-name-here': {
components: {
'foo.js': stripIndent`
export function whatever() {}\nthrow new Error("\`app-name-here/components/foo.hbs\` does not contain a \`default export\`. Did you forget to export the component class?");
export function whatever() {}\nthrow new Error("\`app-name-here/components/foo.js\` does not contain a \`default export\`. Did you forget to export the component class?");
`,
},
},
Expand Down