Skip to content

Add an InternalName type for clarity #246

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 20 additions & 10 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ export class Source extends Node {
/** Normalized path. */
normalizedPath: string;
/** Path used internally. */
internalPath: string;
internalPath: InternalName;
/** Simple path (last part without extension). */
simplePath: string;
/** Contained statements. */
Expand Down Expand Up @@ -1562,19 +1562,19 @@ export abstract class DeclarationStatement extends Statement {
/** Array of decorators. */
decorators: DecoratorNode[] | null = null;

protected cachedProgramLevelInternalName: string | null = null;
protected cachedFileLevelInternalName: string | null = null;
protected cachedProgramLevelInternalName: InternalName | null = null;
protected cachedFileLevelInternalName: InternalName | null = null;

/** Gets the mangled program-level internal name of this declaration. */
get programLevelInternalName(): string {
get programLevelInternalName(): InternalName {
if (!this.cachedProgramLevelInternalName) {
this.cachedProgramLevelInternalName = mangleInternalName(this, true);
}
return this.cachedProgramLevelInternalName;
}

/** Gets the mangled file-level internal name of this declaration. */
get fileLevelInternalName(): string {
get fileLevelInternalName(): InternalName {
if (!this.cachedFileLevelInternalName) {
this.cachedFileLevelInternalName = mangleInternalName(this, false);
}
Expand Down Expand Up @@ -1736,7 +1736,7 @@ export class ExportStatement extends Statement {
/** Normalized path, if `path` is set. */
normalizedPath: string | null;
/** Mangled internal path being referenced, if `path` is set. */
internalPath: string | null;
internalPath: InternalName | null;
}

/** Represents an expression that is used as a statement. */
Expand Down Expand Up @@ -1822,7 +1822,7 @@ export class ImportStatement extends Statement {
/** Normalized path. */
normalizedPath: string;
/** Mangled internal path being referenced. */
internalPath: string;
internalPath: InternalName;
}

/** Represents an `interfarce` declaration. */
Expand Down Expand Up @@ -1947,8 +1947,18 @@ export function findDecorator(kind: DecoratorKind, decorators: DecoratorNode[] |
return null;
}

export type InternalName = string;

export function getInternalName(sourceInternalPath: string, name: string): InternalName {
return sourceInternalPath + PATH_DELIMITER + name;
}

export function getInternalNameFromSource(source: Source, name: string): InternalName {
return getInternalName(source.internalPath, name);
}

/** Mangles a declaration's name to an internal name. */
export function mangleInternalName(declaration: DeclarationStatement, asGlobal: bool = false): string {
export function mangleInternalName(declaration: DeclarationStatement, asGlobal: bool = false): InternalName {
var name = declaration.name.text;
var parent = declaration.parent;
if (!parent) return name;
Expand All @@ -1974,11 +1984,11 @@ export function mangleInternalName(declaration: DeclarationStatement, asGlobal:
}
return asGlobal
? name
: declaration.range.source.internalPath + PATH_DELIMITER + name;
: getInternalNameFromSource(declaration.range.source, name);
}

/** Mangles an external to an internal path. */
export function mangleInternalPath(path: string): string {
export function mangleInternalPath(path: string): InternalName {
if (path.endsWith(".ts")) path = path.substring(0, path.length - 3);
return path;
}
Expand Down
8 changes: 3 additions & 5 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {

import {
CommonFlags,
PATH_DELIMITER,
INNER_DELIMITER,
INSTANCE_DELIMITER,
STATIC_DELIMITER,
Expand Down Expand Up @@ -144,7 +143,8 @@ import {

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

import {
Expand Down Expand Up @@ -1266,9 +1266,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(getInternalNameFromSource(statement.range.source, member.externalName.text));
if (!element) continue; // reported in Program#initialize
switch (element.kind) {
case ElementKind.CLASS_PROTOTYPE: {
Expand Down
Loading