Skip to content

guard against visiting the same symbol table multiple times #12818

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

Merged
merged 1 commit into from
Dec 12, 2016
Merged
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
62 changes: 38 additions & 24 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1742,7 +1742,19 @@ namespace ts {
}

function getAccessibleSymbolChain(symbol: Symbol, enclosingDeclaration: Node, meaning: SymbolFlags, useOnlyExternalAliasing: boolean): Symbol[] {
function getAccessibleSymbolChainFromSymbolTable(symbols: SymbolTable): Symbol[] {
function getAccessibleSymbolChainFromSymbolTable(symbols: SymbolTable) {
return getAccessibleSymbolChainFromSymbolTableWorker(symbols, []);
}

function getAccessibleSymbolChainFromSymbolTableWorker(symbols: SymbolTable, visitedSymbolTables: SymbolTable[]): Symbol[] {
if (contains(visitedSymbolTables, symbols)) {
return undefined;
}
visitedSymbolTables.push(symbols);
const result = trySymbolTable(symbols);
visitedSymbolTables.pop();
return result;

function canQualifySymbol(symbolFromSymbolTable: Symbol, meaning: SymbolFlags) {
// If the symbol is equivalent and doesn't need further qualification, this symbol is accessible
if (!needsQualification(symbolFromSymbolTable, enclosingDeclaration, meaning)) {
Expand All @@ -1764,34 +1776,36 @@ namespace ts {
}
}

// If symbol is directly available by its name in the symbol table
if (isAccessible(symbols[symbol.name])) {
return [symbol];
}
function trySymbolTable(symbols: SymbolTable) {
// If symbol is directly available by its name in the symbol table
if (isAccessible(symbols[symbol.name])) {
return [symbol];
}

// Check if symbol is any of the alias
return forEachProperty(symbols, symbolFromSymbolTable => {
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
&& symbolFromSymbolTable.name !== "export="
&& !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier)) {
if (!useOnlyExternalAliasing || // We can use any type of alias to get the name
// Is this external alias, then use it to name
ts.forEach(symbolFromSymbolTable.declarations, isExternalModuleImportEqualsDeclaration)) {
// Check if symbol is any of the alias
return forEachProperty(symbols, symbolFromSymbolTable => {
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
&& symbolFromSymbolTable.name !== "export="
&& !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier)) {
if (!useOnlyExternalAliasing || // We can use any type of alias to get the name
// Is this external alias, then use it to name
ts.forEach(symbolFromSymbolTable.declarations, isExternalModuleImportEqualsDeclaration)) {

const resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable);
if (isAccessible(symbolFromSymbolTable, resolveAlias(symbolFromSymbolTable))) {
return [symbolFromSymbolTable];
}
const resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable);
if (isAccessible(symbolFromSymbolTable, resolveAlias(symbolFromSymbolTable))) {
return [symbolFromSymbolTable];
}

// Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain
// but only if the symbolFromSymbolTable can be qualified
const accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTable(resolvedImportedSymbol.exports) : undefined;
if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) {
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
// Look in the exported members, if we can find accessibleSymbolChain, symbol is accessible using this chain
// but only if the symbolFromSymbolTable can be qualified
const accessibleSymbolsFromExports = resolvedImportedSymbol.exports ? getAccessibleSymbolChainFromSymbolTableWorker(resolvedImportedSymbol.exports, visitedSymbolTables) : undefined;
if (accessibleSymbolsFromExports && canQualifySymbol(symbolFromSymbolTable, getQualifiedLeftMeaning(meaning))) {
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
}
}
}
}
});
});
}
}

if (symbol) {
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/circularReferenceInImport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [tests/cases/compiler/circularReferenceInImport.ts] ////

//// [db.d.ts]

declare namespace Db {
export import Types = Db;
}

export = Db;

//// [app.ts]
import * as Db from "./db"

export function foo() {
return new Object()
}

//// [app.js]
"use strict";
function foo() {
return new Object();
}
exports.foo = foo;


//// [app.d.ts]
export declare function foo(): Object;
23 changes: 23 additions & 0 deletions tests/baselines/reference/circularReferenceInImport.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/db.d.ts ===

declare namespace Db {
>Db : Symbol(Types, Decl(db.d.ts, 0, 0))

export import Types = Db;
>Types : Symbol(Types, Decl(db.d.ts, 1, 22))
>Db : Symbol(Types, Decl(db.d.ts, 0, 0))
}

export = Db;
>Db : Symbol(Db, Decl(db.d.ts, 0, 0))

=== tests/cases/compiler/app.ts ===
import * as Db from "./db"
>Db : Symbol(Db, Decl(app.ts, 0, 6))

export function foo() {
>foo : Symbol(foo, Decl(app.ts, 0, 26))

return new Object()
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
}
24 changes: 24 additions & 0 deletions tests/baselines/reference/circularReferenceInImport.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/db.d.ts ===

declare namespace Db {
>Db : typeof Types

export import Types = Db;
>Types : typeof Types
>Db : typeof Types
}

export = Db;
>Db : typeof Db

=== tests/cases/compiler/app.ts ===
import * as Db from "./db"
>Db : typeof Db

export function foo() {
>foo : () => Object

return new Object()
>new Object() : Object
>Object : ObjectConstructor
}
15 changes: 15 additions & 0 deletions tests/cases/compiler/circularReferenceInImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// @declaration: true

// @filename: db.d.ts
declare namespace Db {
export import Types = Db;
}

export = Db;

// @filename: app.ts
import * as Db from "./db"

export function foo() {
return new Object()
}