Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jsii): introduce submodules feature #1297

Merged
merged 26 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6e08662
feat(jsii): introduce submodules feature
RomainMuller Feb 27, 2020
8161f8f
Merge branch 'master' into rmuller/submodules
RomainMuller Mar 10, 2020
bfcdc22
address PR feedback
RomainMuller Mar 10, 2020
814e395
submodule some declarations in jsii-calc.
RomainMuller Mar 10, 2020
4cd2e17
Merge remote-tracking branch 'origin/master' into rmuller/submodules
RomainMuller Mar 10, 2020
bbb7406
Fix the python stuff
RomainMuller Mar 11, 2020
c8bfb68
Merge remote-tracking branch 'origin/master' into rmuller/submodules
RomainMuller Mar 11, 2020
eecc965
Fixup mktemp invokation for Linux
RomainMuller Mar 11, 2020
e0343da
specification addendum
RomainMuller Mar 12, 2020
bc8d3ba
Update docs/specifications/2-type-system.md
RomainMuller Mar 12, 2020
a396e82
typo
RomainMuller Mar 12, 2020
8845523
Represent submodules in jsii-reflect
RomainMuller Mar 13, 2020
a96ad4e
de-namespacing
RomainMuller Mar 13, 2020
54ab8da
Merge branch 'master' into rmuller/submodules
RomainMuller Mar 16, 2020
d46f8ac
undo import move in Python CodeGen
RomainMuller Mar 16, 2020
570540f
undo namespacing changes in kernel tests, too.
RomainMuller Mar 16, 2020
2d4aa0f
minor fixes
RomainMuller Mar 16, 2020
eec62fb
Fix bugs in compiler
RomainMuller Mar 16, 2020
318c67e
Fixing stuff in the submodules
RomainMuller Mar 16, 2020
9c7fe09
Fixup test
RomainMuller Mar 16, 2020
c8d7514
Merge remote-tracking branch 'origin/master' into rmuller/submodules
RomainMuller Mar 16, 2020
fd2d41a
fix test expectations
RomainMuller Mar 16, 2020
72877ca
Fix path capitalization (MacOS case-insentive FS made this... tough)
RomainMuller Mar 16, 2020
4f26cce
Merge branch 'master' into rmuller/submodules
RomainMuller Mar 17, 2020
09b567b
Merge branch 'master' into rmuller/submodules
RomainMuller Mar 18, 2020
b42349d
Merge remote-tracking branch 'origin/master' into rmuller/submodules
RomainMuller Mar 19, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions docs/specifications/2-type-system.md
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,86 @@ document.
}
```

## Submodules

> :construction: The *submodules* feature is still under active development and
> the specific behavior around it (in particular with respects to code
> generation) are still subject to change.

### Overview

Typescript allows grouping declarations together in *namespaces*, which are
interpreted by *jsii* as *submodules*. *Submodules* names are the fully
qualified name of the namespace from the package's root (if a package `foo`
defines a namespace `ns1`, which itself contains `ns2`, the submodule for `ns2`
will be named `foo.ns1.ns2`).

*Submodules* may use different [code-generation configuration](#code-generation)
than their parent submodule or package.

> :construction: *Submodule*-level code-generation configuration is not yet
> implemented.

### Restrictions

*Submodules* cannot be involved in dependency cycles. While it is possible to
build such cycles in **JavaScript**, that configuration cannot be reliably
reprensented in certain other programming languages (e.g: **Python**).

> :construction: [`jsii`] does not currently check for circular submodule
> dependencies. Invalid dependency patterns may result in errors at code
> generation by [`jsii-pacmak`], or at runtime.

Since this would result in ambiguity that cannot be consistently resolved, a
given type can only be exported as part of one *submodule*.

[`jsii`]: ../../packages/jsii
[`jsii-pacmak`]: ../../packages/jsii-pacmak

### Declaration

There are two supported ways to introduce *submodules*:
* Using the namespaced export syntax:
```ts
export * as ns from './module';
```
* Using an explicit namespace declaration:
```ts
export namespace ns { /* ... */ }
```

*Submodules* declared using the `export * as ns from './module';` syntax can be
documented using a markdown document located at `./module/README.md`.

> :construction: The `./module/README.md` file support is not yet implemented.

### Code Generation

In languages where this is relevant (e.g: **Python**), *submodules* are rendered
as native *submodules*. In languages where a namespace system exists (**Java**
uses *packages*, **C#** uses *namespaces*, ...), *submodules* are rendered using
that.

## Code Generation

In order to generate code in various programming languages, [`jsii-pacmak`]
needs configuration that provides naming directives (e.g: **Java** package
names, **C#** namespaces, **Python** module names, ...). This configuration is
language-specific and each language implementation specifies and documents its
own configuration schema.

Configuration is sourced in the `package.json` file at the root of the npm
package, under the special `jsii` key. The general schema is described in the
[configuration] document.

> :construction: There is a proposition to allow this configuration to be placed
> in a `.jsiirc.json` file, which would take precedence over what is specified
> in `package.json`. *Submodules* introduced using the
> `export * as ns from './module';` syntax would then be able to define
> *submodule*-local configuration using the `./module/.jsiirc.json` file.

[configuration]: ../configuration.md

## References

The [**TypeScript** Handbook] describes the language's type system and syntax
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using Amazon.JSII.Runtime.Deputy;
using Amazon.JSII.Tests.CalculatorNamespace;
using CompositeOperation = Amazon.JSII.Tests.CalculatorNamespace.composition.CompositeOperation;
using CompositeOperation = Amazon.JSII.Tests.CalculatorNamespace.Composition.CompositeOperation;
using Amazon.JSII.Tests.CalculatorNamespace.LibNamespace;
using Newtonsoft.Json.Linq;
using Xunit;
Expand Down Expand Up @@ -1413,11 +1413,11 @@ public void AbstractMembersAreCorrectlyHandled()
var abstractSuite = new AbstractSuiteImpl();
Assert.Equal("Wrapped<String<Oomf!>>", abstractSuite.WorkItAll("Oomf!"));
}

private sealed class AbstractSuiteImpl : AbstractSuite
{
private string _property = "";

public AbstractSuiteImpl() {}

protected override string SomeMethod(string str)
Expand All @@ -1440,7 +1440,7 @@ public void CollectionOfInterfaces_ListOfStructs()
Assert.IsAssignableFrom<IStructA>(elt);
}
}

[Fact(DisplayName = Prefix + nameof(CollectionOfInterfaces_ListOfInterfaces))]
public void CollectionOfInterfaces_ListOfInterfaces()
{
Expand All @@ -1449,7 +1449,7 @@ public void CollectionOfInterfaces_ListOfInterfaces()
Assert.IsAssignableFrom<IBell>(elt);
}
}

[Fact(DisplayName = Prefix + nameof(CollectionOfInterfaces_MapOfStructs))]
public void CollectionOfInterfaces_MapOfStructs()
{
Expand All @@ -1458,7 +1458,7 @@ public void CollectionOfInterfaces_MapOfStructs()
Assert.IsAssignableFrom<IStructA>(elt);
}
}

[Fact(DisplayName = Prefix + nameof(CollectionOfInterfaces_MapOfInterfaces))]
public void CollectionOfInterfaces_MapOfInterfaces()
{
Expand Down
2 changes: 2 additions & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ export * from './compliance';
export * from './documented';
export * from './erasures';
export * from './stability';

export * as submodule from './submodule';
12 changes: 12 additions & 0 deletions packages/jsii-calc/lib/submodule/child/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export interface Structure {
readonly bool: boolean;
}

export enum Goodness {
/** It's pretty good */
PRETTY_GOOD,
/** It's really good */
REALLY_GOOD,
/** It's amazingly good */
AMAZINGLY_GOOD
}
Comment on lines +5 to +12
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimistic today, aren't we? :-D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 changes: 4 additions & 0 deletions packages/jsii-calc/lib/submodule/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as child from './child';
export * from './my-class';
export * from './nested_submodule';
export * as back_references from './refers-to-parent';
11 changes: 11 additions & 0 deletions packages/jsii-calc/lib/submodule/my-class.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { nested_submodule } from './nested_submodule';
import { Goodness } from './child';
import { AllTypes } from '..';

export class MyClass implements nested_submodule.deeplyNested.INamespaced {
public readonly definedAt = __filename;
public readonly goodness = Goodness.AMAZINGLY_GOOD;
public allTypes?: AllTypes;

public constructor() { }
}
16 changes: 16 additions & 0 deletions packages/jsii-calc/lib/submodule/nested_submodule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Goodness } from './child';

export namespace nested_submodule {
export namespace deeplyNested {
export interface INamespaced {
readonly definedAt: string;
}
}

export abstract class Namespaced implements deeplyNested.INamespaced {
public readonly definedAt = __filename;
public abstract readonly goodness: Goodness;

private constructor() { }
}
}
5 changes: 5 additions & 0 deletions packages/jsii-calc/lib/submodule/refers-to-parent/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { MyClass } from '..';

export interface MyClassReference {
readonly reference: MyClass;
}
Loading