Skip to content

Change import type determination to not use a RE on the symbol name #25381

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
Jul 3, 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
84 changes: 43 additions & 41 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3733,19 +3733,56 @@ namespace ts {
return top;
}

function getSpecifierForModuleSymbol(symbol: Symbol, context: NodeBuilderContext) {
const file = getDeclarationOfKind<SourceFile>(symbol, SyntaxKind.SourceFile);
if (file && file.moduleName !== undefined) {
// Use the amd name if it is available
return file.moduleName;
}
if (!file) {
if (context.tracker.trackReferencedAmbientModule) {
const ambientDecls = filter(symbol.declarations, isAmbientModule);
if (length(ambientDecls)) {
for (const decl of ambientDecls) {
context.tracker.trackReferencedAmbientModule(decl);
}
}
}
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
}
else {
if (!context.enclosingDeclaration || !context.tracker.moduleResolverHost) {
// If there's no context declaration, we can't lookup a non-ambient specifier, so we just use the symbol name
return (symbol.escapedName as string).substring(1, (symbol.escapedName as string).length - 1);
}
const contextFile = getSourceFileOfNode(getOriginalNode(context.enclosingDeclaration));
const links = getSymbolLinks(symbol);
let specifier = links.specifierCache && links.specifierCache.get(contextFile.path);
if (!specifier) {
specifier = flatten(moduleSpecifiers.getModuleSpecifiers(
symbol,
compilerOptions,
contextFile,
context.tracker.moduleResolverHost,
context.tracker.moduleResolverHost.getSourceFiles!(), // TODO: GH#18217
{ importModuleSpecifierPreference: "non-relative" }
))[0];
links.specifierCache = links.specifierCache || createMap();
links.specifierCache.set(contextFile.path, specifier);
}
return specifier;
}
}

function symbolToTypeNode(symbol: Symbol, context: NodeBuilderContext, meaning: SymbolFlags, overrideTypeArguments?: ReadonlyArray<TypeNode>): TypeNode {
const chain = lookupSymbolChain(symbol, context, meaning, !(context.flags & NodeBuilderFlags.UseAliasDefinedOutsideCurrentScope)); // If we're using aliases outside the current scope, dont bother with the module

context.flags |= NodeBuilderFlags.InInitialEntityName;
const rootName = getNameOfSymbolAsWritten(chain[0], context);
context.flags ^= NodeBuilderFlags.InInitialEntityName;

const isTypeOf = meaning === SymbolFlags.Value;
if (ambientModuleSymbolRegex.test(rootName)) {
if (some(chain[0].declarations, hasNonGlobalAugmentationExternalModuleSymbol)) {
// module is root, must use `ImportTypeNode`
const nonRootParts = chain.length > 1 ? createAccessFromSymbolChain(chain, chain.length - 1, 1) : undefined;
const typeParameterNodes = overrideTypeArguments || lookupTypeParameterNodes(chain, 0, context);
const lit = createLiteralTypeNode(createLiteral(rootName.substring(1, rootName.length - 1)));
const lit = createLiteralTypeNode(createLiteral(getSpecifierForModuleSymbol(chain[0], context)));
if (!nonRootParts || isEntityName(nonRootParts)) {
if (nonRootParts) {
const lastId = isIdentifier(nonRootParts) ? nonRootParts : nonRootParts.right;
Expand Down Expand Up @@ -3990,41 +4027,6 @@ namespace ts {
return "default";
}
if (symbol.declarations && symbol.declarations.length) {
if (some(symbol.declarations, hasExternalModuleSymbol) && context!.enclosingDeclaration) { // TODO: GH#18217
const file = getDeclarationOfKind<SourceFile>(symbol, SyntaxKind.SourceFile);
if (!file || !context!.tracker.moduleResolverHost) {
if (context!.tracker.trackReferencedAmbientModule) {
const ambientDecls = filter(symbol.declarations, isAmbientModule);
if (length(ambientDecls)) {
for (const decl of ambientDecls) {
context!.tracker.trackReferencedAmbientModule!(decl); // TODO: GH#18217
}
}
}
// ambient module, just use declaration/symbol name (fallthrough)
}
else {
if (file.moduleName) {
return `"${file.moduleName}"`;
}
const contextFile = getSourceFileOfNode(getOriginalNode(context!.enclosingDeclaration))!;
const links = getSymbolLinks(symbol);
let specifier = links.specifierCache && links.specifierCache.get(contextFile.path);
if (!specifier) {
specifier = flatten(moduleSpecifiers.getModuleSpecifiers(
symbol,
compilerOptions,
contextFile,
context!.tracker.moduleResolverHost!,
context!.tracker.moduleResolverHost!.getSourceFiles!(),
{ importModuleSpecifierPreference: "non-relative" }
))[0];
links.specifierCache = links.specifierCache || createMap();
links.specifierCache.set(contextFile.path, specifier);
}
return `"${specifier}"`;
}
}
const declaration = symbol.declarations[0];
const name = getNameOfDeclaration(declaration);
if (name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ declare namespace demoNS {
>f : () => void
}
declare module 'demoModule' {
>'demoModule' : typeof 'demoModule'
>'demoModule' : typeof import("demoModule")

import alias = demoNS;
>alias : typeof alias
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/ambientDeclarations.types
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ var q = M1.fn();
// Ambient external module in the global module
// Ambient external module with a string literal name that is a top level external module name
declare module 'external1' {
>'external1' : typeof 'external1'
>'external1' : typeof import("external1")

var q;
>q : any
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ambientDeclarationsExternal.types
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var n: number;
=== tests/cases/conformance/ambient/decls.ts ===
// Ambient external module with export assignment
declare module 'equ' {
>'equ' : typeof 'equ'
>'equ' : typeof import("equ")

var x;
>x : any
Expand All @@ -30,7 +30,7 @@ declare module 'equ' {
}

declare module 'equ2' {
>'equ2' : typeof 'equ2'
>'equ2' : typeof import("equ2")

var x: number;
>x : number
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/ambientErrors.types
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,16 @@ module M2 {
>M2 : any

declare module 'nope' { }
>'nope' : typeof 'nope'
>'nope' : typeof import("nope")
}

// Ambient external module with a string literal name that isn't a top level external module name
declare module '../foo' { }
>'../foo' : typeof '../foo'
>'../foo' : typeof import("../foo")

// Ambient external module with export assignment and other exported members
declare module 'bar' {
>'bar' : typeof 'bar'
>'bar' : typeof import("bar")

var n;
>n : any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var c = new A();

=== tests/cases/compiler/ambientExternalModuleWithInternalImportDeclaration_0.ts ===
declare module 'M' {
>'M' : typeof 'M'
>'M' : typeof import("M")

module C {
>C : typeof C
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ declare module "./relativeModule" {
}

declare module ".\\relativeModule" {
>".\\relativeModule" : typeof import(".\\\\relativeModule")
>".\\relativeModule" : typeof import(".\\relativeModule")

var x: string;
>x : string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var c = new A();

=== tests/cases/compiler/ambientExternalModuleWithoutInternalImportDeclaration_0.ts ===
declare module 'M' {
>'M' : typeof 'M'
>'M' : typeof import("M")

module C {
>C : typeof C
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/augmentExportEquals3.types
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== tests/cases/compiler/file1.ts ===
function foo() {}
>foo : typeof foo
>foo : typeof import("o")

namespace foo {
>foo : typeof foo
>foo : typeof import("o")

export var v = 1;
>v : number
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/augmentExportEquals3_1.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ declare module "file1" {
>"file1" : typeof import("file1")

function foo(): void;
>foo : typeof foo
>foo : typeof import("o")

namespace foo {
>foo : typeof foo
>foo : typeof import("o")

export var v: number;
>v : number
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/augmentExportEquals4.types
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== tests/cases/compiler/file1.ts ===
class foo {}
>foo : foo
>foo : import("o")

namespace foo {
>foo : typeof foo
>foo : typeof import("o")

export var v = 1;
>v : number
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/augmentExportEquals4_1.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ declare module "file1" {
>"file1" : typeof import("file1")

class foo {}
>foo : foo
>foo : import("o")

namespace foo {
>foo : typeof foo
>foo : typeof import("o")

export var v: number;
>v : number
Expand Down
6 changes: 3 additions & 3 deletions tests/baselines/reference/augmentExportEquals5.types
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ declare module "express" {
>"express" : typeof import("express")

function e(): e.Express;
>e : typeof e
>e : typeof import("e")
>e : any
>Express : e.Express
>Express : import("e").Express

namespace e {
>e : typeof e
>e : typeof import("e")

interface IRoute {
>IRoute : IRoute
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/augmentExportEquals6.types
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
=== tests/cases/compiler/file1.ts ===
class foo {}
>foo : foo
>foo : import("o")

namespace foo {
>foo : typeof foo
>foo : typeof import("o")

export class A {}
>A : A
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/augmentExportEquals6_1.types
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ declare module "file1" {
>"file1" : typeof import("file1")

class foo {}
>foo : foo
>foo : import("o")

namespace foo {
>foo : typeof foo
>foo : typeof import("o")

class A {}
>A : A
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/bangInModuleName.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare module "http" {
}

declare module 'intern/dojo/node!http' {
>'intern/dojo/node!http' : typeof 'intern/dojo/node!http'
>'intern/dojo/node!http' : typeof import("intern/dojo/node!http")

import http = require('http');
>http : typeof http
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ class List<T> { }
>T : T

declare module 'mod1' {
>'mod1' : typeof 'mod1'
>'mod1' : typeof import("mod1")

class Foo {
>Foo : Foo
}
}

declare module 'moo' {
>'moo' : typeof 'moo'
>'moo' : typeof import("moo")

import x = require('mod1');
>x : typeof x
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//// [tests/cases/compiler/declarationEmitCrossFileImportTypeOfAmbientModule.ts] ////

//// [component.d.ts]
declare module '@namespace/component' {
export class Foo {}
}
//// [index.d.ts]
import { Foo } from "@namespace/component";
export declare const item: typeof Foo;
//// [index.ts]
import { item } from "../somepackage";
export const reeexported = item;


//// [index.js]
"use strict";
exports.__esModule = true;
var somepackage_1 = require("../somepackage");
exports.reeexported = somepackage_1.item;


//// [index.d.ts]
/// <reference path="../../types/component.d.ts" />
export declare const reeexported: typeof import("@namespace/component").Foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/types/component.d.ts ===
declare module '@namespace/component' {
>'@namespace/component' : Symbol('@namespace/component', Decl(component.d.ts, 0, 0))

export class Foo {}
>Foo : Symbol(Foo, Decl(component.d.ts, 0, 39))
}
=== tests/cases/compiler/packages/somepackage/index.d.ts ===
import { Foo } from "@namespace/component";
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 8))

export declare const item: typeof Foo;
>item : Symbol(item, Decl(index.d.ts, 1, 20))
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 8))

=== tests/cases/compiler/packages/secondpackage/index.ts ===
import { item } from "../somepackage";
>item : Symbol(item, Decl(index.ts, 0, 8))

export const reeexported = item;
>reeexported : Symbol(reeexported, Decl(index.ts, 1, 12))
>item : Symbol(item, Decl(index.ts, 0, 8))

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/types/component.d.ts ===
declare module '@namespace/component' {
>'@namespace/component' : typeof import("@namespace/component")

export class Foo {}
>Foo : Foo
}
=== tests/cases/compiler/packages/somepackage/index.d.ts ===
import { Foo } from "@namespace/component";
>Foo : typeof Foo

export declare const item: typeof Foo;
>item : typeof Foo
>Foo : typeof Foo

=== tests/cases/compiler/packages/secondpackage/index.ts ===
import { item } from "../somepackage";
>item : typeof import("@namespace/component").Foo

export const reeexported = item;
>reeexported : typeof import("@namespace/component").Foo
>item : typeof import("@namespace/component").Foo

2 changes: 1 addition & 1 deletion tests/baselines/reference/declaredExternalModule.types
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/declaredExternalModule.ts ===
declare module 'connect' {
>'connect' : typeof 'connect'
>'connect' : typeof import("connect")

interface connectModule {
>connectModule : connectModule
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
=== tests/cases/compiler/declaredExternalModuleWithExportAssignment.ts ===
declare module 'connect' {
>'connect' : typeof 'connect'
>'connect' : typeof import("connect")

interface connectModule {
>connectModule : connectModule
Expand Down
Loading