Skip to content

Improve error reporting when failing to associate a companion template #56

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

Merged
merged 1 commit into from
Mar 9, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions packages/transform/__tests__/rewrite.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,68 @@ describe('rewriteModule', () => {
`);
});

test('with no default value export', () => {
let script = {
filename: 'test.ts',
contents: stripIndent`
import Component from '@glimmer/component';
export class MyComponent extends Component {}
`,
};

let template = {
filename: 'test.hbs',
contents: stripIndent``,
};

let transformedModule = rewriteModule({ script, template }, emberLooseEnvironment);

expect(transformedModule?.errors).toEqual([
{
message:
'Modules with an associated template must have a default export that is a class declaration or expression',
source: script,
location: {
start: 0,
end: script.contents.length,
},
},
]);

expect(transformedModule?.getOriginalRange(0, script.contents.length)).toEqual({
source: script,
start: 0,
end: script.contents.length,
});
});

test('with an unresolvable default export', () => {
let script = {
filename: 'test.ts',
contents: stripIndent`
export default Foo;
`,
};

let template = {
filename: 'test.hbs',
contents: stripIndent``,
};

let transformedModule = rewriteModule({ script, template }, emberLooseEnvironment);

expect(transformedModule?.errors).toEqual([
{
message: 'Unable to resolve a class body to associate a template declaration to',
source: script,
location: {
start: script.contents.indexOf('Foo'),
end: script.contents.indexOf(';'),
},
},
]);
});

test('with a class with default export in module augmentation', () => {
let script = {
filename: 'test.ts',
Expand Down
15 changes: 13 additions & 2 deletions packages/transform/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ function calculateCorrelatedSpans(
): CorrelatedSpansResult {
let errors: Array<TransformError> = [];
let partialSpans: Array<PartialCorrelatedSpan> = [];
let defaultExportSeen = false;

traverse(ast, {
TaggedTemplateExpression(path) {
Expand All @@ -152,15 +153,25 @@ function calculateCorrelatedSpans(
},

ExportDefaultDeclaration(path) {
if (template) {
let result = calculateCompanionTemplateSpans(path, script, template, environment);
let declaration = path.get('declaration');
if (template && (declaration.isClass() || declaration.isExpression())) {
let result = calculateCompanionTemplateSpans(declaration, script, template, environment);

defaultExportSeen = true;
errors.push(...result.errors);
partialSpans.push(...result.partialSpans);
}
},
});

if (template && !defaultExportSeen) {
errors.push({
message: `Modules with an associated template must have a default export that is a class declaration or expression`,
source: script,
location: { start: 0, end: script.contents.length },
});
}

return { errors, partialSpans };
}

Expand Down
14 changes: 8 additions & 6 deletions packages/transform/src/inlining/companion-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { assert } from '../util';
const STANDALONE_TEMPLATE_FIELD = `'~template'`;

export function calculateCompanionTemplateSpans(
path: NodePath<t.ExportDefaultDeclaration>,
path: NodePath<t.Class> | NodePath<t.Expression>,
script: SourceFile,
template: SourceFile,
environment: GlintEnvironment
Expand All @@ -30,9 +30,12 @@ export function calculateCompanionTemplateSpans(
let targetPath = findCompanionTemplateTarget(path);
if (!targetPath) {
errors.push({
message: `Unable to resolve a class body to associate a template declaration to`,
source: script,
location: { start: 0, end: script.contents.length },
message: `Modules with an associated template must have a default export`,
location: {
start: path.node.start ?? 0,
end: path.node.end ?? script.contents.length,
},
});

return { errors, partialSpans };
Expand Down Expand Up @@ -124,11 +127,10 @@ export function calculateCompanionTemplateSpans(
type CompanionTemplateTarget = NodePath<t.Class> | NodePath<t.Expression> | null;

function findCompanionTemplateTarget(
path: NodePath<t.ExportDefaultDeclaration>
declaration: NodePath<t.Class> | NodePath<t.Expression>
): CompanionTemplateTarget {
let declaration = path.get('declaration');
let value = declaration.isIdentifier()
? path.scope.getBinding(declaration.node.name)?.path
? declaration.scope.getBinding(declaration.node.name)?.path
: declaration;

if (value?.isClass() || value?.isExpression()) {
Expand Down
5 changes: 4 additions & 1 deletion packages/transform/src/transformed-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ export default class TransformedModule {
let startInfo = this.determineOriginalOffsetAndSpan(transformedStart);
let endInfo = this.determineOriginalOffsetAndSpan(transformedEnd);

assert(startInfo.correlatedSpan.originalFile === endInfo.correlatedSpan.originalFile);
assert(
startInfo.correlatedSpan.originalFile === endInfo.correlatedSpan.originalFile,
'Attempted to transform a range across two different files'
);

let source = startInfo.correlatedSpan.originalFile;
let start = startInfo.originalOffset;
Expand Down