Skip to content

Use symbolToTypeNode for class & interface references (#24330) #24334

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
May 22, 2018
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
22 changes: 16 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2672,7 +2672,7 @@ namespace ts {
// if the symbolFromSymbolTable is not external module (it could be if it was determined as ambient external module and would be in globals table)
// and if symbolFromSymbolTable or alias resolution matches the symbol,
// check the symbol can be qualified, it is only then this symbol is accessible
!some(symbolFromSymbolTable.declarations, hasExternalModuleSymbol) &&
!some(symbolFromSymbolTable.declarations, hasNonGlobalAugmentationExternalModuleSymbol) &&
(ignoreQualification || canQualifySymbol(symbolFromSymbolTable, meaning));
}

Expand Down Expand Up @@ -2704,6 +2704,11 @@ namespace ts {
return [symbolFromSymbolTable].concat(accessibleSymbolsFromExports);
}
}
if (symbolFromSymbolTable.escapedName === symbol.escapedName && symbolFromSymbolTable.exportSymbol) {
if (isAccessible(getMergedSymbol(symbolFromSymbolTable.exportSymbol), /*aliasSymbol*/ undefined, ignoreQualification)) {
return [symbol];
}
}
});
}
}
Expand All @@ -2712,7 +2717,7 @@ namespace ts {
let qualify = false;
forEachSymbolTableInScope(enclosingDeclaration, symbolTable => {
// If symbol of this name is not available in the symbol table we are ok
let symbolFromSymbolTable = symbolTable.get(symbol.escapedName);
let symbolFromSymbolTable = getMergedSymbol(symbolTable.get(symbol.escapedName));
if (!symbolFromSymbolTable) {
// Continue to the next symbol table
return false;
Expand Down Expand Up @@ -2792,7 +2797,7 @@ namespace ts {
return hasAccessibleDeclarations;
}
else {
if (some(symbol.declarations, hasExternalModuleSymbol)) {
if (some(symbol.declarations, hasNonGlobalAugmentationExternalModuleSymbol)) {
// Any meaning of a module symbol is always accessible via an `import` type
return {
accessibility: SymbolAccessibility.Accessible
Expand Down Expand Up @@ -2850,6 +2855,10 @@ namespace ts {
return isAmbientModule(declaration) || (declaration.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule(<SourceFile>declaration));
}

function hasNonGlobalAugmentationExternalModuleSymbol(declaration: Node) {
return isModuleWithStringLiteralName(declaration) || (declaration.kind === SyntaxKind.SourceFile && isExternalOrCommonJsModule(<SourceFile>declaration));
}

function hasVisibleDeclarations(symbol: Symbol, shouldComputeAliasToMakeVisible: boolean): SymbolVisibilityResult {
let aliasesToMakeVisible: LateVisibilityPaintedStatement[];
if (forEach(symbol.declarations, declaration => !getIsDeclarationVisible(declaration))) {
Expand Down Expand Up @@ -3164,9 +3173,10 @@ namespace ts {
!isTypeSymbolAccessible(type.symbol, context.enclosingDeclaration)) {
return createTypeReferenceNode(getGeneratedNameForNode((type.symbol.declarations[0] as TypeParameterDeclaration).name, GeneratedIdentifierFlags.Optimistic | GeneratedIdentifierFlags.ReservedInNestedScopes), /*typeArguments*/ undefined);
}
const name = type.symbol ? symbolToName(type.symbol, context, SymbolFlags.Type, /*expectsIdentifier*/ false) : createIdentifier("?");
// Ignore constraint/default when creating a usage (as opposed to declaration) of a type parameter.
return createTypeReferenceNode(name, /*typeArguments*/ undefined);
return type.symbol
? symbolToTypeNode(type.symbol, context, SymbolFlags.Type)
: createTypeReferenceNode(createIdentifier("?"), /*typeArguments*/ undefined);
}
if (!inTypeAlias && type.aliasSymbol && (context.flags & NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope || isTypeSymbolAccessible(type.aliasSymbol, context.enclosingDeclaration))) {
const typeArgumentNodes = mapToTypeNodes(type.aliasTypeArguments, context);
Expand Down Expand Up @@ -3712,7 +3722,7 @@ namespace ts {
// If this is the last part of outputting the symbol, always output. The cases apply only to parent symbols.
endOfChain ||
// If a parent symbol is an external module, don't write it. (We prefer just `x` vs `"foo/bar".x`.)
(yieldModuleSymbol || !(!parentSymbol && forEach(symbol.declarations, hasExternalModuleSymbol))) &&
(yieldModuleSymbol || !(!parentSymbol && forEach(symbol.declarations, hasNonGlobalAugmentationExternalModuleSymbol))) &&
// If a parent symbol is an anonymous type, don't write it.
!(symbol.flags & (SymbolFlags.TypeLiteral | SymbolFlags.ObjectLiteral))) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module clodule {

// error: duplicate identifier expected
export function fn<T>(x: T, y: T): T {
>fn : Symbol(clodule.fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
>fn : Symbol(fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 9, 23))
>x : Symbol(x, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 9, 26))
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndGenericClassStaticFunctionOfTheSameName.ts, 9, 23))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module clodule {

// error: duplicate identifier expected
export function fn<T>(x: T, y: T): T {
>fn : Symbol(clodule.fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
>fn : Symbol(fn, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 7, 16))
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 9, 23))
>x : Symbol(x, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 9, 26))
>T : Symbol(T, Decl(ClassAndModuleThatMergeWithModulesExportedGenericFunctionAndNonGenericClassStaticFunctionOfTheSameName.ts, 9, 23))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Point {
>Point : Symbol(Point, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 0, 0), Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 4, 1))

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


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

export function Origin() { return ""; }//expected duplicate identifier error
>Origin : Symbol(Point.Origin, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 18, 25))
>Origin : Symbol(Origin, Decl(ClassAndModuleThatMergeWithStaticFunctionAndExportedFunctionThatShareAName.ts, 18, 25))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Point {
>Point : Symbol(Point, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 0, 0), Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 4, 1))

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


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

export var Origin = ""; //expected duplicate identifier error
>Origin : Symbol(Point.Origin, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 19, 18))
>Origin : Symbol(Origin, Decl(ClassAndModuleThatMergeWithStaticVariableAndExportedVarThatShareAName.ts, 19, 18))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ module A{

// expected error
export class Point {
>Point : Symbol(A.Point, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 7, 9))
>Point : Symbol(Point, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 7, 9))

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

angle: number;
>angle : Symbol(A.Point.angle, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 10, 23))
>angle : Symbol(Point.angle, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 10, 23))
}
}

Expand Down Expand Up @@ -52,10 +52,10 @@ module X {

// expected error
export class Line {
>Line : Symbol(Z.Line, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 23, 25))
>Line : Symbol(Line, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 23, 25))

name: string;
>name : Symbol(Z.Line.name, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 25, 31))
>name : Symbol(Line.name, Decl(TwoInternalModulesThatMergeEachWithExportedClassesOfTheSameName.ts, 25, 31))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare module m {
>f : Symbol(f, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 1, 7))

var prototype; // This should be error since prototype would be static property on class m
>prototype : Symbol(m.prototype, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 2, 7))
>prototype : Symbol(prototype, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 2, 7))
}
declare class m {
>m : Symbol(m, Decl(augmentedClassWithPrototypePropertyOnModule.ts, 0, 0), Decl(augmentedClassWithPrototypePropertyOnModule.ts, 3, 1))
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/cloduleWithDuplicateMember1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ module C {
>C : Symbol(C, Decl(cloduleWithDuplicateMember1.ts, 0, 0), Decl(cloduleWithDuplicateMember1.ts, 6, 1), Decl(cloduleWithDuplicateMember1.ts, 10, 1))

export var x = 1;
>x : Symbol(C.x, Decl(cloduleWithDuplicateMember1.ts, 9, 14))
>x : Symbol(x, Decl(cloduleWithDuplicateMember1.ts, 9, 14))
}
module C {
>C : Symbol(C, Decl(cloduleWithDuplicateMember1.ts, 0, 0), Decl(cloduleWithDuplicateMember1.ts, 6, 1), Decl(cloduleWithDuplicateMember1.ts, 10, 1))

export function foo() { }
>foo : Symbol(C.foo, Decl(cloduleWithDuplicateMember1.ts, 11, 10))
>foo : Symbol(foo, Decl(cloduleWithDuplicateMember1.ts, 11, 10))

export function x() { }
>x : Symbol(C.x, Decl(cloduleWithDuplicateMember1.ts, 12, 29))
>x : Symbol(x, Decl(cloduleWithDuplicateMember1.ts, 12, 29))
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ module C {
>C : Symbol(C, Decl(cloduleWithDuplicateMember2.ts, 0, 0), Decl(cloduleWithDuplicateMember2.ts, 3, 1), Decl(cloduleWithDuplicateMember2.ts, 7, 1))

export function x() { }
>x : Symbol(C.x, Decl(cloduleWithDuplicateMember2.ts, 8, 10))
>x : Symbol(x, Decl(cloduleWithDuplicateMember2.ts, 8, 10))
}
2 changes: 1 addition & 1 deletion tests/baselines/reference/constructorOverloads4.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare module M {
>M : Symbol(M, Decl(constructorOverloads4.ts, 0, 0))

export class Function {
>Function : Symbol(M.Function, Decl(constructorOverloads4.ts, 0, 18))
>Function : Symbol(Function, Decl(constructorOverloads4.ts, 0, 18))

constructor(...args: string[]);
>args : Symbol(args, Decl(constructorOverloads4.ts, 2, 20))
Expand Down
16 changes: 8 additions & 8 deletions tests/baselines/reference/constructorOverloads5.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
>RegExp : Symbol(RegExp, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

export class RegExp {
>RegExp : Symbol(M.RegExp, Decl(constructorOverloads5.ts, 4, 67))
>RegExp : Symbol(RegExp, Decl(constructorOverloads5.ts, 4, 67))

constructor(pattern: string);
>pattern : Symbol(pattern, Decl(constructorOverloads5.ts, 6, 20))
Expand All @@ -27,27 +27,27 @@
>flags : Symbol(flags, Decl(constructorOverloads5.ts, 7, 36))

exec(string: string): string[];
>exec : Symbol(M.RegExp.exec, Decl(constructorOverloads5.ts, 7, 52))
>exec : Symbol(RegExp.exec, Decl(constructorOverloads5.ts, 7, 52))
>string : Symbol(string, Decl(constructorOverloads5.ts, 8, 13))

test(string: string): boolean;
>test : Symbol(M.RegExp.test, Decl(constructorOverloads5.ts, 8, 39))
>test : Symbol(RegExp.test, Decl(constructorOverloads5.ts, 8, 39))
>string : Symbol(string, Decl(constructorOverloads5.ts, 9, 13))

source: string;
>source : Symbol(M.RegExp.source, Decl(constructorOverloads5.ts, 9, 38))
>source : Symbol(RegExp.source, Decl(constructorOverloads5.ts, 9, 38))

global: boolean;
>global : Symbol(M.RegExp.global, Decl(constructorOverloads5.ts, 10, 23))
>global : Symbol(RegExp.global, Decl(constructorOverloads5.ts, 10, 23))

ignoreCase: boolean;
>ignoreCase : Symbol(M.RegExp.ignoreCase, Decl(constructorOverloads5.ts, 11, 24))
>ignoreCase : Symbol(RegExp.ignoreCase, Decl(constructorOverloads5.ts, 11, 24))

multiline: boolean;
>multiline : Symbol(M.RegExp.multiline, Decl(constructorOverloads5.ts, 12, 28))
>multiline : Symbol(RegExp.multiline, Decl(constructorOverloads5.ts, 12, 28))

lastIndex: boolean;
>lastIndex : Symbol(M.RegExp.lastIndex, Decl(constructorOverloads5.ts, 13, 27))
>lastIndex : Symbol(RegExp.lastIndex, Decl(constructorOverloads5.ts, 13, 27))
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/declarationEmitForTypesWhichNeedImportTypes.ts] ////

//// [b.ts]
export interface Named {}

export function createNamed(): Named {
return {};
}
//// [a.ts]
import { createNamed } from "./b";

export const Value = createNamed();


//// [b.js]
"use strict";
exports.__esModule = true;
function createNamed() {
return {};
}
exports.createNamed = createNamed;
//// [a.js]
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.Value = b_1.createNamed();


//// [b.d.ts]
export interface Named {
}
export declare function createNamed(): Named;
//// [a.d.ts]
export declare const Value: import("./b").Named;
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
=== tests/cases/compiler/b.ts ===
export interface Named {}
>Named : Symbol(Named, Decl(b.ts, 0, 0))

export function createNamed(): Named {
>createNamed : Symbol(createNamed, Decl(b.ts, 0, 25))
>Named : Symbol(Named, Decl(b.ts, 0, 0))

return {};
}
=== tests/cases/compiler/a.ts ===
import { createNamed } from "./b";
>createNamed : Symbol(createNamed, Decl(a.ts, 0, 8))

export const Value = createNamed();
>Value : Symbol(Value, Decl(a.ts, 2, 12))
>createNamed : Symbol(createNamed, Decl(a.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/b.ts ===
export interface Named {}
>Named : Named

export function createNamed(): Named {
>createNamed : () => Named
>Named : Named

return {};
>{} : {}
}
=== tests/cases/compiler/a.ts ===
import { createNamed } from "./b";
>createNamed : () => import("tests/cases/compiler/b").Named

export const Value = createNamed();
>Value : import("tests/cases/compiler/b").Named
>createNamed() : import("tests/cases/compiler/b").Named
>createNamed : () => import("tests/cases/compiler/b").Named

Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ module m {
>m : Symbol(m, Decl(declarationEmitImportInExportAssignmentModule.ts, 0, 0))

export module c {
>c : Symbol(x, Decl(declarationEmitImportInExportAssignmentModule.ts, 0, 10))
>c : Symbol(c, Decl(declarationEmitImportInExportAssignmentModule.ts, 0, 10))

export class c {
>c : Symbol(c, Decl(declarationEmitImportInExportAssignmentModule.ts, 1, 21))
}
}
import x = c;
>x : Symbol(x, Decl(declarationEmitImportInExportAssignmentModule.ts, 4, 5))
>c : Symbol(x, Decl(declarationEmitImportInExportAssignmentModule.ts, 0, 10))
>c : Symbol(c, Decl(declarationEmitImportInExportAssignmentModule.ts, 0, 10))

export var a: typeof x;
>a : Symbol(a, Decl(declarationEmitImportInExportAssignmentModule.ts, 6, 14))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ module m {
>m : typeof m

export module c {
>c : typeof x
>c : typeof m.c

export class c {
>c : c
}
}
import x = c;
>x : typeof x
>c : typeof x
>x : typeof c
>c : typeof c

export var a: typeof x;
>a : typeof x
>x : typeof x
>a : typeof c
>x : typeof c
}
export = m;
>m : typeof m
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface Things<P, T> {
>T : Symbol(T, Decl(this.ts, 1, 26))
}
export function make<P, CTor>(x: { new (): CTor & {props: P} }): Things<P, CTor> {
>make : Symbol(me.make, Decl(this.ts, 4, 1))
>make : Symbol(make, Decl(this.ts, 4, 1))
>P : Symbol(P, Decl(this.ts, 5, 21))
>CTor : Symbol(CTor, Decl(this.ts, 5, 23))
>x : Symbol(x, Decl(this.ts, 5, 30))
Expand Down Expand Up @@ -49,7 +49,7 @@ export namespace Something {

export const create = make(me.default);
>create : Symbol(create, Decl(this.ts, 17, 16))
>make : Symbol(me.make, Decl(this.ts, 4, 1))
>make : Symbol(make, Decl(this.ts, 4, 1))
>me.default : Symbol(me.default, Decl(this.ts, 10, 1))
>me : Symbol(me, Decl(this.ts, 0, 6))
>default : Symbol(me.default, Decl(this.ts, 10, 1))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ interface Foo {
}

export interface Foo {
>Foo : Foo
>Foo : import("tests/cases/conformance/es6/modules/defaultExportsCannotMerge04").Foo
}
Loading