Skip to content

Commit e7eef83

Browse files
committed
Fix #3245: ensure transpile diagnostics only include syntactic and compiler options diagnostics
1 parent 711886e commit e7eef83

File tree

6 files changed

+98
-12
lines changed

6 files changed

+98
-12
lines changed

Jakefile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ var harnessSources = [
128128
"services/preProcessFile.ts",
129129
"services/patternMatcher.ts",
130130
"versionCache.ts",
131-
"convertToBase64.ts"
131+
"convertToBase64.ts",
132+
"transpile.ts"
132133
].map(function (f) {
133134
return path.join(unittestsDirectory, f);
134135
})).concat([

src/compiler/program.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ module ts {
1010
/** The version of the TypeScript compiler release */
1111
export const version = "1.5.3";
1212

13-
const carriageReturnLineFeed = "\r\n";
14-
const lineFeed = "\n";
15-
1613
export function findConfigFile(searchPath: string): string {
1714
var fileName = "tsconfig.json";
1815
while (true) {
@@ -94,10 +91,7 @@ module ts {
9491
}
9592
}
9693

97-
let newLine =
98-
options.newLine === NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
99-
options.newLine === NewLineKind.LineFeed ? lineFeed :
100-
sys.newLine;
94+
const newLine = getNewLineCharacter(options);
10195

10296
return {
10397
getSourceFile,
@@ -175,6 +169,7 @@ module ts {
175169
getGlobalDiagnostics,
176170
getSemanticDiagnostics,
177171
getDeclarationDiagnostics,
172+
getCompilerOptionsDiagnostics,
178173
getTypeChecker,
179174
getDiagnosticsProducingTypeChecker,
180175
getCommonSourceDirectory: () => commonSourceDirectory,
@@ -291,6 +286,12 @@ module ts {
291286
}
292287
}
293288

289+
function getCompilerOptionsDiagnostics(): Diagnostic[]{
290+
let allDiagnostics: Diagnostic[] = [];
291+
addRange(allDiagnostics, diagnostics.getGlobalDiagnostics());
292+
return sortAndDeduplicateDiagnostics(allDiagnostics);
293+
}
294+
294295
function getGlobalDiagnostics(): Diagnostic[] {
295296
let typeChecker = getDiagnosticsProducingTypeChecker();
296297

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,7 @@ module ts {
10641064
getGlobalDiagnostics(): Diagnostic[];
10651065
getSemanticDiagnostics(sourceFile?: SourceFile): Diagnostic[];
10661066
getDeclarationDiagnostics(sourceFile?: SourceFile): Diagnostic[];
1067+
/* @internal */ getCompilerOptionsDiagnostics(): Diagnostic[];
10671068

10681069
/**
10691070
* Gets a type checker that can be used to semantically analyze source fils in the program.

src/compiler/utilities.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,21 @@ module ts {
17741774

17751775
return result;
17761776
}
1777+
1778+
const carriageReturnLineFeed = "\r\n";
1779+
const lineFeed = "\n";
1780+
export function getNewLineCharacter(options: CompilerOptions): string {
1781+
if (options.newLine === NewLineKind.CarriageReturnLineFeed) {
1782+
return carriageReturnLineFeed;
1783+
}
1784+
else if (options.newLine === NewLineKind.LineFeed) {
1785+
return lineFeed;
1786+
}
1787+
else if (sys) {
1788+
return sys.newLine
1789+
}
1790+
return carriageReturnLineFeed;
1791+
}
17771792
}
17781793

17791794
module ts {

src/services/services.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1772,15 +1772,24 @@ module ts {
17721772
// Filename can be non-ts file.
17731773
options.allowNonTsExtensions = true;
17741774

1775+
// We are not returning a lib file when asked, so pass this flag to
1776+
// avoid reporting a file not found error
1777+
options.noLib = true;
1778+
1779+
// Similar to the library, we are not returning any refrenced files
1780+
options.noResolve = true;
1781+
17751782
// Parse
1776-
var inputFileName = fileName || "module.ts";
1777-
var sourceFile = createSourceFile(inputFileName, input, options.target);
1783+
let inputFileName = fileName || "module.ts";
1784+
let sourceFile = createSourceFile(inputFileName, input, options.target);
17781785

17791786
// Store syntactic diagnostics
17801787
if (diagnostics && sourceFile.parseDiagnostics) {
17811788
diagnostics.push(...sourceFile.parseDiagnostics);
17821789
}
17831790

1791+
let newLine = getNewLineCharacter(options);
1792+
17841793
// Output
17851794
let outputText: string;
17861795

@@ -1795,13 +1804,13 @@ module ts {
17951804
useCaseSensitiveFileNames: () => false,
17961805
getCanonicalFileName: fileName => fileName,
17971806
getCurrentDirectory: () => "",
1798-
getNewLine: () => (sys && sys.newLine) || "\r\n"
1807+
getNewLine: () => newLine
17991808
};
18001809

18011810
var program = createProgram([inputFileName], options, compilerHost);
18021811

18031812
if (diagnostics) {
1804-
diagnostics.push(...program.getGlobalDiagnostics());
1813+
diagnostics.push(...program.getCompilerOptionsDiagnostics());
18051814
}
18061815

18071816
// Emit

tests/cases/unittests/transpile.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/// <reference path="..\..\..\src\harness\harness.ts" />
2+
3+
module ts {
4+
describe("Transpile", () => {
5+
6+
function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
7+
let diagnostics: Diagnostic[] = [];
8+
let result = transpile(input, compilerOptions, "file.ts", diagnostics);
9+
10+
assert.equal(diagnostics.length, expectedDiagnosticCodes.length);
11+
for (let diagnostic of diagnostics) {
12+
assert.isTrue(expectedDiagnosticCodes.indexOf(diagnostic.code) >= 0, `Found an unexpected diagnostic: ${ diagnostic.code }`);
13+
}
14+
15+
if (expectedOutput !== undefined) {
16+
assert.equal(result, expectedOutput);
17+
}
18+
}
19+
20+
it("Generates correct compilerOptions diagnostics", () => {
21+
// 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;`, {}, /*expectedOutput*/ undefined, [5047]);
23+
});
24+
25+
it("Generates no diagnostics with valid inputs", () => {
26+
// No errors
27+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
28+
});
29+
30+
it("Generates no diagnostics for missing file references", () => {
31+
runTest(`/// <reference path="file2.ts" />
32+
var x = 0;`,
33+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
34+
});
35+
36+
it("Generates no diagnostics for missing module imports", () => {
37+
runTest(`import {a} from "module2";`,
38+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
39+
});
40+
41+
it("Generates expected syntactic diagnostics", () => {
42+
runTest(`a b`,
43+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, [1005]); /// 1005: ';' Expected
44+
});
45+
46+
it("Does not generate semantic diagnostics", () => {
47+
runTest(`var x: string = 0;`,
48+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
49+
});
50+
51+
it("Generates module output", () => {
52+
runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
53+
});
54+
55+
it("Uses correct newLine character", () => {
56+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`);
57+
});
58+
});
59+
}

0 commit comments

Comments
 (0)