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

fix(assembler): handle unknown types without crashing #501

Merged
merged 1 commit into from
May 7, 2019
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
24 changes: 22 additions & 2 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@ export class Assembler implements Emitter {

const processBaseTypes = (types: ts.Type[]) => {
for (const iface of types) {

// base is private/internal, so we continue recursively with it's own bases
if (this._isPrivateOrInternal(iface.symbol)) {
erasedBases.push(iface);
Expand Down Expand Up @@ -507,7 +506,7 @@ export class Assembler implements Emitter {
// process all "implements" clauses
const allInterfaces = new Set<string>();
for (const clause of implementsClauses) {
const { interfaces } = await this._processBaseInterfaces(fqn, clause.types.map(t => this._typeChecker.getTypeFromTypeNode(t)));
const { interfaces } = await this._processBaseInterfaces(fqn, clause.types.map(t => this._getTypeFromTypeNode(t)));
for (const ifc of (interfaces || [])) {
allInterfaces.add(ifc.fqn);
}
Expand Down Expand Up @@ -623,6 +622,17 @@ export class Assembler implements Emitter {
return _sortMembers(jsiiType);
}

/**
* Use the TypeChecker's getTypeFromTypeNode, but throw a descriptive error if it fails
*/
private _getTypeFromTypeNode(t: ts.TypeNode) {
const type = this._typeChecker.getTypeFromTypeNode(t);
if (isErrorType(type)) {
throw new Error(`Unable to resolve type: ${t.getFullText()}. This typically happens if something is wrong with your dependency closure.`);
}
return type;
}

/**
* Check that this class doesn't declare any members that are of different staticness in itself or any of its bases
*/
Expand Down Expand Up @@ -1585,3 +1595,13 @@ function noEmptyDict<T>(xs: {[key: string]: T}): {[key: string]: T} | undefined
if (Object.keys(xs).length === 0) { return undefined; }
return xs;
}

/**
* Check whether this type is the intrinsic TypeScript "error type"
*
* This type is returned if type lookup fails. Unfortunately no public
* accessors for it are exposed.
*/
function isErrorType(t: ts.Type) {
return (t as any).intrinsicName === 'error';
}
32 changes: 22 additions & 10 deletions packages/jsii/lib/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,30 @@ export class Compiler implements Emitter {

private async _consumeProgram(program: ts.Program, stdlib: string): Promise<EmitResult> {
const emit = program.emit();
if (emit.emitSkipped) {
let hasErrors = emitHasErrors(emit);
const diagnostics = [...emit.diagnostics];

if (hasErrors) {
LOG.error('Compilation errors prevented the JSII assembly from being created');
}

// we continue to do jsii checker even if there are compilation errors so that
// jsii warnings will appear.
const assembler = new Assembler(this.options.projectInfo, program, stdlib);
const assmEmit = await assembler.emit();
if (assmEmit.hasErrors) {
LOG.error('Type model errors prevented the JSII assembly from being created');
// jsii warnings will appear. However, the Assembler might throw an exception
// because broken/missing type information might lead it to fail completely.
try {
const assembler = new Assembler(this.options.projectInfo, program, stdlib);
const assmEmit = await assembler.emit();
if (assmEmit.hasErrors) {
LOG.error('Type model errors prevented the JSII assembly from being created');
}

hasErrors = hasErrors || assmEmit.hasErrors;
diagnostics.push(...assmEmit.diagnostics);
} catch (e) {
LOG.error(`Error during type model analysis: ${e}`);
}

return {
hasErrors: emit.emitSkipped || assmEmit.hasErrors,
diagnostics: [...emit.diagnostics, ...assmEmit.diagnostics]
};
return { hasErrors, diagnostics };
}

/**
Expand Down Expand Up @@ -353,3 +361,7 @@ function parseConfigHostFromCompilerHost(host: ts.CompilerHost): ts.ParseConfigH
trace: host.trace ? (s) => host.trace!(s) : undefined
};
}

function emitHasErrors(result: ts.EmitResult) {
return result.diagnostics.some(d => d.category === ts.DiagnosticCategory.Error) || result.emitSkipped;
}