Skip to content

Commit

Permalink
fix(jsii): do not mark "any" or "unknown" as optional (unless "?")
Browse files Browse the repository at this point in the history
We accidentally marked every "any" and "unknown" types as optional,
regardless if the declaration had a question mark or not.

Fixes #284
  • Loading branch information
Elad Ben-Israel committed Nov 7, 2018
1 parent b7b91db commit 5feacf9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
3 changes: 1 addition & 2 deletions packages/jsii-calc-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
},
"name": "typeName",
"returns": {
"optional": true,
"primitive": "any"
}
}
Expand Down Expand Up @@ -103,5 +102,5 @@
}
},
"version": "0.7.8",
"fingerprint": "K1rAUs6WiQ5lF08T46B8v/5UL8T8Ot59e0Nc8rh2jiQ="
"fingerprint": "FbKHAP60R40tOOnvhCeDrZDrNlgzIypOfAdWPed+Jog="
}
9 changes: 9 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -939,3 +939,12 @@ export interface IInterfaceWithMethods {
export interface IInterfaceThatShouldNotBeADataType extends IInterfaceWithMethods {
readonly otherValue: string;
}

/**
* jsii#284: do not recognize "any" as an optional argument
*/
export class DoNotRecognizeAnyAsOptional {
public method(_requiredAny: any, _optionalAny?: any, _optionalString?: string) {

}
}
62 changes: 47 additions & 15 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@
"type": {
"collection": {
"elementtype": {
"optional": true,
"primitive": "any"
},
"kind": "array"
Expand All @@ -394,13 +393,18 @@
"type": {
"collection": {
"elementtype": {
"optional": true,
"primitive": "any"
},
"kind": "map"
}
}
},
{
"name": "anyProperty",
"type": {
"primitive": "any"
}
},
{
"name": "arrayProperty",
"type": {
Expand Down Expand Up @@ -528,7 +532,6 @@
"type": {
"collection": {
"elementtype": {
"optional": true,
"primitive": "any"
},
"kind": "array"
Expand All @@ -540,17 +543,15 @@
"type": {
"collection": {
"elementtype": {
"optional": true,
"primitive": "any"
},
"kind": "map"
}
}
},
{
"name": "anyProperty",
"name": "unknownProperty",
"type": {
"optional": true,
"primitive": "any"
}
},
Expand All @@ -560,13 +561,6 @@
"fqn": "jsii-calc.StringEnum",
"optional": true
}
},
{
"name": "unknownProperty",
"type": {
"optional": true,
"primitive": "any"
}
}
]
},
Expand Down Expand Up @@ -1301,6 +1295,45 @@
],
"name": "DoNotOverridePrivates"
},
"jsii-calc.DoNotRecognizeAnyAsOptional": {
"assembly": "jsii-calc",
"docs": {
"comment": "jsii#284: do not recognize \"any\" as an optional argument"
},
"fqn": "jsii-calc.DoNotRecognizeAnyAsOptional",
"initializer": {
"initializer": true
},
"kind": "class",
"methods": [
{
"name": "method",
"parameters": [
{
"name": "_requiredAny",
"type": {
"primitive": "any"
}
},
{
"name": "_optionalAny",
"type": {
"optional": true,
"primitive": "any"
}
},
{
"name": "_optionalString",
"type": {
"optional": true,
"primitive": "string"
}
}
]
}
],
"name": "DoNotRecognizeAnyAsOptional"
},
"jsii-calc.DoubleTrouble": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.DoubleTrouble",
Expand Down Expand Up @@ -3062,7 +3095,6 @@
{
"name": "value",
"returns": {
"optional": true,
"primitive": "any"
}
}
Expand Down Expand Up @@ -3412,5 +3444,5 @@
}
},
"version": "0.7.8",
"fingerprint": "Xn7Rk17rqR3AaMx3+ssxT0GR1sCWwz0OGC+C8QuLI3A="
"fingerprint": "2BaszImarh4WChl9DFUcygfTpEfXU17fHQT2wgEptfM="
}
4 changes: 2 additions & 2 deletions packages/jsii/lib/assembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ export class Assembler implements Emitter {
this._diagnostic(declaration,
ts.DiagnosticCategory.Error,
`Only string index maps are supported`);
elementtype = { primitive: spec.PrimitiveType.Any, optional: true };
elementtype = { primitive: spec.PrimitiveType.Any };
}
return {
collection: {
Expand All @@ -830,7 +830,7 @@ export class Assembler implements Emitter {
}
// tslint:disable-next-line:no-bitwise
if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
return { primitive: spec.PrimitiveType.Any, optional: true };
return { primitive: spec.PrimitiveType.Any };
}
} else if (type.symbol.valueDeclaration && isUnder(type.symbol.valueDeclaration.getSourceFile().fileName, this.stdlib)) {
switch (type.symbol.name) {
Expand Down

0 comments on commit 5feacf9

Please sign in to comment.