Skip to content

Support export * #249

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ under the licensing terms detailed in LICENSE:
* Igor Sbitnev <[email protected]>
* Norton Wang <[email protected]>
* Alan Pierce <[email protected]>
* Andy Hanson <[email protected]>

Portions of this software are derived from third-party works licensed under
the following terms:
Expand Down
21 changes: 18 additions & 3 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1972,9 +1972,24 @@ export function mangleInternalName(declaration: DeclarationStatement, asGlobal:
return mangleInternalName(<DeclarationStatement>parent, asGlobal) +
STATIC_DELIMITER + name;
}
return asGlobal
? name
: declaration.range.source.internalPath + PATH_DELIMITER + name;
return asGlobal ? name : getSourceLevelName(declaration.range.source, name);
}

export function getSourceLevelName({ internalPath }: Source, simpleName: string): string {
return getSourceLevelNameFromInternalPath(internalPath, simpleName);
}

export function getSourceLevelNameFromInternalPath(internalPath: string, simpleName: string): string {
return stripIndex(internalPath) + PATH_DELIMITER + simpleName;
}

// "foo/index" -> "foo"
// "foo/bar" -> itself
export function stripIndex(internalPath: string): string {
Copy link
Member

Choose a reason for hiding this comment

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

Before, this worked as follows: On import "some/file", the compiler first requested some/file.ts from the frontend (here: asc), and if that didn't exist, some/file/index.ts. Both had a distinct internal name (either some/file or some/file/index), and their declarations were not mixed in any way. From a source file, one could both import some/file and some/file/index, if both would happen to exist. This matches what node.js is doing, and I wonder why a change like stripping /index is necessary, effectively removing that functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated this branch so we can support foo.ts and foo/index.ts simultaneously.

There actually was a bug due to being too permissive and trying to resolve imports from both foo.ts and foo/index.ts:

foo.ts: export const foo1 = 0;
foo/index.ts: export const foo2 = 0;
user.ts:

import {} from "./foo"; //ensure file loaded
import { foo1 } from "./foo/index"; // Shouldn't compile

export function test(): i32 {
  return foo1;
}

Shouldn't compile, but did. Fixed in this branch. (Would be nice if we could have negative tests to verify that this isn't allowed, is there an issue open for that?)

const indexPart = PATH_DELIMITER + "index";
return internalPath.endsWith(indexPart)
? internalPath.substring(0, internalPath.length - indexPart.length)
: internalPath;
}

/** Mangles an external to an internal path. */
Expand Down
7 changes: 3 additions & 4 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ import {

nodeIsConstantValue,
isLastStatement,
findDecorator
findDecorator,
getSourceLevelName
} from "./ast";

import {
Expand Down Expand Up @@ -1266,9 +1267,7 @@ export class Compiler extends DiagnosticEmitter {
if (!members) return; // filespace
for (let i = 0, k = members.length; i < k; ++i) {
let member = members[i];
let element = fileLevelExports.get(
statement.range.source.internalPath + PATH_DELIMITER + member.externalName.text
);
let element = fileLevelExports.get(getSourceLevelName(statement.range.source, member.externalName.text));
if (!element) continue; // reported in Program#initialize
switch (element.kind) {
case ElementKind.CLASS_PROTOTYPE: {
Expand Down
2 changes: 2 additions & 0 deletions src/diagnosticMessages.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export enum DiagnosticCode {
A_class_may_only_extend_another_class = 1311,
A_parameter_property_cannot_be_declared_using_a_rest_parameter = 1317,
Duplicate_identifier_0 = 2300,
Identifier_0_is_re_exported_from_modules_1_and_2 = 2301,
Cannot_find_name_0 = 2304,
Module_0_has_no_exported_member_1 = 2305,
Generic_type_0_requires_1_type_argument_s = 2314,
Expand Down Expand Up @@ -202,6 +203,7 @@ export function diagnosticCodeToString(code: DiagnosticCode): string {
case 1311: return "A class may only extend another class.";
case 1317: return "A parameter property cannot be declared using a rest parameter.";
case 2300: return "Duplicate identifier '{0}'.";
case 2301: return "Identifier '{0}' is re-exported from modules '{1}' and '{2}'.";
case 2304: return "Cannot find name '{0}'.";
case 2305: return "Module '{0}' has no exported member '{1}'.";
case 2314: return "Generic type '{0}' requires {1} type argument(s).";
Expand Down
1 change: 1 addition & 0 deletions src/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"A parameter property cannot be declared using a rest parameter.": 1317,

"Duplicate identifier '{0}'.": 2300,
"Identifier '{0}' is re-exported from modules '{1}' and '{2}'.": 2301,
Copy link
Member

Choose a reason for hiding this comment

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

The convention for new errors that do not exist in TS I used so far is to use a 3-digit code instead (TS has 4 digit codes only), in this case that'd be the next 2XX one.

For reference, the original TS error here is

    "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor.": {
        "category": "Error",
        "code": 2301
},

Copy link
Contributor Author

@andy-hanson andy-hanson Sep 9, 2018

Choose a reason for hiding this comment

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

Looks like TS already has a diagnostic for this situation, so I'll use that instead of creating an AS-specific one.

"Cannot find name '{0}'.": 2304,
"Module '{0}' has no exported member '{1}'.": 2305,
"Generic type '{0}' requires {1} type argument(s).": 2314,
Expand Down
Loading