Skip to content

Commit 104a52b

Browse files
author
Andy Hanson
committed
Support packageId for <reference types> too
1 parent 052bb69 commit 104a52b

7 files changed

+140
-14
lines changed

src/compiler/moduleNameResolver.ts

+12-8
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,17 @@ namespace ts {
5151
DtsOnly /** Only '.d.ts' */
5252
}
5353

54+
interface PathAndPackageId {
55+
readonly fileName: string;
56+
readonly packageId: PackageId;
57+
}
5458
/** Used with `Extensions.DtsOnly` to extract the path from TypeScript results. */
55-
function resolvedTypeScriptOnly(resolved: Resolved | undefined): string | undefined {
59+
function resolvedTypeScriptOnly(resolved: Resolved | undefined): PathAndPackageId | undefined {
5660
if (!resolved) {
5761
return undefined;
5862
}
5963
Debug.assert(extensionIsTypeScript(resolved.extension));
60-
return resolved.path;
64+
return { fileName: resolved.path, packageId: resolved.packageId };
6165
}
6266

6367
function createResolvedModuleWithFailedLookupLocations(resolved: Resolved | undefined, isExternalLibraryImport: boolean, failedLookupLocations: string[]): ResolvedModuleWithFailedLookupLocations {
@@ -201,18 +205,18 @@ namespace ts {
201205
let resolvedTypeReferenceDirective: ResolvedTypeReferenceDirective | undefined;
202206
if (resolved) {
203207
if (!options.preserveSymlinks) {
204-
resolved = realPath(resolved, host, traceEnabled);
208+
resolved = { ...resolved, fileName: realPath(resolved.fileName, host, traceEnabled) };
205209
}
206210

207211
if (traceEnabled) {
208-
trace(host, Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolved, primary);
212+
trace(host, Diagnostics.Type_reference_directive_0_was_successfully_resolved_to_1_primary_Colon_2, typeReferenceDirectiveName, resolved.fileName, primary);
209213
}
210-
resolvedTypeReferenceDirective = { primary, resolvedFileName: resolved };
214+
resolvedTypeReferenceDirective = { primary, resolvedFileName: resolved.fileName, packageId: resolved.packageId };
211215
}
212216

213217
return { resolvedTypeReferenceDirective, failedLookupLocations };
214218

215-
function primaryLookup(): string | undefined {
219+
function primaryLookup(): PathAndPackageId | undefined {
216220
// Check primary library paths
217221
if (typeRoots && typeRoots.length) {
218222
if (traceEnabled) {
@@ -237,8 +241,8 @@ namespace ts {
237241
}
238242
}
239243

240-
function secondaryLookup(): string | undefined {
241-
let resolvedFile: string;
244+
function secondaryLookup(): PathAndPackageId | undefined {
245+
let resolvedFile: PathAndPackageId;
242246
const initialLocationForSecondaryLookup = containingFile && getDirectoryPath(containingFile);
243247

244248
if (initialLocationForSecondaryLookup !== undefined) {

src/compiler/program.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ namespace ts {
14271427
}
14281428

14291429
function processRootFile(fileName: string, isDefaultLib: boolean) {
1430-
processSourceFile(normalizePath(fileName), isDefaultLib);
1430+
processSourceFile(normalizePath(fileName), isDefaultLib, /*packageId*/ undefined);
14311431
}
14321432

14331433
function fileReferenceIsEqualTo(a: FileReference, b: FileReference): boolean {
@@ -1591,9 +1591,9 @@ namespace ts {
15911591
}
15921592

15931593
/** This has side effects through `findSourceFile`. */
1594-
function processSourceFile(fileName: string, isDefaultLib: boolean, refFile?: SourceFile, refPos?: number, refEnd?: number): void {
1594+
function processSourceFile(fileName: string, isDefaultLib: boolean, packageId: PackageId | undefined, refFile?: SourceFile, refPos?: number, refEnd?: number): void {
15951595
getSourceFileFromReferenceWorker(fileName,
1596-
fileName => findSourceFile(fileName, toPath(fileName), isDefaultLib, refFile, refPos, refEnd, /*packageId*/ undefined),
1596+
fileName => findSourceFile(fileName, toPath(fileName), isDefaultLib, refFile, refPos, refEnd, packageId),
15971597
(diagnostic, ...args) => {
15981598
fileProcessingDiagnostics.add(refFile !== undefined && refEnd !== undefined && refPos !== undefined
15991599
? createFileDiagnostic(refFile, refPos, refEnd - refPos, diagnostic, ...args)
@@ -1735,7 +1735,7 @@ namespace ts {
17351735
function processReferencedFiles(file: SourceFile, isDefaultLib: boolean) {
17361736
forEach(file.referencedFiles, ref => {
17371737
const referencedFileName = resolveTripleslashReference(ref.fileName, file.fileName);
1738-
processSourceFile(referencedFileName, isDefaultLib, file, ref.pos, ref.end);
1738+
processSourceFile(referencedFileName, isDefaultLib, /*packageId*/ undefined, file, ref.pos, ref.end);
17391739
});
17401740
}
17411741

@@ -1766,7 +1766,7 @@ namespace ts {
17661766
if (resolvedTypeReferenceDirective) {
17671767
if (resolvedTypeReferenceDirective.primary) {
17681768
// resolved from the primary path
1769-
processSourceFile(resolvedTypeReferenceDirective.resolvedFileName, /*isDefaultLib*/ false, refFile, refPos, refEnd);
1769+
processSourceFile(resolvedTypeReferenceDirective.resolvedFileName, /*isDefaultLib*/ false, resolvedTypeReferenceDirective.packageId, refFile, refPos, refEnd);
17701770
}
17711771
else {
17721772
// If we already resolved to this file, it must have been a secondary reference. Check file contents
@@ -1789,7 +1789,7 @@ namespace ts {
17891789
}
17901790
else {
17911791
// First resolution of this library
1792-
processSourceFile(resolvedTypeReferenceDirective.resolvedFileName, /*isDefaultLib*/ false, refFile, refPos, refEnd);
1792+
processSourceFile(resolvedTypeReferenceDirective.resolvedFileName, /*isDefaultLib*/ false, resolvedTypeReferenceDirective.packageId, refFile, refPos, refEnd);
17931793
}
17941794
}
17951795
}

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4038,6 +4038,7 @@ namespace ts {
40384038
primary: boolean;
40394039
// The location of the .d.ts file we located, or undefined if resolution failed
40404040
resolvedFileName?: string;
4041+
packageId?: PackageId;
40414042
}
40424043

40434044
export interface ResolvedTypeReferenceDirectiveWithFailedLookupLocations {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//// [tests/cases/compiler/duplicatePackage_referenceTypes.ts] ////
2+
3+
//// [index.d.ts]
4+
/// <reference types="foo" />
5+
import { Foo } from "foo";
6+
export const foo: Foo;
7+
8+
//// [index.d.ts]
9+
export class Foo { private x; }
10+
11+
//// [package.json]
12+
{ "name": "foo", "version": "1.2.3" }
13+
14+
//// [index.d.ts]
15+
export class Foo { private x; }
16+
17+
//// [package.json]
18+
{ "name": "foo", "version": "1.2.3" }
19+
20+
//// [index.ts]
21+
import * as a from "a";
22+
import { Foo } from "foo";
23+
24+
let foo: Foo = a.foo;
25+
26+
27+
//// [index.js]
28+
"use strict";
29+
exports.__esModule = true;
30+
var a = require("a");
31+
var foo = a.foo;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== /index.ts ===
2+
import * as a from "a";
3+
>a : Symbol(a, Decl(index.ts, 0, 6))
4+
5+
import { Foo } from "foo";
6+
>Foo : Symbol(Foo, Decl(index.ts, 1, 8))
7+
8+
let foo: Foo = a.foo;
9+
>foo : Symbol(foo, Decl(index.ts, 3, 3))
10+
>Foo : Symbol(Foo, Decl(index.ts, 1, 8))
11+
>a.foo : Symbol(a.foo, Decl(index.d.ts, 2, 12))
12+
>a : Symbol(a, Decl(index.ts, 0, 6))
13+
>foo : Symbol(a.foo, Decl(index.d.ts, 2, 12))
14+
15+
=== /node_modules/a/index.d.ts ===
16+
/// <reference types="foo" />
17+
import { Foo } from "foo";
18+
>Foo : Symbol(Foo, Decl(index.d.ts, 1, 8))
19+
20+
export const foo: Foo;
21+
>foo : Symbol(foo, Decl(index.d.ts, 2, 12))
22+
>Foo : Symbol(Foo, Decl(index.d.ts, 1, 8))
23+
24+
=== /node_modules/a/node_modules/foo/index.d.ts ===
25+
export class Foo { private x; }
26+
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 0))
27+
>x : Symbol(Foo.x, Decl(index.d.ts, 0, 18))
28+
29+
=== /node_modules/@types/foo/index.d.ts ===
30+
export class Foo { private x; }
31+
>Foo : Symbol(Foo, Decl(index.d.ts, 0, 0))
32+
>x : Symbol(Foo.x, Decl(index.d.ts, 0, 18))
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
=== /index.ts ===
2+
import * as a from "a";
3+
>a : typeof a
4+
5+
import { Foo } from "foo";
6+
>Foo : typeof Foo
7+
8+
let foo: Foo = a.foo;
9+
>foo : Foo
10+
>Foo : Foo
11+
>a.foo : Foo
12+
>a : typeof a
13+
>foo : Foo
14+
15+
=== /node_modules/a/index.d.ts ===
16+
/// <reference types="foo" />
17+
import { Foo } from "foo";
18+
>Foo : typeof Foo
19+
20+
export const foo: Foo;
21+
>foo : Foo
22+
>Foo : Foo
23+
24+
=== /node_modules/a/node_modules/foo/index.d.ts ===
25+
export class Foo { private x; }
26+
>Foo : Foo
27+
>x : any
28+
29+
=== /node_modules/@types/foo/index.d.ts ===
30+
export class Foo { private x; }
31+
>Foo : Foo
32+
>x : any
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @noImplicitReferences: true
2+
3+
// @Filename: /node_modules/a/index.d.ts
4+
/// <reference types="foo" />
5+
import { Foo } from "foo";
6+
export const foo: Foo;
7+
8+
// @Filename: /node_modules/a/node_modules/foo/index.d.ts
9+
export class Foo { private x; }
10+
11+
// @Filename: /node_modules/a/node_modules/foo/package.json
12+
{ "name": "foo", "version": "1.2.3" }
13+
14+
// @Filename: /node_modules/@types/foo/index.d.ts
15+
export class Foo { private x; }
16+
17+
// @Filename: /node_modules/@types/foo/package.json
18+
{ "name": "foo", "version": "1.2.3" }
19+
20+
// @Filename: /index.ts
21+
import * as a from "a";
22+
import { Foo } from "foo";
23+
24+
let foo: Foo = a.foo;

0 commit comments

Comments
 (0)