|
1 | 1 | ///ts:ref=globals |
2 | 2 | /// <reference path="../../globals.ts"/> ///ts:ref:generated |
3 | 3 |
|
| 4 | +// Most compiler options come from require('typescript').CompilerOptions, but |
| 5 | +// 'module' and 'target' cannot use the same enum as that interface since we |
| 6 | +// do not want to force users to put magic numbers in their tsconfig files |
| 7 | +// TODO: Use require('typescript').parseConfigFile when TS1.5 is released |
4 | 8 | interface CompilerOptions { |
5 | | - target?: string; // 'es3'|'es5' (default) | 'es6' |
6 | | - module?: string; // 'amd'|'commonjs' (default) |
7 | | - |
8 | | - declaration?: boolean; // Generates corresponding `.d.ts` file |
9 | | - out?: string; // Concatenate and emit a single file |
| 9 | + // Backwards compatibility for 0.27.0 and earlier |
10 | 10 | outdir?: string; // Redirect output structure to this directory |
11 | | - |
12 | 11 | noimplicitany?: boolean; // Error on inferred `any` type |
13 | 12 | removecomments?: boolean; // Do not emit comments in output |
14 | | - |
15 | 13 | sourcemap?: boolean; // Generates SourceMaps (.map files) |
16 | 14 | sourceroot?: string; // Optionally specifies the location where debugger should locate TypeScript source files after deployment |
17 | 15 | maproot?: string; // Optionally Specifies the location where debugger should locate map files after deployment |
18 | 16 | nolib?: boolean; |
| 17 | + |
| 18 | + allowNonTsExtensions?: boolean; |
| 19 | + charset?: string; |
| 20 | + codepage?: number; |
| 21 | + declaration?: boolean; |
| 22 | + diagnostics?: boolean; |
| 23 | + emitBOM?: boolean; |
| 24 | + help?: boolean; |
| 25 | + locale?: string; |
| 26 | + mapRoot?: string; |
| 27 | + module?: string; //'amd'|'commonjs' (default) |
| 28 | + noEmitOnError?: boolean; |
| 29 | + noErrorTruncation?: boolean; |
| 30 | + noImplicitAny?: boolean; |
| 31 | + noLib?: boolean; |
| 32 | + noLibCheck?: boolean; |
| 33 | + noResolve?: boolean; |
| 34 | + out?: string; |
| 35 | + outDir?: string; |
| 36 | + preserveConstEnums?: boolean; |
| 37 | + removeComments?: boolean; |
| 38 | + sourceMap?: boolean; |
| 39 | + sourceRoot?: string; |
| 40 | + suppressImplicitAnyIndexErrors?: boolean; |
| 41 | + target?: string; // 'es3'|'es5' (default)|'es6' |
| 42 | + version?: boolean; |
| 43 | + watch?: boolean; |
19 | 44 | } |
20 | 45 |
|
21 | 46 | interface TypeScriptProjectRawSpecification { |
@@ -67,52 +92,88 @@ export var defaults: ts.CompilerOptions = { |
67 | 92 | }; |
68 | 93 |
|
69 | 94 | // TODO: add validation and add all options |
70 | | -function rawToTsCompilerOptions(raw: CompilerOptions): ts.CompilerOptions { |
71 | | - // Convert everything inside compilerOptions to lowerCase |
72 | | - var lower: CompilerOptions = {}; |
73 | | - Object.keys(raw).forEach((key: string) => { |
74 | | - lower[key.toLowerCase()] = raw[key]; |
75 | | - }); |
76 | | - |
77 | | - // Change to enums |
78 | | - var proper: ts.CompilerOptions = {}; |
79 | | - proper.target = |
80 | | - lower.target == 'es3' ? ts.ScriptTarget.ES3 |
81 | | - : lower.target == 'es5' ? ts.ScriptTarget.ES5 |
82 | | - : lower.target == 'es6' ? ts.ScriptTarget.ES6 |
83 | | - : defaults.target; |
84 | | - proper.module = |
85 | | - lower.module == 'none' ? ts.ModuleKind.None |
86 | | - : lower.module == 'commonjs' ? ts.ModuleKind.CommonJS |
87 | | - : lower.module == 'amd' ? ts.ModuleKind.AMD |
88 | | - : defaults.module; |
89 | | - proper.declaration = lower.declaration == void 0 ? defaults.declaration : lower.declaration; |
90 | | - proper.noImplicitAny = lower.noimplicitany == void 0 ? defaults.noImplicitAny : lower.noimplicitany; |
91 | | - proper.removeComments = lower.removecomments == void 0 ? defaults.removeComments : lower.removecomments; |
92 | | - runWithDefault((val) => proper.noLib = val, lower.nolib, defaults.noLib); |
93 | | - return proper; |
| 95 | + |
| 96 | +var deprecatedKeys = { |
| 97 | + outdir: 'outDir', |
| 98 | + noimplicitany: 'noImplicitAny', |
| 99 | + removecomments: 'removeComments', |
| 100 | + sourcemap: 'sourceMap', |
| 101 | + sourceroot: 'sourceRoot', |
| 102 | + maproot: 'mapRoot', |
| 103 | + nolib: 'noLib' |
| 104 | +}; |
| 105 | + |
| 106 | +var typescriptEnumMap = { |
| 107 | + target: { |
| 108 | + 'es3': ts.ScriptTarget.ES3, |
| 109 | + 'es5': ts.ScriptTarget.ES5, |
| 110 | + 'es6': ts.ScriptTarget.ES6, |
| 111 | + 'latest': ts.ScriptTarget.Latest |
| 112 | + }, |
| 113 | + module: { |
| 114 | + 'none': ts.ModuleKind.None, |
| 115 | + 'commonjs': ts.ModuleKind.CommonJS, |
| 116 | + 'amd': ts.ModuleKind.AMD |
| 117 | + } |
| 118 | +}; |
| 119 | + |
| 120 | +var jsonEnumMap = { |
| 121 | + target: (function () { |
| 122 | + var map: { [key: number]: string; } = {}; |
| 123 | + map[ts.ScriptTarget.ES3] = 'es3'; |
| 124 | + map[ts.ScriptTarget.ES5] = 'es5'; |
| 125 | + map[ts.ScriptTarget.ES6] = 'es6'; |
| 126 | + map[ts.ScriptTarget.Latest] = 'latest'; |
| 127 | + return map; |
| 128 | + })(), |
| 129 | + module: (function () { |
| 130 | + var map: { [key: number]: string; } = {}; |
| 131 | + map[ts.ModuleKind.None] = 'none'; |
| 132 | + map[ts.ModuleKind.CommonJS] = 'commonjs'; |
| 133 | + map[ts.ModuleKind.AMD] = 'amd'; |
| 134 | + return map; |
| 135 | + })() |
| 136 | +}; |
| 137 | + |
| 138 | +function mixin(target: any, source: any): any { |
| 139 | + for (var key in source) { |
| 140 | + target[key] = source[key]; |
| 141 | + } |
| 142 | + return target; |
94 | 143 | } |
95 | 144 |
|
96 | | -function tsToRawCompilerOptions(proper: ts.CompilerOptions): CompilerOptions { |
97 | | - var raw: CompilerOptions = {}; |
| 145 | +function rawToTsCompilerOptions(jsonOptions: CompilerOptions): ts.CompilerOptions { |
| 146 | + // Cannot use Object.create because the compiler checks hasOwnProperty |
| 147 | + var compilerOptions = <ts.CompilerOptions> mixin({}, defaults); |
| 148 | + for (var key in jsonOptions) { |
| 149 | + if (deprecatedKeys[key]) { |
| 150 | + atom.notifications.addWarning('Compiler option "' + key + '" is deprecated; use "' + deprecatedKeys[key] + '" instead'); |
| 151 | + key = deprecatedKeys[key]; |
| 152 | + } |
98 | 153 |
|
99 | | - var targetLookup = {}; |
100 | | - targetLookup[ts.ScriptTarget.ES3] = 'es3'; |
101 | | - targetLookup[ts.ScriptTarget.ES5] = 'es5'; |
102 | | - targetLookup[ts.ScriptTarget.ES6] = 'es6'; |
| 154 | + if (typescriptEnumMap[key]) { |
| 155 | + compilerOptions[key] = typescriptEnumMap[key][jsonOptions[key].toLowerCase()]; |
| 156 | + } |
| 157 | + else { |
| 158 | + compilerOptions[key] = jsonOptions[key]; |
| 159 | + } |
| 160 | + } |
103 | 161 |
|
104 | | - var moduleLookup = {}; |
105 | | - moduleLookup[ts.ModuleKind.None] = 'none'; |
106 | | - moduleLookup[ts.ModuleKind.CommonJS] = 'commonjs'; |
107 | | - moduleLookup[ts.ModuleKind.AMD] = 'amd'; |
| 162 | + return compilerOptions; |
| 163 | +} |
108 | 164 |
|
109 | | - runWithDefault((val) => raw.target = val, targetLookup[proper.target]); |
110 | | - runWithDefault((val) => raw.module = val, moduleLookup[proper.module]); |
111 | | - runWithDefault((val) => raw.declaration = val, proper.declaration); |
112 | | - runWithDefault((val) => raw.noimplicitany = val, proper.noImplicitAny); |
113 | | - runWithDefault((val) => raw.removecomments = val, proper.removeComments); |
| 165 | +function tsToRawCompilerOptions(compilerOptions: ts.CompilerOptions): CompilerOptions { |
| 166 | + var jsonOptions = <CompilerOptions> mixin({}, compilerOptions); |
114 | 167 |
|
115 | | - return raw; |
| 168 | + if (compilerOptions.target !== undefined) { |
| 169 | + jsonOptions.target = jsonEnumMap.target[compilerOptions.target]; |
| 170 | + } |
| 171 | + |
| 172 | + if (compilerOptions.module !== undefined) { |
| 173 | + jsonOptions.module = jsonEnumMap.module[compilerOptions.module]; |
| 174 | + } |
| 175 | + |
| 176 | + return jsonOptions; |
116 | 177 | } |
117 | 178 |
|
118 | 179 | /** Given an src (source file or directory) goes up the directory tree to find the project specifications. |
@@ -295,19 +356,6 @@ function createMap(arr: string[]): { [string: string]: boolean } { |
295 | 356 | }, <{ [string: string]: boolean }>{}); |
296 | 357 | } |
297 | 358 |
|
298 | | -/** if ( no val given && default given then run with default ) else ( run with val ) */ |
299 | | -function runWithDefault<T>(run: (val: T) => any, val: T, def?: T) { |
300 | | - // no val |
301 | | - if (val == void 0) { |
302 | | - if (def != void 0) { |
303 | | - run(def); |
304 | | - } |
305 | | - } |
306 | | - else { |
307 | | - run(val); |
308 | | - } |
309 | | -} |
310 | | - |
311 | 359 | function prettyJSON(object: any): string { |
312 | 360 | var cache = []; |
313 | 361 | var value = JSON.stringify(object, |
|
0 commit comments