Skip to content

Commit

Permalink
Denote the optional aspect of types when describing them (#159)
Browse files Browse the repository at this point in the history
Makes the error message when an optional type is overridden with a non-optional:

```
// Old, broken error message
Error: @aws-cdk/aws-codebuild.Project.role: type changed from @aws-cdk/aws-iam.Role (in @aws-cdk/aws-codebuild.ProjectRef) to @aws-cdk/aws-iam.Role

// New, fixed error message
Error: @aws-cdk/aws-codebuild.Project.role: type changed from @aws-cdk/aws-iam.Role? (in @aws-cdk/aws-codebuild.ProjectRef) to @aws-cdk/aws-iam.Role
```
  • Loading branch information
RomainMuller committed Aug 7, 2018
1 parent eb291b9 commit 45d3632
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 12 additions & 5 deletions packages/jsii-spec/lib/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,20 +569,27 @@ export function isClassOrInterfaceType(type: Type): type is (InterfaceType | Cla
export function describeTypeReference(a?: TypeReference): string {
if (a === undefined) { return '(none)'; }

const optionalMarker = a.optional ? '?' : '';

if (isNamedTypeReference(a)) {
return a.fqn;
return `${a.fqn}${optionalMarker}`;
}

if (isPrimitiveTypeReference(a)) {
return a.primitive;
return `${a.primitive}${optionalMarker}`;
}

if (isCollectionTypeReference(a)) {
return `${a.collection.kind}<${describeTypeReference(a.collection.elementtype)}>`;
return `${a.collection.kind}<${describeTypeReference(a.collection.elementtype)}>${optionalMarker}`;
}
if (isUnionTypeReference(a)) {
return a.union.types.map(describeTypeReference).join('|');
const unionType = a.union.types.map(describeTypeReference).join(' | ');
if (a.optional) {
return `(${unionType})${optionalMarker}`;
} else {
return unionType;
}
}

throw new Error('Unrecognized type reference');
}
}
4 changes: 2 additions & 2 deletions packages/jsii/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": false, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": false, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
"inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
"inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */
Expand Down

0 comments on commit 45d3632

Please sign in to comment.