Skip to content

Commit

Permalink
Fix #221: improve diagnostics when dereference fails
Browse files Browse the repository at this point in the history
In some cases, when a type could not be referenced, the diagnostic 
message doesn't indicate that (the undefined return value will still 
cause an error, but we also want to know that we couldn't deref the 
type)
  • Loading branch information
Elad Ben-Israel committed Sep 12, 2018
1 parent e80a889 commit fc805f9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,21 @@ export class Assembler implements Emitter {
*
* @returns the de-referenced type, if it was found, otherwise ``undefined``.
*/
private _dereference(ref: spec.NamedTypeReference): spec.Type | undefined {
private _dereference(ref: spec.NamedTypeReference, referencingNode: ts.Node): spec.Type | undefined {
const [assm, ] = ref.fqn.split('.');
let type;
if (assm === this.projectInfo.name) {
return this._types[ref.fqn];
type = this._types[ref.fqn];
} else {
const assembly = this.projectInfo.transitiveDependencies.find(dep => dep.name === assm);
return assembly && assembly.types && assembly.types[ref.fqn];
type = assembly && assembly.types && assembly.types[ref.fqn];
}

if (!type) {
this._diagnostic(referencingNode, ts.DiagnosticCategory.Error, `Unable to resolve referenced type '${ref.fqn}'. Missing export?`);
}

return type;
}

private _diagnostic(node: ts.Node | null, category: ts.DiagnosticCategory, messageText: string) {
Expand Down Expand Up @@ -206,7 +213,7 @@ export class Assembler implements Emitter {
return `unknown.${typeName}`;
}
const fqn = `${pkg.name}.${typeName}`;
if (pkg.name !== this.projectInfo.name && !this._dereference({ fqn })) {
if (pkg.name !== this.projectInfo.name && !this._dereference({ fqn }, type.symbol.valueDeclaration)) {
this._diagnostic(type.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
`Use of foreign type not present in the ${pkg.name}'s assembly: ${fqn}`);
Expand Down Expand Up @@ -314,10 +321,10 @@ export class Assembler implements Emitter {
continue;
}
this._defer(() => {
if (!spec.isClassType(this._dereference(ref))) {
if (!spec.isClassType(this._dereference(ref, base.symbol.valueDeclaration))) {
this._diagnostic(base.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
`Base type of ${jsiiType.fqn} is not a class (${spec.describeTypeReference(ref)})`);
`Base type of ${jsiiType.fqn} is not a class or cannot be dereferenced (${spec.describeTypeReference(ref)})`);
}
});
jsiiType.base = ref;
Expand All @@ -340,7 +347,7 @@ export class Assembler implements Emitter {
continue;
}
this._defer(() => {
if (!spec.isInterfaceType(this._dereference(typeRef))) {
if (!spec.isInterfaceType(this._dereference(typeRef, expression))) {
this._diagnostic(expression,
ts.DiagnosticCategory.Error,
`Implements clause of ${jsiiType.fqn} uses ${spec.describeTypeReference(typeRef)} as an interface`);
Expand Down Expand Up @@ -393,7 +400,7 @@ export class Assembler implements Emitter {
}
} else if (jsiiType.base) {
this._defer(() => {
const baseType = this._dereference(jsiiType.base!);
const baseType = this._dereference(jsiiType.base!, type.symbol.valueDeclaration);
if (!baseType) {
this._diagnostic(type.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
Expand All @@ -403,7 +410,7 @@ export class Assembler implements Emitter {
} else {
this._diagnostic(type.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
`Base type of ${jsiiType.fqn} (${jsiiType.base!.fqn}) is not a class`);
`Base type of ${jsiiType.fqn} (${jsiiType.base!.fqn}) is not a class or cannot be dereferenced`);
}
});
} else {
Expand Down Expand Up @@ -499,12 +506,14 @@ export class Assembler implements Emitter {
continue;
}
this._defer(() => {
if (!spec.isInterfaceType(this._dereference(ref))) {
const baseType = this._dereference(ref);
const baseType = this._dereference(ref, base.symbol.valueDeclaration);
if (!spec.isInterfaceType(baseType)) {
if (baseType) {
// tslint:disable:max-line-length
this._diagnostic(base.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
`Base type of ${jsiiType.fqn} is not an interface (${baseType.kind} ${spec.describeTypeReference(ref)})`);
`Base type of ${jsiiType.fqn} is not an interface or cannot be dereferenced (${baseType.kind} ${spec.describeTypeReference(ref)})`);
// tslint:enable:max-line-length
} else {
this._diagnostic(base.symbol.valueDeclaration,
ts.DiagnosticCategory.Error,
Expand Down
10 changes: 10 additions & 0 deletions packages/jsii/test/negatives/neg.deref-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
///!MATCH_ERROR: Unable to resolve referenced type 'Base'. Missing export?
///!MATCH_ERROR: Base type of jsii.Derived is not a class or cannot be dereferenced (Base)

class Base {

}

export class Derived extends Base {

}

0 comments on commit fc805f9

Please sign in to comment.