Skip to content

Commit 9b12fab

Browse files
committed
Merge pull request #3274 from Microsoft/filterTranspileDiagnostics
Fix #3245: ensure transpile diagnostics only include syntactic and compiler options diagnostics
2 parents a10cd1e + 6902b05 commit 9b12fab

File tree

6 files changed

+101
-12
lines changed

6 files changed

+101
-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: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,8 @@ module ts {
17631763
* Extra compiler options that will unconditionally be used bu this function are:
17641764
* - isolatedModules = true
17651765
* - allowNonTsExtensions = true
1766+
* - noLib = true
1767+
* - noResolve = true
17661768
*/
17671769
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[]): string {
17681770
let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions();
@@ -1772,15 +1774,25 @@ module ts {
17721774
// Filename can be non-ts file.
17731775
options.allowNonTsExtensions = true;
17741776

1777+
// We are not returning a sourceFile for lib file when asked by the program,
1778+
// so pass --noLib to avoid reporting a file not found error.
1779+
options.noLib = true;
1780+
1781+
// We are not doing a full typecheck, we are not resolving the whole context,
1782+
// so pass --noResolve to avoid reporting missing file errors.
1783+
options.noResolve = true;
1784+
17751785
// Parse
1776-
var inputFileName = fileName || "module.ts";
1777-
var sourceFile = createSourceFile(inputFileName, input, options.target);
1786+
let inputFileName = fileName || "module.ts";
1787+
let sourceFile = createSourceFile(inputFileName, input, options.target);
17781788

17791789
// Store syntactic diagnostics
17801790
if (diagnostics && sourceFile.parseDiagnostics) {
17811791
diagnostics.push(...sourceFile.parseDiagnostics);
17821792
}
17831793

1794+
let newLine = getNewLineCharacter(options);
1795+
17841796
// Output
17851797
let outputText: string;
17861798

@@ -1795,13 +1807,13 @@ module ts {
17951807
useCaseSensitiveFileNames: () => false,
17961808
getCanonicalFileName: fileName => fileName,
17971809
getCurrentDirectory: () => "",
1798-
getNewLine: () => (sys && sys.newLine) || "\r\n"
1810+
getNewLine: () => newLine
17991811
};
18001812

18011813
var program = createProgram([inputFileName], options, compilerHost);
18021814

18031815
if (diagnostics) {
1804-
diagnostics.push(...program.getGlobalDiagnostics());
1816+
diagnostics.push(...program.getCompilerOptionsDiagnostics());
18051817
}
18061818

18071819
// 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+
for (let i = 0; i < expectedDiagnosticCodes.length; i++) {
11+
assert.equal(expectedDiagnosticCodes[i], diagnostics[i] && diagnostics[i].code, `Could not find expeced diagnostic.`);
12+
}
13+
assert.equal(diagnostics.length, expectedDiagnosticCodes.length, "Resuting diagnostics count does not match expected");
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, /*expectedDiagnosticCodes*/ [5047]);
23+
});
24+
25+
it("Generates no diagnostics with valid inputs", () => {
26+
// No errors
27+
runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
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, /*expectedDiagnosticCodes*/ []);
34+
});
35+
36+
it("Generates no diagnostics for missing module imports", () => {
37+
runTest(`import {a} from "module2";`,
38+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
39+
});
40+
41+
it("Generates expected syntactic diagnostics", () => {
42+
runTest(`a b`,
43+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ [1005]); /// 1005: ';' Expected
44+
});
45+
46+
it("Does not generate semantic diagnostics", () => {
47+
runTest(`var x: string = 0;`,
48+
{ module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, /*expectedDiagnosticCodes*/ []);
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`, /*expectedDiagnosticCodes*/ []);
57+
});
58+
});
59+
}

0 commit comments

Comments
 (0)