Skip to content

Commit

Permalink
Adjust error message for re-exported class when it has a co-located t…
Browse files Browse the repository at this point in the history
…emplate
  • Loading branch information
robinborst95 committed Aug 1, 2023
1 parent 2479dec commit d5889af
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 5 deletions.
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.`;
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

0 comments on commit d5889af

Please sign in to comment.