diff --git a/Jakefile.js b/Jakefile.js
index cb53b479502..f27e61062f0 100644
--- a/Jakefile.js
+++ b/Jakefile.js
@@ -128,7 +128,8 @@ var harnessSources = [
"services/preProcessFile.ts",
"services/patternMatcher.ts",
"versionCache.ts",
- "convertToBase64.ts"
+ "convertToBase64.ts",
+ "transpile.ts"
].map(function (f) {
return path.join(unittestsDirectory, f);
})).concat([
diff --git a/src/compiler/program.ts b/src/compiler/program.ts
index 32503de2566..4cccc881d40 100644
--- a/src/compiler/program.ts
+++ b/src/compiler/program.ts
@@ -10,9 +10,6 @@ module ts {
/** The version of the TypeScript compiler release */
export const version = "1.5.3";
- const carriageReturnLineFeed = "\r\n";
- const lineFeed = "\n";
-
export function findConfigFile(searchPath: string): string {
var fileName = "tsconfig.json";
while (true) {
@@ -94,10 +91,7 @@ module ts {
}
}
- let newLine =
- options.newLine === NewLineKind.CarriageReturnLineFeed ? carriageReturnLineFeed :
- options.newLine === NewLineKind.LineFeed ? lineFeed :
- sys.newLine;
+ const newLine = getNewLineCharacter(options);
return {
getSourceFile,
@@ -175,6 +169,7 @@ module ts {
getGlobalDiagnostics,
getSemanticDiagnostics,
getDeclarationDiagnostics,
+ getCompilerOptionsDiagnostics,
getTypeChecker,
getDiagnosticsProducingTypeChecker,
getCommonSourceDirectory: () => commonSourceDirectory,
@@ -291,6 +286,12 @@ module ts {
}
}
+ function getCompilerOptionsDiagnostics(): Diagnostic[]{
+ let allDiagnostics: Diagnostic[] = [];
+ addRange(allDiagnostics, diagnostics.getGlobalDiagnostics());
+ return sortAndDeduplicateDiagnostics(allDiagnostics);
+ }
+
function getGlobalDiagnostics(): Diagnostic[] {
let typeChecker = getDiagnosticsProducingTypeChecker();
diff --git a/src/compiler/types.ts b/src/compiler/types.ts
index a6fa89846bb..7eec44eb2f2 100644
--- a/src/compiler/types.ts
+++ b/src/compiler/types.ts
@@ -1064,6 +1064,7 @@ module ts {
getGlobalDiagnostics(): Diagnostic[];
getSemanticDiagnostics(sourceFile?: SourceFile): Diagnostic[];
getDeclarationDiagnostics(sourceFile?: SourceFile): Diagnostic[];
+ /* @internal */ getCompilerOptionsDiagnostics(): Diagnostic[];
/**
* Gets a type checker that can be used to semantically analyze source fils in the program.
diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts
index 3ded0a7cfd9..3bb459ff99e 100644
--- a/src/compiler/utilities.ts
+++ b/src/compiler/utilities.ts
@@ -1774,6 +1774,21 @@ module ts {
return result;
}
+
+ const carriageReturnLineFeed = "\r\n";
+ const lineFeed = "\n";
+ export function getNewLineCharacter(options: CompilerOptions): string {
+ if (options.newLine === NewLineKind.CarriageReturnLineFeed) {
+ return carriageReturnLineFeed;
+ }
+ else if (options.newLine === NewLineKind.LineFeed) {
+ return lineFeed;
+ }
+ else if (sys) {
+ return sys.newLine
+ }
+ return carriageReturnLineFeed;
+ }
}
module ts {
diff --git a/src/services/services.ts b/src/services/services.ts
index d5ba024cee5..ff1e627a241 100644
--- a/src/services/services.ts
+++ b/src/services/services.ts
@@ -1772,15 +1772,24 @@ module ts {
// Filename can be non-ts file.
options.allowNonTsExtensions = true;
+ // We are not returning a lib file when asked, so pass this flag to
+ // avoid reporting a file not found error
+ options.noLib = true;
+
+ // Similar to the library, we are not returning any refrenced files
+ options.noResolve = true;
+
// Parse
- var inputFileName = fileName || "module.ts";
- var sourceFile = createSourceFile(inputFileName, input, options.target);
+ let inputFileName = fileName || "module.ts";
+ let sourceFile = createSourceFile(inputFileName, input, options.target);
// Store syntactic diagnostics
if (diagnostics && sourceFile.parseDiagnostics) {
diagnostics.push(...sourceFile.parseDiagnostics);
}
+ let newLine = getNewLineCharacter(options);
+
// Output
let outputText: string;
@@ -1795,13 +1804,13 @@ module ts {
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: fileName => fileName,
getCurrentDirectory: () => "",
- getNewLine: () => (sys && sys.newLine) || "\r\n"
+ getNewLine: () => newLine
};
var program = createProgram([inputFileName], options, compilerHost);
if (diagnostics) {
- diagnostics.push(...program.getGlobalDiagnostics());
+ diagnostics.push(...program.getCompilerOptionsDiagnostics());
}
// Emit
diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts
new file mode 100644
index 00000000000..32caad8d05c
--- /dev/null
+++ b/tests/cases/unittests/transpile.ts
@@ -0,0 +1,59 @@
+///
+
+module ts {
+ describe("Transpile", () => {
+
+ function runTest(input: string, compilerOptions: ts.CompilerOptions = {}, expectedOutput?: string, expectedDiagnosticCodes: number[] = []): void {
+ let diagnostics: Diagnostic[] = [];
+ let result = transpile(input, compilerOptions, "file.ts", diagnostics);
+
+ assert.equal(diagnostics.length, expectedDiagnosticCodes.length);
+ for (let diagnostic of diagnostics) {
+ assert.isTrue(expectedDiagnosticCodes.indexOf(diagnostic.code) >= 0, `Found an unexpected diagnostic: ${ diagnostic.code }`);
+ }
+
+ if (expectedOutput !== undefined) {
+ assert.equal(result, expectedOutput);
+ }
+ }
+
+ it("Generates correct compilerOptions diagnostics", () => {
+ // Expecting 5047: "Option 'isolatedModules' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher."
+ runTest(`var x = 0;`, {}, /*expectedOutput*/ undefined, [5047]);
+ });
+
+ it("Generates no diagnostics with valid inputs", () => {
+ // No errors
+ runTest(`var x = 0;`, { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
+ });
+
+ it("Generates no diagnostics for missing file references", () => {
+ runTest(`///
+var x = 0;`,
+ { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
+ });
+
+ it("Generates no diagnostics for missing module imports", () => {
+ runTest(`import {a} from "module2";`,
+ { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
+ });
+
+ it("Generates expected syntactic diagnostics", () => {
+ runTest(`a b`,
+ { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, [1005]); /// 1005: ';' Expected
+ });
+
+ it("Does not generate semantic diagnostics", () => {
+ runTest(`var x: string = 0;`,
+ { module: ModuleKind.CommonJS }, /*expectedOutput*/ undefined, []);
+ });
+
+ it("Generates module output", () => {
+ runTest(`var x = 0;`, { module: ModuleKind.AMD }, `define(["require", "exports"], function (require, exports) {\r\n var x = 0;\r\n});\r\n`);
+ });
+
+ it("Uses correct newLine character", () => {
+ runTest(`var x = 0;`, { module: ModuleKind.CommonJS, newLine: NewLineKind.LineFeed }, `var x = 0;\n`);
+ });
+ });
+}