Add a MuIL export node; transform simple re-exports

This change first and foremost adds a MuIL export node (ast.Export);
this is for re-exporting members of other modules, potentially under
a different name.

This works by simply by associating a name with a fully qualified token
name that, when binding, can be re-resolved to the real target.  This
should be sufficient for expressing everything we need from MuJS.

In addition to that, there is an initial cut at basic re-exports; this
is the form where you would say:

    export { some, nifty as cool, stuff } from "module";

In this example, we end up with two Export nodes: one maps "some" to
"some/module", "cool" to "some/nifty", and "stuff" to "some/stuff".

Note that we have not yet implemented two other variants.

First, we will want to support the "export all" variety, e.g.

    export * from "module";

Instead of supporting wildcards in MuIL -- which would result in the
odd attribute that you couldn't know the totality of a module's exports
without also examining its dependencies (possibly recursively) -- we
will require the compiler to map the "*" to a concrete list of Export nodes.

Second, we also want to support named exports from the current module.
That is to say, export clauses without `from "module"` parts.  E.g.

    let some = ...;
    let nifty = ...;
    let stuff = ...;
    export { some, nifty as cool, stuff };

This is less important because you can always export members using the
more familiar `export <decl>` syntax (which is already recognized by MuJS).
But of course, we will want to support it eventually.  It is slightly more
complex, because we will need to use static typing to resolve the exported
members to their types -- e.g., variable, class, interface -- and in fact
this could lead us to re-exporting whole modules in their entirety.

Both of these two will require marapongo/mu#46.  Since so much is piling up
behind this, I will likely begin biting this off soon.  I also believe that
we may want re-export nodes, Export, to carry a specifier that indicates whether
the target is a class, variable, or sub-module.
This commit is contained in:
joeduffy 2017-01-12 15:51:49 -08:00
parent 49d0c2613a
commit 9df12235a6
3 changed files with 64 additions and 12 deletions

View file

@ -33,8 +33,16 @@ export type ModuleMembers = { [token: string /*symbols.ModuleToken*/]: ModuleMem
/* Classes */
// An export definition re-exports a definition from another module, possibly with a different name.
export interface Export extends ModuleMember {
kind: ExportKind;
token: symbols.Token;
}
export const exportKind = "Export";
export type ExportKind = "Export";
// A class can be constructed to create an object, and exports properties, methods, and has a number of attributes.
export interface Class extends Definition {
export interface Class extends ModuleMember {
kind: ClassKind;
extends?: symbols.TypeToken;
implements?: symbols.TypeToken[];

View file

@ -21,12 +21,13 @@ export type NodeKind =
IdentifierKind |
definitions.ModuleKind |
definitions.LocalVariableKind |
definitions.ModulePropertyKind |
definitions.ClassPropertyKind |
definitions.ModuleMethodKind |
definitions.ClassMethodKind |
definitions.ClassKind |
definitions.ExportKind |
definitions.LocalVariableKind |
definitions.ClassPropertyKind |
definitions.ModulePropertyKind |
definitions.ClassMethodKind |
definitions.ModuleMethodKind |
statements.BlockKind |
statements.LocalVariableDeclarationKind |

View file

@ -135,8 +135,11 @@ function isComputed(name: ts.Node | undefined): boolean {
}
// notYetImplemented simply fail-fasts, but does so in a way where we at least get Node source information.
function notYetImplemented(node: ts.Node | undefined): never {
function notYetImplemented(node: ts.Node | undefined, label?: string): never {
let msg: string = "Not Yet Implemented";
if (label) {
msg += `[${label}]`;
}
if (node) {
let s: ts.SourceFile = node.getSourceFile();
let start: ts.LineAndCharacter = s.getLineAndCharacterOfPosition(node.getStart());
@ -313,7 +316,7 @@ export class Transformer {
case ts.SyntaxKind.ExportAssignment:
return [ this.transformExportAssignment(<ts.ExportAssignment>node) ];
case ts.SyntaxKind.ExportDeclaration:
return [ this.transformExportDeclaration(<ts.ExportDeclaration>node) ];
return this.transformExportDeclaration(<ts.ExportDeclaration>node);
case ts.SyntaxKind.ImportDeclaration:
return [ this.transformImportDeclaration(<ts.ImportDeclaration>node) ];
@ -352,8 +355,46 @@ export class Transformer {
return notYetImplemented(node);
}
private transformExportDeclaration(node: ts.ExportDeclaration): ast.Definition {
return notYetImplemented(node);
private transformExportDeclaration(node: ts.ExportDeclaration): ast.ModuleMember[] {
// In the case of a module specifier, we are re-exporting elements from another module.
if (node.moduleSpecifier) {
return this.transformReExportDeclaration(node);
}
// Otherwise, we are exporting already-imported names from the current module.
// TODO(joe): support this, by enumerating all exports, and flipping any privates to publics. As we proceed, we
// will need to keep an eye out for exporting whole sub-modules.
return notYetImplemented(node, "manual exports");
}
private transformReExportDeclaration(node: ts.ExportDeclaration): ast.ModuleMember[] {
contract.assert(!!node.moduleSpecifier);
contract.assert(node.moduleSpecifier!.kind === ts.SyntaxKind.StringLiteral);
let spec: ts.StringLiteral = <ts.StringLiteral>node.moduleSpecifier!;
// The module specifier will be a string literal; fetch that so we can resolve to a symbol token.
let source: symbols.ModuleToken = this.transformStringLiteral(spec).value;
if (node.exportClause) {
// For every export clause, we will issue a top-level MuIL export AST node.
let exports: ast.Export[] = [];
for (let exportClause of node.exportClause.elements) {
contract.assert(!exportClause.propertyName);
let name: ast.Identifier = this.transformIdentifier(exportClause.name);
// TODO[marapongo/mu#46]: produce real module tokens that will be recognizable.
let token: symbols.Token = `${source}::${name.ident}`;
exports.push(<ast.Export>{
kind: ast.exportKind,
name: name,
token: token,
});
}
return exports;
}
else {
// TODO[marapongo/mu#46]: we need to enumerate all known exports, but to do that, we'll need to have
// semantic understanding of the imported module with a given name.
return notYetImplemented(node, "export *");
}
}
private transformExportSpecifier(node: ts.ExportSpecifier): ast.Definition {
@ -1224,9 +1265,11 @@ export class Transformer {
case ts.SyntaxKind.ExclamationEqualsEqualsToken:
log.out(3).info(
`ECMAScript operator '${ts.SyntaxKind[node.operatorToken.kind]}' not supported; ` +
`until marapongo/mu#50 is implemented, be careful about subtle behavioral differences`
`until marapongo/mu#50 is implemented, be careful about subtle behavioral differences`,
);
break;
default:
break;
}
}
@ -1290,7 +1333,7 @@ export class Transformer {
if (log.v(3)) {
log.out(3).info(
`ECMAScript operator 'delete' not supported; ` +
`until marapongo/mu#50 is implemented, be careful about subtle behavioral differences`
`until marapongo/mu#50 is implemented, be careful about subtle behavioral differences`,
);
}
// TODO[marapongo/mu#50]: we need to decide how to map `delete` into a runtime MuIL operator. It's possible