Skip to content

Commit

Permalink
fix(jsii): unable to use type from dependencies' submodules (#1557)
Browse files Browse the repository at this point in the history
When referring to a type defined in a submodule of a dependency (but
not using the "inline `namespace`" syntax style), the `jsii` compiler
failed to account for the namespace segment and generated an incorrect
`fqn`, leading to a type resolution error failing the compilation.

This adds a pass in the compiler to gather submodule layout from
dependencies and correctly register them in the compilation context, so
correct `fqn` are emitted.

New types have been introduced in `@scope/jsii-calc-lib` and `jsii-calc`
to verify that the compiler produces reasonable output. Those will be
leveraged in new compliance tests in a subsequent PR, as some
outstanding code-generation issues come in the way of these tests.
  • Loading branch information
RomainMuller authored Apr 24, 2020
1 parent e016163 commit ba7fac2
Show file tree
Hide file tree
Showing 38 changed files with 1,357 additions and 33 deletions.
4 changes: 3 additions & 1 deletion packages/@scope/jsii-calc-lib/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,6 @@ export enum EnumFromScopedModule {
*/
export interface IThreeLevelsInterface extends base.IBaseInterface {
baz(): void;
}
}

export * as submodule from './submodule';
22 changes: 22 additions & 0 deletions packages/@scope/jsii-calc-lib/lib/submodule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface IReflectable {
readonly entries: ReflectableEntry[];
}

export interface ReflectableEntry {
readonly key: string;
readonly value: unknown;
}

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>,
);
}
}
142 changes: 141 additions & 1 deletion packages/@scope/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
"url": "https://github.com/aws/jsii.git"
},
"schema": "jsii/0.10.0",
"submodules": {
"@scope/jsii-calc-lib.submodule": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 112
}
}
},
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.LibNamespace",
Expand Down Expand Up @@ -514,8 +522,140 @@
}
}
]
},
"@scope/jsii-calc-lib.submodule.IReflectable": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
"stability": "deprecated"
},
"fqn": "@scope/jsii-calc-lib.submodule.IReflectable",
"kind": "interface",
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 1
},
"name": "IReflectable",
"namespace": "submodule",
"properties": [
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 2
},
"name": "entries",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-lib.submodule.ReflectableEntry"
},
"kind": "array"
}
}
}
]
},
"@scope/jsii-calc-lib.submodule.ReflectableEntry": {
"assembly": "@scope/jsii-calc-lib",
"datatype": true,
"docs": {
"stability": "deprecated"
},
"fqn": "@scope/jsii-calc-lib.submodule.ReflectableEntry",
"kind": "interface",
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 5
},
"name": "ReflectableEntry",
"namespace": "submodule",
"properties": [
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 6
},
"name": "key",
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "deprecated"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 7
},
"name": "value",
"type": {
"primitive": "any"
}
}
]
},
"@scope/jsii-calc-lib.submodule.Reflector": {
"assembly": "@scope/jsii-calc-lib",
"docs": {
"stability": "deprecated"
},
"fqn": "@scope/jsii-calc-lib.submodule.Reflector",
"initializer": {
"docs": {
"stability": "deprecated"
}
},
"kind": "class",
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 10
},
"methods": [
{
"docs": {
"stability": "deprecated"
},
"locationInModule": {
"filename": "lib/submodule/index.ts",
"line": 13
},
"name": "asMap",
"parameters": [
{
"name": "reflectable",
"type": {
"fqn": "@scope/jsii-calc-lib.submodule.IReflectable"
}
}
],
"returns": {
"type": {
"collection": {
"elementtype": {
"primitive": "any"
},
"kind": "map"
}
}
}
}
],
"name": "Reflector",
"namespace": "submodule"
}
},
"version": "0.0.0",
"fingerprint": "7VgHmQKZ2ceAa99bmuocZRLFxQths+Sf35amRt1SZpE="
"fingerprint": "+7cEXNA9wAn/mht3AcwDtIFLvsfZoQhQGBQ4J1pPCO4="
}
1 change: 1 addition & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './compliance';
export * from './documented';
export * from './erasures';
export * from './stability';
export * from './submodules';

export * as submodule from './submodule';
15 changes: 15 additions & 0 deletions packages/jsii-calc/lib/submodules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { submodule } from '@scope/jsii-calc-lib';

/**
* Ensures submodule-imported types from dependencies can be used correctly.
*/
export class UpcasingReflectable implements submodule.IReflectable {
public static readonly reflector = new submodule.Reflector();

public constructor(private readonly delegate: Record<string, unknown>) { }

public get entries(): submodule.ReflectableEntry[] {
return Object.entries(this.delegate)
.map(([key, value]) => ({ key: key.toLocaleUpperCase(), value }));
}
}
91 changes: 89 additions & 2 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@
}
},
"@scope/jsii-calc-lib": {
"submodules": {
"@scope/jsii-calc-lib.submodule": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 112
}
}
},
"targets": {
"dotnet": {
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.LibNamespace",
Expand Down Expand Up @@ -138,6 +146,12 @@
},
"schema": "jsii/0.10.0",
"submodules": {
"@scope/jsii-calc-lib.submodule": {
"locationInModule": {
"filename": "../@scope/jsii-calc-lib/build/index.d.ts",
"line": 94
}
},
"jsii-calc.DerivedClassHasNoProperties": {
"locationInModule": {
"filename": "lib/compliance.ts",
Expand Down Expand Up @@ -165,7 +179,7 @@
"jsii-calc.submodule": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 7
"line": 8
}
},
"jsii-calc.submodule.back_references": {
Expand Down Expand Up @@ -11519,6 +11533,79 @@
}
]
},
"jsii-calc.UpcasingReflectable": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental",
"summary": "Ensures submodule-imported types from dependencies can be used correctly."
},
"fqn": "jsii-calc.UpcasingReflectable",
"initializer": {
"docs": {
"stability": "experimental"
},
"parameters": [
{
"name": "delegate",
"type": {
"collection": {
"elementtype": {
"primitive": "any"
},
"kind": "map"
}
}
}
]
},
"interfaces": [
"@scope/jsii-calc-lib.submodule.IReflectable"
],
"kind": "class",
"locationInModule": {
"filename": "lib/submodules.ts",
"line": 6
},
"name": "UpcasingReflectable",
"properties": [
{
"const": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodules.ts",
"line": 7
},
"name": "reflector",
"static": true,
"type": {
"fqn": "@scope/jsii-calc-lib.submodule.Reflector"
}
},
{
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/submodules.ts",
"line": 11
},
"name": "entries",
"overrides": "@scope/jsii-calc-lib.submodule.IReflectable",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-lib.submodule.ReflectableEntry"
},
"kind": "array"
}
}
}
]
},
"jsii-calc.UseBundledDependency": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -12498,5 +12585,5 @@
}
},
"version": "0.0.0",
"fingerprint": "LPI1XbecdL/VPbXVTmbAHHV0ox1k1cAxzrWF/f0rIxI="
"fingerprint": "DX1QMQgQSTuLzsXpnv5YcFSvPRw/30xHOP5U+5pQY0o="
}
9 changes: 8 additions & 1 deletion packages/jsii-pacmak/lib/targets/python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,14 @@ class PythonModule implements PythonType {
// We need to make sure direct dependencies are always loaded...
for (const dep of Object.keys(this.assembly.dependencies ?? {})) {
const depConfig = this.assembly.dependencyClosure![dep];
deps.add(depConfig.targets!.python!.module);

const packageName = depConfig.targets!.python!.module;
deps.add(packageName);

for (const [submoduleName] of Object.entries(depConfig.submodules ?? {})) {
const [, ...parts] = submoduleName.split('.');
deps.add([packageName, ...parts.map(part => toSnakeCase(part))].join('.'));
}
}

// Now actually write the import statements...
Expand Down
Loading

0 comments on commit ba7fac2

Please sign in to comment.