From da3333e967a39aba6fffc7325f0a790f3ada1af1 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 14 Jun 2016 19:23:04 -0700 Subject: [PATCH 1/3] Clear out unused compiler options when transpiling --- src/services/services.ts | 10 +- tests/cases/unittests/transpile.ts | 223 +++++++++++++++++++++++++++-- 2 files changed, 219 insertions(+), 14 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index c20f95a56f2de..19167a5f6e00e 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2001,9 +2001,17 @@ namespace ts { // so pass --noLib to avoid reporting a file not found error. options.noLib = true; - // Clear out the lib and types option as well + // Clear out other settings that would not be participate in transpiling this module options.lib = undefined; options.types = undefined; + options.noEmit = undefined; + options.noEmitOnError = undefined; + options.paths = undefined; + options.rootDirs = undefined; + options.declaration = undefined; + options.declarationDir = undefined; + options.out = undefined; + options.outFile = undefined; // We are not doing a full typecheck, we are not resolving the whole context, // so pass --noResolve to avoid reporting missing file errors. diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index cac44994420ce..c8025bfc0a248 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -1,5 +1,9 @@ /// +interface ObjectConstructor { + assign(target: T, source: U): T & U; +} + namespace ts { describe("Transpile", () => { @@ -21,6 +25,9 @@ namespace ts { assert.equal(expectedDiagnosticTexts[i], diagnostics[i] && diagnostics[i].messageText); } }; + if (diagnostics.length !== n && diagnostics.length) { + console.log(JSON.stringify(diagnostics, undefined, 2)); + } assert.equal(diagnostics.length, n, "Resuting diagnostics count does not match expected"); } @@ -89,6 +96,19 @@ namespace ts { } + function testCompilerOption(options: CompilerOptions, input?: string, output?: string): void { + input = input || "x = 0;"; + output = output || `"use strict";\r\nx = 0;\r\n`; + test(input, { + expectedOutput: output, + options: { + compilerOptions: Object.assign({ module: ModuleKind.CommonJS, newLine: NewLineKind.CarriageReturnLineFeed }, options), + fileName: "input.js", + reportDiagnostics: true + } + }); + } + it("Generates no diagnostics with valid inputs", () => { // No errors test(`var x = 0;`, { options: { compilerOptions: { module: ModuleKind.CommonJS } } }); @@ -304,21 +324,198 @@ var x = 0;`, test("var x", { expectedOutput: `"use strict";\r\nvar x;\r\n`, options: { fileName: "http://somewhere/directory//directory2/file.ts" } }); }); - it("Support options with lib values", () => { - const input = "const a = 10;"; - const output = `"use strict";\r\nvar a = 10;\r\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { lib: ["es6", "dom"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + describe("Works with all compiler options", () => { + + it("Supports setting 'allowJs'", () => { + testCompilerOption({ allowJs: true }); }); - }); - it("Support options with types values", () => { - const input = "const a = 10;"; - const output = `"use strict";\r\nvar a = 10;\r\n`; - test(input, { - expectedOutput: output, - options: { compilerOptions: { types: ["jquery", "typescript"], module: ModuleKind.CommonJS }, fileName: "input.js", reportDiagnostics: true } + it("Supports setting 'allowSyntheticDefaultImports'", () => { + testCompilerOption({ allowSyntheticDefaultImports: true }); + }); + + it("Supports setting 'allowUnreachableCode'", () => { + testCompilerOption({ allowUnreachableCode: true }); + }); + + it("Supports setting 'allowUnusedLabels'", () => { + testCompilerOption({ allowUnusedLabels: true }); + }); + + it("Supports setting 'baseUrl'", () => { + testCompilerOption({ baseUrl: "./folder/baseUrl" }); + }); + + it("Supports setting 'charset'", () => { + testCompilerOption({ charset: "en-us" }); + }); + + it("Supports setting 'declaration'", () => { + testCompilerOption({ declaration: true }); + }); + + it("Supports setting 'declarationDir'", () => { + testCompilerOption({ declarationDir: "out/declarations" }); + }); + + it("Supports setting 'emitBOM'", () => { + testCompilerOption({ emitBOM: true }); + }); + + it("Supports setting 'emitDecoratorMetadata'", () => { + testCompilerOption({ emitDecoratorMetadata: true, experimentalDecorators: true }); + }); + + it("Supports setting 'experimentalDecorators'", () => { + testCompilerOption({ experimentalDecorators: true }); + }); + + it("Supports setting 'forceConsistentCasingInFileNames'", () => { + testCompilerOption({ forceConsistentCasingInFileNames: true }); + }); + + it("Supports setting 'isolatedModules'", () => { + testCompilerOption({ isolatedModules: true }); + }); + + it("Supports setting 'jsx'", () => { + testCompilerOption({ jsx: 1 }); + }); + + it("Supports setting 'lib'", () => { + testCompilerOption({ lib: ["es2015", "dom"] }); + }); + + it("Supports setting 'locale'", () => { + testCompilerOption({ locale: "en-us" }); + }); + + it("Supports setting 'module'", () => { + testCompilerOption({ module: 1 }); + }); + + it("Supports setting 'moduleResolution'", () => { + testCompilerOption({ moduleResolution: 2 }); + }); + + it("Supports setting 'newLine'", () => { + testCompilerOption({ newLine: 0 }); + }); + + it("Supports setting 'noEmit'", () => { + testCompilerOption({ noEmit: true }); + }); + + it("Supports setting 'noEmitHelpers'", () => { + testCompilerOption({ noEmitHelpers: true }); + }); + + it("Supports setting 'noEmitOnError'", () => { + testCompilerOption({ noEmitOnError: true }); + }); + + it("Supports setting 'noErrorTruncation'", () => { + testCompilerOption({ noErrorTruncation: true }); + }); + + it("Supports setting 'noFallthroughCasesInSwitch'", () => { + testCompilerOption({ noFallthroughCasesInSwitch: true }); + }); + + it("Supports setting 'noImplicitAny'", () => { + testCompilerOption({ noImplicitAny: true }); + }); + + it("Supports setting 'noImplicitReturns'", () => { + testCompilerOption({ noImplicitReturns: true }); + }); + + it("Supports setting 'noImplicitThis'", () => { + testCompilerOption({ noImplicitThis: true }); + }); + + it("Supports setting 'noImplicitUseStrict'", () => { + testCompilerOption({ noImplicitUseStrict: true }, "x;", "x;\r\n"); + }); + + it("Supports setting 'noLib'", () => { + testCompilerOption({ noLib: true }); + }); + + it("Supports setting 'noResolve'", () => { + testCompilerOption({ noResolve: true }); + }); + + it("Supports setting 'out'", () => { + testCompilerOption({ out: "./out" }); + }); + + it("Supports setting 'outDir'", () => { + testCompilerOption({ outDir: "./outDir" }); + }); + + it("Supports setting 'outFile'", () => { + testCompilerOption({ outFile: "./outFile" }); + }); + + it("Supports setting 'paths'", () => { + testCompilerOption({ paths: { "*": ["./generated*"] } }); + }); + + it("Supports setting 'preserveConstEnums'", () => { + testCompilerOption({ preserveConstEnums: true }); + }); + + it("Supports setting 'reactNamespace'", () => { + testCompilerOption({ reactNamespace: "react" }); + }); + + it("Supports setting 'removeComments'", () => { + testCompilerOption({ removeComments: true }); + }); + + it("Supports setting 'rootDir'", () => { + testCompilerOption({ rootDir: "./rootDir" }); + }); + + it("Supports setting 'rootDirs'", () => { + testCompilerOption({ rootDirs: ["./a", "./b"] }); + }); + + it("Supports setting 'skipLibCheck'", () => { + testCompilerOption({ skipLibCheck: true }); + }); + + it("Supports setting 'skipDefaultLibCheck'", () => { + testCompilerOption({ skipDefaultLibCheck: true }); + }); + + it("Supports setting 'strictNullChecks'", () => { + testCompilerOption({ strictNullChecks: true }); + }); + + it("Supports setting 'stripInternal'", () => { + testCompilerOption({ stripInternal: true }); + }); + + it("Supports setting 'suppressExcessPropertyErrors'", () => { + testCompilerOption({ suppressExcessPropertyErrors: true }); + }); + + it("Supports setting 'suppressImplicitAnyIndexErrors'", () => { + testCompilerOption({ suppressImplicitAnyIndexErrors: true }); + }); + + it("Supports setting 'target'", () => { + testCompilerOption({ target: 2 }); + }); + + it("Supports setting 'types'", () => { + testCompilerOption({ types: ["jquery", "jasmine"] }); + }); + + it("Supports setting 'typeRoots'", () => { + testCompilerOption({ typeRoots: ["./folder"] }); }); }); From 95ddfc7efc788c14900dc20da03f2b805e495854 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 15 Jun 2016 11:38:28 -0700 Subject: [PATCH 2/3] Do not use Object.assing in test --- tests/cases/unittests/transpile.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/cases/unittests/transpile.ts b/tests/cases/unittests/transpile.ts index c8025bfc0a248..6c88c2558fc6c 100644 --- a/tests/cases/unittests/transpile.ts +++ b/tests/cases/unittests/transpile.ts @@ -1,9 +1,5 @@ /// -interface ObjectConstructor { - assign(target: T, source: U): T & U; -} - namespace ts { describe("Transpile", () => { @@ -102,7 +98,7 @@ namespace ts { test(input, { expectedOutput: output, options: { - compilerOptions: Object.assign({ module: ModuleKind.CommonJS, newLine: NewLineKind.CarriageReturnLineFeed }, options), + compilerOptions: extend(options, { module: ModuleKind.CommonJS, newLine: NewLineKind.CarriageReturnLineFeed }), fileName: "input.js", reportDiagnostics: true } From ce45ee797c919b914ce65f4e5ef1065696d04662 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 15 Jun 2016 12:23:00 -0700 Subject: [PATCH 3/3] Fix comment --- src/services/services.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/services/services.ts b/src/services/services.ts index 19167a5f6e00e..62a50db65ef5b 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -2001,7 +2001,7 @@ namespace ts { // so pass --noLib to avoid reporting a file not found error. options.noLib = true; - // Clear out other settings that would not be participate in transpiling this module + // Clear out other settings that would not be used in transpiling this module options.lib = undefined; options.types = undefined; options.noEmit = undefined;