Skip to content

Commit

Permalink
fix(jsii): better usage reporting of private types (#247)
Browse files Browse the repository at this point in the history
Anonymous types that are deduced by TypeScript get reported as:

    Cannot use private type __object in exported declarations

The usage location would not be reported however, because the field
'symbol.valueDeclaration' was empty.

There are locations in 'symbol.declarations' though, so we use
that as a fallback instead, which works.
  • Loading branch information
rix0rrr committed Sep 27, 2018
1 parent 6ad6b9d commit 96ac5d6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,21 @@ export class Assembler implements Emitter {
private async _getFQN(type: ts.Type): Promise<string> {
const tsName = this._typeChecker.getFullyQualifiedName(type.symbol);
const groups = tsName.match(/^\"([^\"]+)\"\.(.*)$/);
let node = type.symbol.valueDeclaration;
if (!node && type.symbol.declarations.length > 0) { node = type.symbol.declarations[0]; }
if (!groups) {
this._diagnostic(type.symbol.valueDeclaration, ts.DiagnosticCategory.Error, `Cannot use private type ${tsName} in exported declarations`);
this._diagnostic(node, ts.DiagnosticCategory.Error, `Cannot use private type ${tsName} in exported declarations`);
return tsName;
}
const [, modulePath, typeName, ] = groups;
const pkg = await _findPackageInfo(modulePath);
if (!pkg) {
this._diagnostic(type.symbol.valueDeclaration, ts.DiagnosticCategory.Error, `Could not find module for ${modulePath}`);
this._diagnostic(node, ts.DiagnosticCategory.Error, `Could not find module for ${modulePath}`);
return `unknown.${typeName}`;
}
const fqn = `${pkg.name}.${typeName}`;
if (pkg.name !== this.projectInfo.name && !this._dereference({ fqn }, type.symbol.valueDeclaration)) {
this._diagnostic(type.symbol.valueDeclaration,
this._diagnostic(node,
ts.DiagnosticCategory.Error,
`Use of foreign type not present in the ${pkg.name}'s assembly: ${fqn}`);
}
Expand Down

0 comments on commit 96ac5d6

Please sign in to comment.