Skip to content

Commit acf557d

Browse files
committed
Do not emit references in generated declaration files if the --noresolve flag was set. This fixes a crash in the compiler when generating declarations with /// reference and noResolve
1 parent 4381f72 commit acf557d

File tree

5 files changed

+173
-27
lines changed

5 files changed

+173
-27
lines changed

scripts/importDefinitllyTypedTests.ts

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
declare var require: any, process: any;
2+
declare var __dirname: any;
3+
4+
var fs = require("fs");
5+
var path = require("path");
6+
var child_process = require('child_process');
7+
8+
var tscRoot = path.join(__dirname, "..\\");
9+
var tscPath = path.join(tscRoot, "built", "instrumented", "tsc.js");
10+
var rwcTestPath = path.join(tscRoot, "tests", "cases", "rwc", "dt");
11+
var definitelyTypedRoot = process.argv[2];
12+
13+
function fileExtensionIs(path: string, extension: string): boolean {
14+
var pathLen = path.length;
15+
var extLen = extension.length;
16+
return pathLen > extLen && path.substr(pathLen - extLen, extLen).toLocaleLowerCase() === extension.toLocaleLowerCase();
17+
}
18+
19+
function copyFileSync(source, destination) {
20+
var text = fs.readFileSync(source);
21+
fs.writeFileSync(destination, text);
22+
}
23+
24+
function importDefinitelyTypedTest(testCaseName: string, testFiles: string[], responseFile: string ) {
25+
var cmd = "node " + tscPath + " --module commonjs " + testFiles.join(" ");
26+
if (responseFile) cmd += " @" + responseFile;
27+
28+
var testDirectoryName = testCaseName + "_" + Math.floor((Math.random() * 10000) + 1);
29+
var testDirectoryPath = path.join(process.env["temp"], testDirectoryName);
30+
if (fs.existsSync(testDirectoryPath)) {
31+
throw new Error("Could not create test directory");
32+
}
33+
fs.mkdirSync(testDirectoryPath);
34+
35+
child_process.exec(cmd, {
36+
maxBuffer: 1 * 1024 * 1024,
37+
cwd: testDirectoryPath
38+
}, (error, stdout, stderr) => {
39+
//console.log("importing " + testCaseName + " ...");
40+
//console.log(cmd);
41+
42+
if (error) {
43+
console.log("importing " + testCaseName + " ...");
44+
console.log(cmd);
45+
console.log("==> error " + JSON.stringify(error));
46+
console.log("==> stdout " + String(stdout));
47+
console.log("==> stderr " + String(stderr));
48+
console.log("\r\n");
49+
return;
50+
}
51+
52+
// copy generated file to output location
53+
var outputFilePath = path.join(testDirectoryPath, "iocapture0.json");
54+
var testCasePath = path.join(rwcTestPath, "DefinitelyTyped_" + testCaseName + ".json");
55+
copyFileSync(outputFilePath, testCasePath);
56+
57+
//console.log("output generated at: " + outputFilePath);
58+
59+
if (!fs.existsSync(testCasePath)) {
60+
throw new Error("could not find test case at: " + testCasePath);
61+
}
62+
else {
63+
fs.unlinkSync(outputFilePath);
64+
fs.rmdirSync(testDirectoryPath);
65+
//console.log("testcase generated at: " + testCasePath);
66+
//console.log("Done.");
67+
}
68+
//console.log("\r\n");
69+
70+
})
71+
.on('error', function (error) {
72+
console.log("==> error " + JSON.stringify(error));
73+
console.log("\r\n");
74+
});
75+
}
76+
77+
function importDefinitelyTypedTests(definitelyTypedRoot: string): void {
78+
fs.readdir(definitelyTypedRoot, (err, subDirectorys) => {
79+
if (err) throw err;
80+
81+
subDirectorys
82+
.filter(d => ["_infrastructure", "node_modules", ".git"].indexOf(d) >= 0)
83+
.filter(i => fs.statSync(path.join(definitelyTypedRoot, i)).isDirectory())
84+
.forEach(d => {
85+
var directoryPath = path.join(definitelyTypedRoot, d);
86+
fs.readdir(directoryPath, function (err, files) {
87+
if (err) throw err;
88+
89+
var tsFiles = [];
90+
var testFiles = [];
91+
var paramFile;
92+
93+
files
94+
.map(f => path.join(directoryPath, f))
95+
.forEach(f => {
96+
if (fileExtensionIs(f, ".ts")) tsFiles.push(f);
97+
else if (fileExtensionIs(f, ".tscparams")) paramFile = f;
98+
99+
if (fileExtensionIs(f, "-tests.ts")) testFiles.push(f);
100+
});
101+
102+
if (testFiles.length === 0) {
103+
// no test files but multiple d.ts's, e.g. winjs
104+
var regexp = new RegExp(d + "(([-][0-9])|([\.]d[\.]ts))");
105+
if (tsFiles.length > 1 && tsFiles.every(t => fileExtensionIs(t, ".d.ts") && regexp.test(t))) {
106+
tsFiles.forEach(filename => {
107+
importDefinitelyTypedTest(path.basename(filename, ".d.ts"), [filename], paramFile);
108+
});
109+
}
110+
else {
111+
importDefinitelyTypedTest(d, tsFiles, paramFile);
112+
}
113+
}
114+
else {
115+
testFiles.forEach(filename => {
116+
importDefinitelyTypedTest(path.basename(filename, "-tests.ts"), [filename], paramFile);
117+
});
118+
}
119+
});
120+
})
121+
});
122+
}
123+
124+
importDefinitelyTypedTests(definitelyTypedRoot);

src/compiler/emitter.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3079,10 +3079,8 @@ module ts {
30793079
}
30803080
}
30813081

3082-
function resolveScriptReference(sourceFile: SourceFile, reference: FileReference) {
3083-
var referenceFileName = compilerOptions.noResolve
3084-
? reference.filename
3085-
: normalizePath(combinePaths(getDirectoryPath(sourceFile.filename), reference.filename));
3082+
function tryResolveScriptReference(sourceFile: SourceFile, reference: FileReference) {
3083+
var referenceFileName = normalizePath(combinePaths(getDirectoryPath(sourceFile.filename), reference.filename));
30863084
return program.getSourceFile(referenceFileName);
30873085
}
30883086

@@ -3108,21 +3106,23 @@ module ts {
31083106

31093107
if (root) {
31103108
// Emiting single file so emit references in this file only
3111-
var addedGlobalFileReference = false;
3112-
forEach(root.referencedFiles, fileReference => {
3113-
var referencedFile = resolveScriptReference(root, fileReference);
3114-
3115-
// All the references that are not going to be part of same file
3116-
if ((referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
3117-
shouldEmitToOwnFile(referencedFile) || // This is referenced file is emitting its own js file
3118-
!addedGlobalFileReference) { // Or the global out file corresponding to this reference was not added
3119-
3120-
writeReferencePath(referencedFile);
3121-
if (!isExternalModuleOrDeclarationFile(referencedFile)) {
3122-
addedGlobalFileReference = true;
3109+
if (!compilerOptions.noResolve) {
3110+
var addedGlobalFileReference = false;
3111+
forEach(root.referencedFiles, fileReference => {
3112+
var referencedFile = tryResolveScriptReference(root, fileReference);
3113+
3114+
// All the references that are not going to be part of same file
3115+
if (referencedFile && (referencedFile.flags & NodeFlags.DeclarationFile) || // This is a declare file reference
3116+
shouldEmitToOwnFile(referencedFile) || // This is referenced file is emitting its own js file
3117+
!addedGlobalFileReference) { // Or the global out file corresponding to this reference was not added
3118+
3119+
writeReferencePath(referencedFile);
3120+
if (!isExternalModuleOrDeclarationFile(referencedFile)) {
3121+
addedGlobalFileReference = true;
3122+
}
31233123
}
3124-
}
3125-
});
3124+
});
3125+
}
31263126

31273127
emitNode(root);
31283128
}
@@ -3132,17 +3132,19 @@ module ts {
31323132
forEach(program.getSourceFiles(), sourceFile => {
31333133
if (!isExternalModuleOrDeclarationFile(sourceFile)) {
31343134
// Check what references need to be added
3135-
forEach(sourceFile.referencedFiles, fileReference => {
3136-
var referencedFile = resolveScriptReference(sourceFile, fileReference);
3135+
if (!compilerOptions.noResolve) {
3136+
forEach(sourceFile.referencedFiles, fileReference => {
3137+
var referencedFile = tryResolveScriptReference(sourceFile, fileReference);
31373138

3138-
// If the reference file is declaration file or external module emit that reference
3139-
if (isExternalModuleOrDeclarationFile(referencedFile) &&
3140-
!contains(emittedReferencedFiles, referencedFile)) { // If the file refernece was not already emitted
3139+
// If the reference file is declaration file or external module emit that reference
3140+
if (referencedFile && isExternalModuleOrDeclarationFile(referencedFile) &&
3141+
!contains(emittedReferencedFiles, referencedFile)) { // If the file refernece was not already emitted
31413142

3142-
writeReferencePath(referencedFile);
3143-
emittedReferencedFiles.push(referencedFile);
3144-
}
3145-
});
3143+
writeReferencePath(referencedFile);
3144+
emittedReferencedFiles.push(referencedFile);
3145+
}
3146+
});
3147+
}
31463148

31473149
emitNode(sourceFile);
31483150
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [declarationEmit_invalidReference.ts]
2+
/// <reference path="invalid.ts" />
3+
var x = 0;
4+
5+
//// [declarationEmit_invalidReference.js]
6+
/// <reference path="invalid.ts" />
7+
var x = 0;
8+
9+
10+
//// [declarationEmit_invalidReference.d.ts]
11+
declare var x: number;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/declarationEmit_invalidReference.ts ===
2+
/// <reference path="invalid.ts" />
3+
var x = 0;
4+
>x : number
5+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @declaration: true
2+
// @noresolve: true
3+
/// <reference path="invalid.ts" />
4+
var x = 0;

0 commit comments

Comments
 (0)