Skip to content

Commit

Permalink
fix(jsii): unable to use nested types from dependencies
Browse files Browse the repository at this point in the history
Using `--project-references`, nested types from dependencies could not
be used as they would result in the following `jsii` error:

```
Type "<type name>" cannot be used as the return type because it is private or @internal
```

THis is because in `--project-references` mode, the ambient declaration
for the nested type is the declaration that gets resolved to, and those
cannot have the `export` keyword: instead, the surrounding module
declaration is annotated with `export ambient`.

This adds the necessary code path to actually identify this scenario and
to appropriately detect the type is actually visible and exported.

The check that was failing had been added in #1861 as a way to provide a
more helpful error message when private or `@internal` types are
mistakenly used on exported APIs; which explains why this situation did
not previously occur.
  • Loading branch information
RomainMuller committed Aug 10, 2020
1 parent 2b9d07c commit e30d36a
Show file tree
Hide file tree
Showing 10 changed files with 523 additions and 27 deletions.
29 changes: 20 additions & 9 deletions packages/@scope/jsii-calc-lib/lib/submodule/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@ export interface ReflectableEntry {
}

export class Reflector {
public constructor() { }

public asMap(reflectable: IReflectable): Record<string, unknown> {
return reflectable.entries.reduce(
(mapping, entry) => {
mapping[entry.key] = entry.value;
return mapping;
},
{} as Record<string, unknown>,
);
return reflectable.entries.reduce((mapping, entry) => {
mapping[entry.key] = entry.value;
return mapping;
}, {} as Record<string, unknown>);
}
}

/**
* This class is here to show we can use nested classes across module boundaries.
*/
export class NestingClass {
private constructor() {}
}
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace NestingClass {
/**
* This class is here to show we can use nested classes across module boundaries.
*/
export class NestedClass {
public readonly property: string = 'property';
}
}
59 changes: 53 additions & 6 deletions packages/@scope/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,57 @@
}
]
},
"@scope/jsii-calc-lib.submodule.NestingClass": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
"stability": "deprecated",
"summary": "This class is here to show we can use nested classes across module boundaries."
},
"fqn": "@scope/jsii-calc-lib.submodule.NestingClass",
"kind": "class",
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 22
},
"name": "NestingClass",
"namespace": "submodule"
},
"@scope/jsii-calc-lib.submodule.NestingClass.NestedClass": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
"stability": "deprecated",
"summary": "This class is here to show we can use nested classes across module boundaries."
},
"fqn": "@scope/jsii-calc-lib.submodule.NestingClass.NestedClass",
"initializer": {
"docs": {
"stability": "deprecated"
}
},
"kind": "class",
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 30
},
"name": "NestedClass",
"namespace": "submodule.NestingClass",
"properties": [
{
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 31
},
"name": "property",
"type": {
"primitive": "string"
}
}
]
},
"@scope/jsii-calc-lib.submodule.ReflectableEntry": {
"assembly": "@scope/jsii-calc-lib",
"datatype": true,
Expand Down Expand Up @@ -630,10 +681,6 @@
"initializer": {
"docs": {
"stability": "deprecated"
},
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 11
}
},
"kind": "class",
Expand All @@ -648,7 +695,7 @@
},
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 13
"line": 11
},
"name": "asMap",
"parameters": [
Expand Down Expand Up @@ -676,5 +723,5 @@
}
},
"version": "0.0.0",
"fingerprint": "fVfpIK7xUajlT1zkHIJ8uYJPvy0gLgEe5BM8afu1mVg="
"fingerprint": "f/4VuNiOkSgTgLR80loQUAzAuzFi+25rmfLcRWKDCrY="
}
1 change: 1 addition & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './calculator';
export * from './compliance';
export * from './documented';
export * from './erasures';
export * from './nested-class';
export * from './stability';
export * from './submodules';

Expand Down
9 changes: 9 additions & 0 deletions packages/jsii-calc/lib/nested-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { submodule } from '@scope/jsii-calc-lib';

export class NestedClassInstance {
public static makeInstance(): submodule.NestingClass.NestedClass {
return new submodule.NestingClass.NestedClass();
}

private constructor() {}
}
35 changes: 33 additions & 2 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@
"jsii-calc.submodule": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 8
"line": 9
}
},
"jsii-calc.submodule.back_references": {
Expand Down Expand Up @@ -8373,6 +8373,37 @@
}
]
},
"jsii-calc.NestedClassInstance": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.NestedClassInstance",
"kind": "class",
"locationInModule": {
"filename": "lib/nested-class.ts",
"line": 3
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/nested-class.ts",
"line": 4
},
"name": "makeInstance",
"returns": {
"type": {
"fqn": "@scope/jsii-calc-lib.submodule.NestingClass.NestedClass"
}
},
"static": true
}
],
"name": "NestedClassInstance"
},
"jsii-calc.NestedStruct": {
"assembly": "jsii-calc",
"datatype": true,
Expand Down Expand Up @@ -13722,5 +13753,5 @@
}
},
"version": "0.0.0",
"fingerprint": "AksOoFurRMyuF7gSjHXHbIRIIjq0e0R0CvpHycvyI3U="
"fingerprint": "mLU0hvK9Teaq2GAeeCGFPwAZab85G7lFIH/VCKgAvp4="
}
3 changes: 1 addition & 2 deletions packages/jsii-pacmak/lib/targets/dotnet/dotnetgenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ export class DotNetGenerator extends Generator {

// Nested classes will be dealt with during calc code generation
const nested = this.isNested(cls);
const inner = nested ? ' static' : '';
const absPrefix = abstract ? ' abstract' : '';

this.openFileIfNeeded(className, namespace, nested);
Expand All @@ -294,7 +293,7 @@ export class DotNetGenerator extends Generator {
this.dotnetRuntimeGenerator.emitAttributesForClass(cls);

this.code.openBlock(
`public${inner}${absPrefix} class ${className}${implementsExpr}`,
`public${absPrefix} class ${className}${implementsExpr}`,
);

// Compute the class parameters
Expand Down
Loading

0 comments on commit e30d36a

Please sign in to comment.