Skip to content

Commit 6c9cd9a

Browse files
committed
Library references WIP
1 parent 60bd06d commit 6c9cd9a

File tree

3 files changed

+54
-10
lines changed

3 files changed

+54
-10
lines changed

src/compiler/program.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ namespace ts {
1313
const emptyArray: any[] = [];
1414

1515
const defaultLibrarySearchPaths = [
16-
'./',
17-
'./typings/',
18-
'./node_modules/',
19-
'./node_modules/@types/',
16+
"",
17+
"typings/",
18+
"node_modules/",
19+
"node_modules/@types/",
2020
];
2121

2222
export const version = "1.9.0";
@@ -1489,13 +1489,12 @@ namespace ts {
14891489
skipDefaultLib = skipDefaultLib || file.hasNoDefaultLib;
14901490

14911491
const basePath = getDirectoryPath(fileName);
1492+
const libraryBasePath = options.rootDir || options.configFilePath || getDirectoryPath(file.fileName);
14921493
if (!options.noResolve) {
14931494
processReferencedFiles(file, basePath);
1494-
}
1495-
1496-
const libraryRoot = computeCommonSourceDirectory(files);
14971495

1498-
processReferencedLibraries(file, libraryRoot);
1496+
processReferencedLibraries(file, libraryBasePath);
1497+
}
14991498

15001499
// always process imported modules to record module name resolutions
15011500
processImportedModules(file, basePath);
@@ -1521,12 +1520,34 @@ namespace ts {
15211520
function processReferencedLibraries(file: SourceFile, compilationRoot: string) {
15221521
const searchPaths = getEffectiveLibrarySearchPaths();
15231522
for (const ref of file.referencedLibraries) {
1523+
let foundIt = false;
1524+
15241525
for (const path of searchPaths) {
1525-
const combinedPath = combinePaths(path, ref.fileName);
1526+
let typingFilename = "index.d.ts";
1527+
const searchPath = combinePaths(combinePaths(compilationRoot, path), ref.fileName);
1528+
const packageJsonPath = combinePaths(searchPath, "package.json");
1529+
console.log('Check for ' + packageJsonPath);
1530+
if (host.fileExists(packageJsonPath)) {
1531+
let package: { typings?: string } = {};
1532+
try {
1533+
package = JSON.parse(host.readFile(packageJsonPath));
1534+
} catch (e) { }
1535+
1536+
if (package.typings) {
1537+
typingFilename = package.typings;
1538+
}
1539+
}
1540+
1541+
const combinedPath = normalizePath(combinePaths(compilationRoot, combinePaths(combinePaths(path, ref.fileName), typingFilename)));
15261542
if (host.fileExists(combinedPath)) {
1527-
processSourceFile(combinedPath, false, file, ref.pos, ref.end);
1543+
processSourceFile(combinedPath, /*isDefaultLib*/ false, /*isReference*/ true, file, ref.pos, ref.end);
1544+
foundIt = true;
1545+
break;
15281546
}
15291547
}
1548+
if (!foundIt) {
1549+
fileProcessingDiagnostics.add(createFileDiagnostic(file, ref.pos, ref.end - ref.pos, Diagnostics.Cannot_find_name_0, ref.fileName));
1550+
}
15301551
}
15311552
}
15321553

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @noImplicitReferences: true
2+
3+
// @filename: typings/jquery/index.d.ts
4+
declare var $: { foo(): void };
5+
6+
7+
// @filename: consumer.ts
8+
/// <reference library="jquery" />
9+
$.foo();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @noImplicitReferences: true
2+
3+
// @filename: typings/jquery/package.json
4+
{
5+
"typings": "jquery.d.ts"
6+
}
7+
8+
// @filename: typings/jquery/jquery.d.ts
9+
declare var $: { foo(): void };
10+
11+
12+
// @filename: consumer.ts
13+
/// <reference library="jquery" />
14+
$.foo();

0 commit comments

Comments
 (0)