Skip to content

Commit 97afc1d

Browse files
committed
Merge pull request microsoft#3484 from Microsoft/noExtension_noExtraErrors
do not report extra error if file was already found without extension
2 parents a3916ff + 970dc49 commit 97afc1d

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/compiler/program.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,17 @@ namespace ts {
351351
}
352352
}
353353
else {
354-
if (options.allowNonTsExtensions && !findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd)) {
355-
diagnostic = Diagnostics.File_0_not_found;
356-
diagnosticArgument = [fileName];
357-
}
358-
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) {
359-
diagnostic = Diagnostics.File_0_not_found;
360-
fileName += ".ts";
361-
diagnosticArgument = [fileName];
354+
var nonTsFile: SourceFile = options.allowNonTsExtensions && findSourceFile(fileName, isDefaultLib, refFile, refPos, refEnd);
355+
if (!nonTsFile) {
356+
if (options.allowNonTsExtensions) {
357+
diagnostic = Diagnostics.File_0_not_found;
358+
diagnosticArgument = [fileName];
359+
}
360+
else if (!forEach(supportedExtensions, extension => findSourceFile(fileName + extension, isDefaultLib, refFile, refPos, refEnd))) {
361+
diagnostic = Diagnostics.File_0_not_found;
362+
fileName += ".ts";
363+
diagnosticArgument = [fileName];
364+
}
362365
}
363366
}
364367

tests/cases/unittests/transpile.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
module ts {
44
describe("Transpile", () => {
55

6-
function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, moduleName?: string, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
6+
function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, fileName?: string, moduleName?: string, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
77
let diagnostics: Diagnostic[] = [];
8-
let result = transpile(input, compilerOptions, "file.ts", diagnostics, moduleName);
8+
let result = transpile(input, compilerOptions, fileName || "file.ts", diagnostics, moduleName);
99

1010
for (let i = 0; i < expectedDiagnosticCodes.length; i++) {
1111
assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`);
@@ -19,41 +19,41 @@ module ts {
1919

2020
it("Generates correct compilerOptions diagnostics", () => {
2121
// Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher."
22-
runTest(`var x = 0;`, {}, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]);
22+
runTest(`var x = 0;`, {}, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [5047]);
2323
});
2424

2525
it("Generates no diagnostics with valid inputs", () => {
2626
// No errors
27-
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
27+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
2828
});
2929

3030
it("Generates no diagnostics for missing file references", () => {
3131
runTest(`/// <reference path="file2.ts" />
3232
var x = 0;`,
33-
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
33+
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
3434
});
3535

3636
it("Generates no diagnostics for missing module imports", () => {
3737
runTest(`import {a} from "module2";`,
38-
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
38+
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined,/*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
3939
});
4040

4141
it("Generates expected syntactic diagnostics", () => {
4242
runTest(`a b`,
43-
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
43+
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
4444
});
4545

4646
it("Does not generate semantic diagnostics", () => {
4747
runTest(`var x: string = 0;`,
48-
{ module: ModuleKind.CommonJS }, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
48+
{ module: ModuleKind.CommonJS }, /*fileName*/ undefined, /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
4949
});
5050

5151
it("Generates module output", () => {
52-
runTest(`var x = 0;`, { module: ModuleKind.AMD }, /*moduleName*/undefined, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
52+
runTest(`var x = 0;`, { module: ModuleKind.AMD }, /*fileName*/ undefined, /*moduleName*/undefined, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
5353
});
5454

5555
it("Uses correct newLine character", () => {
56-
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, /*moduleName*/undefined, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []);
56+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, /*fileName*/ undefined, /*moduleName*/undefined, `var x = 0;\n`, /*expectedDiagnosticCodes*/ []);
5757
});
5858

5959
it("Sets module name", () => {
@@ -66,7 +66,11 @@ var x = 0;`,
6666
` }\n` +
6767
` }\n` +
6868
`});\n`;
69-
runTest("var x = 1;", { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, "NamedModule", output)
69+
runTest("var x = 1;", { module: ModuleKind.System, newLine: NewLineKind.LineFeed }, /*fileName*/ undefined, "NamedModule", output)
7070
});
71+
it("No extra errors for file without extension", () => {
72+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, "file", /*moduleName*/undefined, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/[]);
73+
});
74+
7175
});
7276
}

0 commit comments

Comments
 (0)