Skip to content

Commit 3fe946d

Browse files
authored
Use symbolToTypeNode for class & interface references (microsoft#24330)
* Use merged symbols when calculating qualification, use symbolToTypeNode * Accept baselines
1 parent 5622bc2 commit 3fe946d

57 files changed

Lines changed: 392 additions & 300 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/compiler/checker.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2672,7 +2672,7 @@ namespace ts {
26722672
// if the symbolFromSymbolTable is not external module (it could be if it was determined as ambient external module and would be in globals table)
26732673
// and if symbolFromSymbolTable or alias resolution matches the symbol,
26742674
// check the symbol can be qualified, it is only then this symbol is accessible
2675-
!some(symbolFromSymbolTable.declarations, hasExternalModuleSymbol) &&
2675+
!some(symbolFromSymbolTable.declarations, hasNonGlobalAugmentationExternalModuleSymbol) &&
26762676
(ignoreQualification || canQualifySymbol(symbolFromSymbolTable, meaning));
26772677
}
26782678

@@ -2704,6 +2704,11 @@ namespace ts {
27042704
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
27052705
}
27062706
}
2707+
if (symbolFromSymbolTable.escapedName === symbol.escapedName && symbolFromSymbolTable.exportSymbol) {
2708+
if (isAccessible(getMergedSymbol(symbolFromSymbolTable.exportSymbol), /*aliasSymbol*/ undefined, ignoreQualification)) {
2709+
return [symbol];
2710+
}
2711+
}
27072712
});
27082713
}
27092714
}
@@ -2712,7 +2717,7 @@ namespace ts {
27122717
let qualify = false;
27132718
forEachSymbolTableInScope(enclosingDeclaration, symbolTable => {
27142719
// If symbol of this name is not available in the symbol table we are ok
2715-
let symbolFromSymbolTable = symbolTable.get(symbol.escapedName);
2720+
let symbolFromSymbolTable = getMergedSymbol(symbolTable.get(symbol.escapedName));
27162721
if (!symbolFromSymbolTable) {
27172722
// Continue to the next symbol table
27182723
return false;
@@ -2792,7 +2797,7 @@ namespace ts {
27922797
return hasAccessibleDeclarations;
27932798
}
27942799
else {
2795-
if (some(symbol.declarations, hasExternalModuleSymbol)) {
2800+
if (some(symbol.declarations, hasNonGlobalAugmentationExternalModuleSymbol)) {
27962801
// Any meaning of a module symbol is always accessible via an `import` type
27972802
return {
27982803
accessibility: SymbolAccessibility.Accessible
@@ -2850,6 +2855,10 @@ namespace ts {
28502855
return isAmbientModule(declaration) || (declaration.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule(<SourceFile>declaration));
28512856
}
28522857

2858+
function hasNonGlobalAugmentationExternalModuleSymbol(declaration: Node) {
2859+
return isModuleWithStringLiteralName(declaration) || (declaration.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule(<SourceFile>declaration));
2860+
}
2861+
28532862
function hasVisibleDeclarations(symbol: Symbol, shouldComputeAliasToMakeVisible: boolean): SymbolVisibilityResult {
28542863
let aliasesToMakeVisible: LateVisibilityPaintedStatement[];
28552864
if (forEach(symbol.declarations, declaration => !getIsDeclarationVisible(declaration))) {
@@ -3164,9 +3173,10 @@ namespace ts {
31643173
!isTypeSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
31653174
return createTypeReferenceNode(getGeneratedNameForNode((type.symbol.declarations[0] as TypeParameterDeclaration).name, GeneratedIdentifierFlags.Optimistic | GeneratedIdentifierFlags.ReservedInNestedScopes), /*typeArguments*/ undefined);
31663175
}
3167-
const name = type.symbol ? symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ false) : createIdentifier("?");
31683176
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
3169-
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
3177+
return type.symbol
3178+
? symbolToTypeNode(type.symbol, context, SymbolFlags.Type)
3179+
: createTypeReferenceNode(createIdentifier("?"), /*typeArguments*/ undefined);
31703180
}
31713181
if (!inTypeAlias && type.aliasSymbol && (context.flags & NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope || isTypeSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration))) {
31723182
const typeArgumentNodes = mapToTypeNodes(type.aliasTypeArguments, context);
@@ -3712,7 +3722,7 @@ namespace ts {
37123722
// If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols.
37133723
endOfChain ||
37143724
// If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.)
3715-
(yieldModuleSymbol || !(!parentSymbol && forEach(symbol.declarations, hasExternalModuleSymbol))) &&
3725+
(yieldModuleSymbol || !(!parentSymbol && forEach(symbol.declarations, hasNonGlobalAugmentationExternalModuleSymbol))) &&
37163726
// If a parent symbol is an anonymous type, don't write it.
37173727
!(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral))) {
37183728

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ module clodule {
2222

2323
// error: duplicate identifier expected
2424
export function fn<T>(x: T, y: T): T {
25-
>fn : Symbol(clodule.fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
25+
>fn : Symbol(fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
2626
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 9, 23))
2727
>x : Symbol(x, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 9, 26))
2828
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 9, 23))

tests/baselines/reference/ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module clodule {
2020

2121
// error: duplicate identifier expected
2222
export function fn<T>(x: T, y: T): T {
23-
>fn : Symbol(clodule.fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
23+
>fn : Symbol(fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
2424
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 9, 23))
2525
>x : Symbol(x, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 9, 26))
2626
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 9, 23))

tests/baselines/reference/ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Point {
1717
>Point : Symbol(Point, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 0, 0), Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 4, 1))
1818

1919
export function Origin() { return null; } //expected duplicate identifier error
20-
>Origin : Symbol(Point.Origin, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 6, 14))
20+
>Origin : Symbol(Origin, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 6, 14))
2121
}
2222

2323

@@ -42,6 +42,6 @@ module A {
4242
>Point : Symbol(Point, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 11, 10), Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 16, 5))
4343

4444
export function Origin() { return ""; }//expected duplicate identifier error
45-
>Origin : Symbol(Point.Origin, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 18, 25))
45+
>Origin : Symbol(Origin, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 18, 25))
4646
}
4747
}

tests/baselines/reference/ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module Point {
1717
>Point : Symbol(Point, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 0, 0), Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 4, 1))
1818

1919
export var Origin = ""; //expected duplicate identifier error
20-
>Origin : Symbol(Point.Origin, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 7, 14))
20+
>Origin : Symbol(Origin, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 7, 14))
2121
}
2222

2323

@@ -42,6 +42,6 @@ module A {
4242
>Point : Symbol(Point, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 11, 10), Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 16, 5))
4343

4444
export var Origin = ""; //expected duplicate identifier error
45-
>Origin : Symbol(Point.Origin, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 19, 18))
45+
>Origin : Symbol(Origin, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 19, 18))
4646
}
4747
}

tests/baselines/reference/TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.symbols

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ module A{
1818

1919
// expected error
2020
export class Point {
21-
>Point : Symbol(A.Point, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 7, 9))
21+
>Point : Symbol(Point, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 7, 9))
2222

2323
origin: number;
24-
>origin : Symbol(A.Point.origin, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 9, 24))
24+
>origin : Symbol(Point.origin, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 9, 24))
2525

2626
angle: number;
27-
>angle : Symbol(A.Point.angle, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 10, 23))
27+
>angle : Symbol(Point.angle, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 10, 23))
2828
}
2929
}
3030

@@ -52,10 +52,10 @@ module X {
5252

5353
// expected error
5454
export class Line {
55-
>Line : Symbol(Z.Line, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 23, 25))
55+
>Line : Symbol(Line, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 23, 25))
5656

5757
name: string;
58-
>name : Symbol(Z.Line.name, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 25, 31))
58+
>name : Symbol(Line.name, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 25, 31))
5959
}
6060
}
6161
}

tests/baselines/reference/augmentedClassWithPrototypePropertyOnModule.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ declare module m {
66
>f : Symbol(f, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 1, 7))
77

88
var prototype; // This should be error since prototype would be static property on class m
9-
>prototype : Symbol(m.prototype, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 2, 7))
9+
>prototype : Symbol(prototype, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 2, 7))
1010
}
1111
declare class m {
1212
>m : Symbol(m, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 0, 0), Decl(augmentedClassWithPrototypePropertyOnModule.ts, 3, 1))

tests/baselines/reference/cloduleWithDuplicateMember1.symbols

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ module C {
1818
>C : Symbol(C, Decl(cloduleWithDuplicateMember1.ts, 0, 0), Decl(cloduleWithDuplicateMember1.ts, 6, 1), Decl(cloduleWithDuplicateMember1.ts, 10, 1))
1919

2020
export var x = 1;
21-
>x : Symbol(C.x, Decl(cloduleWithDuplicateMember1.ts, 9, 14))
21+
>x : Symbol(x, Decl(cloduleWithDuplicateMember1.ts, 9, 14))
2222
}
2323
module C {
2424
>C : Symbol(C, Decl(cloduleWithDuplicateMember1.ts, 0, 0), Decl(cloduleWithDuplicateMember1.ts, 6, 1), Decl(cloduleWithDuplicateMember1.ts, 10, 1))
2525

2626
export function foo() { }
27-
>foo : Symbol(C.foo, Decl(cloduleWithDuplicateMember1.ts, 11, 10))
27+
>foo : Symbol(foo, Decl(cloduleWithDuplicateMember1.ts, 11, 10))
2828

2929
export function x() { }
30-
>x : Symbol(C.x, Decl(cloduleWithDuplicateMember1.ts, 12, 29))
30+
>x : Symbol(x, Decl(cloduleWithDuplicateMember1.ts, 12, 29))
3131
}

tests/baselines/reference/cloduleWithDuplicateMember2.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ module C {
2121
>C : Symbol(C, Decl(cloduleWithDuplicateMember2.ts, 0, 0), Decl(cloduleWithDuplicateMember2.ts, 3, 1), Decl(cloduleWithDuplicateMember2.ts, 7, 1))
2222

2323
export function x() { }
24-
>x : Symbol(C.x, Decl(cloduleWithDuplicateMember2.ts, 8, 10))
24+
>x : Symbol(x, Decl(cloduleWithDuplicateMember2.ts, 8, 10))
2525
}

tests/baselines/reference/constructorOverloads4.symbols

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ declare module M {
33
>M : Symbol(M, Decl(constructorOverloads4.ts, 0, 0))
44

55
export class Function {
6-
>Function : Symbol(M.Function, Decl(constructorOverloads4.ts, 0, 18))
6+
>Function : Symbol(Function, Decl(constructorOverloads4.ts, 0, 18))
77

88
constructor(...args: string[]);
99
>args : Symbol(args, Decl(constructorOverloads4.ts, 2, 20))

0 commit comments

Comments
 (0)