Skip to content

Commit 37d6ab3

Browse files
author
Andy
authored
Merge pull request #10507 from Microsoft/walk_symbol
Always output something at the end of walkSymbol
2 parents 3cca17e + e0fd0e8 commit 37d6ab3

25 files changed

+79
-71
lines changed

src/compiler/checker.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2068,45 +2068,42 @@ namespace ts {
20682068
parentSymbol = symbol;
20692069
}
20702070

2071-
// const the writer know we just wrote out a symbol. The declaration emitter writer uses
2071+
// Let the writer know we just wrote out a symbol. The declaration emitter writer uses
20722072
// this to determine if an import it has previously seen (and not written out) needs
20732073
// to be written to the file once the walk of the tree is complete.
20742074
//
20752075
// NOTE(cyrusn): This approach feels somewhat unfortunate. A simple pass over the tree
20762076
// up front (for example, during checking) could determine if we need to emit the imports
20772077
// and we could then access that data during declaration emit.
20782078
writer.trackSymbol(symbol, enclosingDeclaration, meaning);
2079-
function walkSymbol(symbol: Symbol, meaning: SymbolFlags): void {
2080-
if (symbol) {
2081-
const accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & SymbolFormatFlags.UseOnlyExternalAliasing));
2079+
/** @param endOfChain Set to false for recursive calls; non-recursive calls should always output something. */
2080+
function walkSymbol(symbol: Symbol, meaning: SymbolFlags, endOfChain: boolean): void {
2081+
const accessibleSymbolChain = getAccessibleSymbolChain(symbol, enclosingDeclaration, meaning, !!(flags & SymbolFormatFlags.UseOnlyExternalAliasing));
20822082

2083-
if (!accessibleSymbolChain ||
2084-
needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) {
2083+
if (!accessibleSymbolChain ||
2084+
needsQualification(accessibleSymbolChain[0], enclosingDeclaration, accessibleSymbolChain.length === 1 ? meaning : getQualifiedLeftMeaning(meaning))) {
20852085

2086-
// Go up and add our parent.
2087-
walkSymbol(
2088-
getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol),
2089-
getQualifiedLeftMeaning(meaning));
2086+
// Go up and add our parent.
2087+
const parent = getParentOfSymbol(accessibleSymbolChain ? accessibleSymbolChain[0] : symbol);
2088+
if (parent) {
2089+
walkSymbol(parent, getQualifiedLeftMeaning(meaning), /*endOfChain*/ false);
20902090
}
2091+
}
20912092

2092-
if (accessibleSymbolChain) {
2093-
for (const accessibleSymbol of accessibleSymbolChain) {
2094-
appendParentTypeArgumentsAndSymbolName(accessibleSymbol);
2095-
}
2093+
if (accessibleSymbolChain) {
2094+
for (const accessibleSymbol of accessibleSymbolChain) {
2095+
appendParentTypeArgumentsAndSymbolName(accessibleSymbol);
20962096
}
2097-
else {
2098-
// If we didn't find accessible symbol chain for this symbol, break if this is external module
2099-
if (!parentSymbol && ts.forEach(symbol.declarations, hasExternalModuleSymbol)) {
2100-
return;
2101-
}
2102-
2103-
// if this is anonymous type break
2104-
if (symbol.flags & SymbolFlags.TypeLiteral || symbol.flags & SymbolFlags.ObjectLiteral) {
2105-
return;
2106-
}
2097+
}
2098+
else if (
2099+
// If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols.
2100+
endOfChain ||
2101+
// If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.)
2102+
!(!parentSymbol && ts.forEach(symbol.declarations, hasExternalModuleSymbol)) &&
2103+
// If a parent symbol is an anonymous type, don't write it.
2104+
!(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral))) {
21072105

2108-
appendParentTypeArgumentsAndSymbolName(symbol);
2109-
}
2106+
appendParentTypeArgumentsAndSymbolName(symbol);
21102107
}
21112108
}
21122109

@@ -2116,11 +2113,11 @@ namespace ts {
21162113
const isTypeParameter = symbol.flags & SymbolFlags.TypeParameter;
21172114
const typeFormatFlag = TypeFormatFlags.UseFullyQualifiedType & typeFlags;
21182115
if (!isTypeParameter && (enclosingDeclaration || typeFormatFlag)) {
2119-
walkSymbol(symbol, meaning);
2120-
return;
2116+
walkSymbol(symbol, meaning, /*endOfChain*/ true);
2117+
}
2118+
else {
2119+
appendParentTypeArgumentsAndSymbolName(symbol);
21212120
}
2122-
2123-
return appendParentTypeArgumentsAndSymbolName(symbol);
21242121
}
21252122

21262123
function buildTypeDisplay(type: Type, writer: SymbolWriter, enclosingDeclaration?: Node, globalFlags?: TypeFormatFlags, symbolStack?: Symbol[]) {

tests/baselines/reference/augmentExportEquals3.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/file1.ts ===
22

33
function foo() {}
4-
>foo : Symbol(, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 17), Decl(file2.ts, 1, 8))
4+
>foo : Symbol(foo, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 17), Decl(file2.ts, 1, 8))
55

66
namespace foo {
7-
>foo : Symbol(, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 17), Decl(file2.ts, 1, 8))
7+
>foo : Symbol(foo, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 17), Decl(file2.ts, 1, 8))
88

99
export var v = 1;
1010
>v : Symbol(v, Decl(file1.ts, 3, 14))

tests/baselines/reference/augmentExportEquals3.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/file1.ts ===
22

33
function foo() {}
4-
>foo : typeof
4+
>foo : typeof foo
55

66
namespace foo {
7-
>foo : typeof
7+
>foo : typeof foo
88

99
export var v = 1;
1010
>v : number

tests/baselines/reference/augmentExportEquals3_1.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/file1.d.ts ===
22
declare module "file1" {
33
function foo(): void;
4-
>foo : Symbol(, Decl(file1.d.ts, 0, 24), Decl(file1.d.ts, 1, 25), Decl(file2.ts, 2, 8))
4+
>foo : Symbol(foo, Decl(file1.d.ts, 0, 24), Decl(file1.d.ts, 1, 25), Decl(file2.ts, 2, 8))
55

66
namespace foo {
7-
>foo : Symbol(, Decl(file1.d.ts, 0, 24), Decl(file1.d.ts, 1, 25), Decl(file2.ts, 2, 8))
7+
>foo : Symbol(foo, Decl(file1.d.ts, 0, 24), Decl(file1.d.ts, 1, 25), Decl(file2.ts, 2, 8))
88

99
export var v: number;
1010
>v : Symbol(v, Decl(file1.d.ts, 3, 18))

tests/baselines/reference/augmentExportEquals3_1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/file1.d.ts ===
22
declare module "file1" {
33
function foo(): void;
4-
>foo : typeof
4+
>foo : typeof foo
55

66
namespace foo {
7-
>foo : typeof
7+
>foo : typeof foo
88

99
export var v: number;
1010
>v : number

tests/baselines/reference/augmentExportEquals4.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/file1.ts ===
22

33
class foo {}
4-
>foo : Symbol(, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 12), Decl(file2.ts, 1, 8))
4+
>foo : Symbol(foo, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 12), Decl(file2.ts, 1, 8))
55

66
namespace foo {
7-
>foo : Symbol(, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 12), Decl(file2.ts, 1, 8))
7+
>foo : Symbol(foo, Decl(file1.ts, 0, 0), Decl(file1.ts, 1, 12), Decl(file2.ts, 1, 8))
88

99
export var v = 1;
1010
>v : Symbol(v, Decl(file1.ts, 3, 14))

tests/baselines/reference/augmentExportEquals4.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== tests/cases/compiler/file1.ts ===
22

33
class foo {}
4-
>foo :
4+
>foo : foo
55

66
namespace foo {
7-
>foo : typeof
7+
>foo : typeof foo
88

99
export var v = 1;
1010
>v : number

tests/baselines/reference/augmentExportEquals4_1.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
declare module "file1" {
44
class foo {}
5-
>foo : Symbol(, Decl(file1.d.ts, 1, 24), Decl(file1.d.ts, 2, 16), Decl(file2.ts, 2, 8))
5+
>foo : Symbol(foo, Decl(file1.d.ts, 1, 24), Decl(file1.d.ts, 2, 16), Decl(file2.ts, 2, 8))
66

77
namespace foo {
8-
>foo : Symbol(, Decl(file1.d.ts, 1, 24), Decl(file1.d.ts, 2, 16), Decl(file2.ts, 2, 8))
8+
>foo : Symbol(foo, Decl(file1.d.ts, 1, 24), Decl(file1.d.ts, 2, 16), Decl(file2.ts, 2, 8))
99

1010
export var v: number;
1111
>v : Symbol(v, Decl(file1.d.ts, 4, 18))

tests/baselines/reference/augmentExportEquals4_1.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
declare module "file1" {
44
class foo {}
5-
>foo :
5+
>foo : foo
66

77
namespace foo {
8-
>foo : typeof
8+
>foo : typeof foo
99

1010
export var v: number;
1111
>v : number

tests/baselines/reference/augmentExportEquals5.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ declare module Express {
1616

1717
declare module "express" {
1818
function e(): e.Express;
19-
>e : Symbol(, Decl(express.d.ts, 8, 26), Decl(express.d.ts, 9, 28), Decl(augmentation.ts, 1, 29))
19+
>e : Symbol(e, Decl(express.d.ts, 8, 26), Decl(express.d.ts, 9, 28), Decl(augmentation.ts, 1, 29))
2020
>e : Symbol(e, Decl(express.d.ts, 8, 26), Decl(express.d.ts, 9, 28))
2121
>Express : Symbol(Express, Decl(express.d.ts, 54, 9))
2222

2323
namespace e {
24-
>e : Symbol(, Decl(express.d.ts, 8, 26), Decl(express.d.ts, 9, 28), Decl(augmentation.ts, 1, 29))
24+
>e : Symbol(e, Decl(express.d.ts, 8, 26), Decl(express.d.ts, 9, 28), Decl(augmentation.ts, 1, 29))
2525

2626
interface IRoute {
2727
>IRoute : Symbol(IRoute, Decl(express.d.ts, 10, 17))

0 commit comments

Comments
 (0)