Skip to content

Commit 5feacf9

Browse files
author
Elad Ben-Israel
committed
fix(jsii): do not mark "any" or "unknown" as optional (unless "?")
We accidentally marked every "any" and "unknown" types as optional, regardless if the declaration had a question mark or not. Fixes #284
1 parent b7b91db commit 5feacf9

File tree

4 files changed

+59
-19
lines changed

4 files changed

+59
-19
lines changed

packages/jsii-calc-base/test/assembly.jsii

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
},
7474
"name": "typeName",
7575
"returns": {
76-
"optional": true,
7776
"primitive": "any"
7877
}
7978
}
@@ -103,5 +102,5 @@
103102
}
104103
},
105104
"version": "0.7.8",
106-
"fingerprint": "K1rAUs6WiQ5lF08T46B8v/5UL8T8Ot59e0Nc8rh2jiQ="
105+
"fingerprint": "FbKHAP60R40tOOnvhCeDrZDrNlgzIypOfAdWPed+Jog="
107106
}

packages/jsii-calc/lib/compliance.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,3 +939,12 @@ export interface IInterfaceWithMethods {
939939
export interface IInterfaceThatShouldNotBeADataType extends IInterfaceWithMethods {
940940
readonly otherValue: string;
941941
}
942+
943+
/**
944+
* jsii#284: do not recognize "any" as an optional argument
945+
*/
946+
export class DoNotRecognizeAnyAsOptional {
947+
public method(_requiredAny: any, _optionalAny?: any, _optionalString?: string) {
948+
949+
}
950+
}

packages/jsii-calc/test/assembly.jsii

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@
382382
"type": {
383383
"collection": {
384384
"elementtype": {
385-
"optional": true,
386385
"primitive": "any"
387386
},
388387
"kind": "array"
@@ -394,13 +393,18 @@
394393
"type": {
395394
"collection": {
396395
"elementtype": {
397-
"optional": true,
398396
"primitive": "any"
399397
},
400398
"kind": "map"
401399
}
402400
}
403401
},
402+
{
403+
"name": "anyProperty",
404+
"type": {
405+
"primitive": "any"
406+
}
407+
},
404408
{
405409
"name": "arrayProperty",
406410
"type": {
@@ -528,7 +532,6 @@
528532
"type": {
529533
"collection": {
530534
"elementtype": {
531-
"optional": true,
532535
"primitive": "any"
533536
},
534537
"kind": "array"
@@ -540,17 +543,15 @@
540543
"type": {
541544
"collection": {
542545
"elementtype": {
543-
"optional": true,
544546
"primitive": "any"
545547
},
546548
"kind": "map"
547549
}
548550
}
549551
},
550552
{
551-
"name": "anyProperty",
553+
"name": "unknownProperty",
552554
"type": {
553-
"optional": true,
554555
"primitive": "any"
555556
}
556557
},
@@ -560,13 +561,6 @@
560561
"fqn": "jsii-calc.StringEnum",
561562
"optional": true
562563
}
563-
},
564-
{
565-
"name": "unknownProperty",
566-
"type": {
567-
"optional": true,
568-
"primitive": "any"
569-
}
570564
}
571565
]
572566
},
@@ -1301,6 +1295,45 @@
13011295
],
13021296
"name": "DoNotOverridePrivates"
13031297
},
1298+
"jsii-calc.DoNotRecognizeAnyAsOptional": {
1299+
"assembly": "jsii-calc",
1300+
"docs": {
1301+
"comment": "jsii#284: do not recognize \"any\" as an optional argument"
1302+
},
1303+
"fqn": "jsii-calc.DoNotRecognizeAnyAsOptional",
1304+
"initializer": {
1305+
"initializer": true
1306+
},
1307+
"kind": "class",
1308+
"methods": [
1309+
{
1310+
"name": "method",
1311+
"parameters": [
1312+
{
1313+
"name": "_requiredAny",
1314+
"type": {
1315+
"primitive": "any"
1316+
}
1317+
},
1318+
{
1319+
"name": "_optionalAny",
1320+
"type": {
1321+
"optional": true,
1322+
"primitive": "any"
1323+
}
1324+
},
1325+
{
1326+
"name": "_optionalString",
1327+
"type": {
1328+
"optional": true,
1329+
"primitive": "string"
1330+
}
1331+
}
1332+
]
1333+
}
1334+
],
1335+
"name": "DoNotRecognizeAnyAsOptional"
1336+
},
13041337
"jsii-calc.DoubleTrouble": {
13051338
"assembly": "jsii-calc",
13061339
"fqn": "jsii-calc.DoubleTrouble",
@@ -3062,7 +3095,6 @@
30623095
{
30633096
"name": "value",
30643097
"returns": {
3065-
"optional": true,
30663098
"primitive": "any"
30673099
}
30683100
}
@@ -3412,5 +3444,5 @@
34123444
}
34133445
},
34143446
"version": "0.7.8",
3415-
"fingerprint": "Xn7Rk17rqR3AaMx3+ssxT0GR1sCWwz0OGC+C8QuLI3A="
3447+
"fingerprint": "2BaszImarh4WChl9DFUcygfTpEfXU17fHQT2wgEptfM="
34163448
}

packages/jsii/lib/assembler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ export class Assembler implements Emitter {
812812
this._diagnostic(declaration,
813813
ts.DiagnosticCategory.Error,
814814
`Only string index maps are supported`);
815-
elementtype = { primitive: spec.PrimitiveType.Any, optional: true };
815+
elementtype = { primitive: spec.PrimitiveType.Any };
816816
}
817817
return {
818818
collection: {
@@ -830,7 +830,7 @@ export class Assembler implements Emitter {
830830
}
831831
// tslint:disable-next-line:no-bitwise
832832
if (type.flags & (ts.TypeFlags.Any | ts.TypeFlags.Unknown)) {
833-
return { primitive: spec.PrimitiveType.Any, optional: true };
833+
return { primitive: spec.PrimitiveType.Any };
834834
}
835835
} else if (type.symbol.valueDeclaration && isUnder(type.symbol.valueDeclaration.getSourceFile().fileName, this.stdlib)) {
836836
switch (type.symbol.name) {

0 commit comments

Comments
 (0)