Skip to content

Commit 11fd061

Browse files
committed
Allow all options to be passed through to the TypeScript compiler
Fixes #48.
1 parent d13dc1c commit 11fd061

File tree

3 files changed

+182
-104
lines changed

3 files changed

+182
-104
lines changed

lib/main/tsconfig/tsconfig.js

Lines changed: 71 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,83 @@ var expand = require('glob-expand');
1111
var ts = require('typescript');
1212
var projectFileName = 'tsconfig.json';
1313
exports.defaults = {
14-
target: ts.ScriptTarget.ES5,
15-
module: ts.ModuleKind.CommonJS,
14+
target: 1 /* ES5 */,
15+
module: 1 /* CommonJS */,
1616
declaration: false,
1717
noImplicitAny: false,
1818
removeComments: true,
1919
noLib: false
2020
};
21-
function rawToTsCompilerOptions(raw) {
22-
var lower = {};
23-
Object.keys(raw).forEach(function (key) {
24-
lower[key.toLowerCase()] = raw[key];
25-
});
26-
var proper = {};
27-
proper.target = lower.target == 'es3' ? ts.ScriptTarget.ES3 : lower.target == 'es5' ? ts.ScriptTarget.ES5 : lower.target == 'es6' ? ts.ScriptTarget.ES6 : exports.defaults.target;
28-
proper.module = lower.module == 'none' ? ts.ModuleKind.None : lower.module == 'commonjs' ? ts.ModuleKind.CommonJS : lower.module == 'amd' ? ts.ModuleKind.AMD : exports.defaults.module;
29-
proper.declaration = lower.declaration == void 0 ? exports.defaults.declaration : lower.declaration;
30-
proper.noImplicitAny = lower.noimplicitany == void 0 ? exports.defaults.noImplicitAny : lower.noimplicitany;
31-
proper.removeComments = lower.removecomments == void 0 ? exports.defaults.removeComments : lower.removecomments;
32-
runWithDefault(function (val) { return proper.noLib = val; }, lower.nolib, exports.defaults.noLib);
33-
return proper;
21+
var deprecatedKeys = {
22+
outdir: 'outDir',
23+
noimplicitany: 'noImplicitAny',
24+
removecomments: 'removeComments',
25+
sourcemap: 'sourceMap',
26+
sourceroot: 'sourceRoot',
27+
maproot: 'mapRoot',
28+
nolib: 'noLib'
29+
};
30+
var typescriptEnumMap = {
31+
target: {
32+
'es3': 0 /* ES3 */,
33+
'es5': 1 /* ES5 */,
34+
'es6': 2 /* ES6 */,
35+
'latest': 2 /* Latest */
36+
},
37+
module: {
38+
'none': 0 /* None */,
39+
'commonjs': 1 /* CommonJS */,
40+
'amd': 2 /* AMD */
41+
}
42+
};
43+
var jsonEnumMap = {
44+
target: (function () {
45+
var map = {};
46+
map[0 /* ES3 */] = 'es3';
47+
map[1 /* ES5 */] = 'es5';
48+
map[2 /* ES6 */] = 'es6';
49+
map[2 /* Latest */] = 'latest';
50+
return map;
51+
})(),
52+
module: (function () {
53+
var map = {};
54+
map[0 /* None */] = 'none';
55+
map[1 /* CommonJS */] = 'commonjs';
56+
map[2 /* AMD */] = 'amd';
57+
return map;
58+
})()
59+
};
60+
function mixin(target, source) {
61+
for (var key in source) {
62+
target[key] = source[key];
63+
}
64+
return target;
3465
}
35-
function tsToRawCompilerOptions(proper) {
36-
var raw = {};
37-
var targetLookup = {};
38-
targetLookup[ts.ScriptTarget.ES3] = 'es3';
39-
targetLookup[ts.ScriptTarget.ES5] = 'es5';
40-
targetLookup[ts.ScriptTarget.ES6] = 'es6';
41-
var moduleLookup = {};
42-
moduleLookup[ts.ModuleKind.None] = 'none';
43-
moduleLookup[ts.ModuleKind.CommonJS] = 'commonjs';
44-
moduleLookup[ts.ModuleKind.AMD] = 'amd';
45-
runWithDefault(function (val) { return raw.target = val; }, targetLookup[proper.target]);
46-
runWithDefault(function (val) { return raw.module = val; }, moduleLookup[proper.module]);
47-
runWithDefault(function (val) { return raw.declaration = val; }, proper.declaration);
48-
runWithDefault(function (val) { return raw.noimplicitany = val; }, proper.noImplicitAny);
49-
runWithDefault(function (val) { return raw.removecomments = val; }, proper.removeComments);
50-
return raw;
66+
function rawToTsCompilerOptions(jsonOptions) {
67+
var compilerOptions = mixin({}, exports.defaults);
68+
for (var key in jsonOptions) {
69+
if (deprecatedKeys[key]) {
70+
atom.notifications.addWarning('Compiler option "' + key + '" is deprecated; use "' + deprecatedKeys[key] + '" instead');
71+
key = deprecatedKeys[key];
72+
}
73+
if (typescriptEnumMap[key]) {
74+
compilerOptions[key] = typescriptEnumMap[key][jsonOptions[key].toLowerCase()];
75+
}
76+
else {
77+
compilerOptions[key] = jsonOptions[key];
78+
}
79+
}
80+
return compilerOptions;
81+
}
82+
function tsToRawCompilerOptions(compilerOptions) {
83+
var jsonOptions = mixin({}, compilerOptions);
84+
if (compilerOptions.target !== undefined) {
85+
jsonOptions.target = jsonEnumMap.target[compilerOptions.target];
86+
}
87+
if (compilerOptions.module !== undefined) {
88+
jsonOptions.module = jsonEnumMap.module[compilerOptions.module];
89+
}
90+
return jsonOptions;
5191
}
5292
function getProjectSync(pathOrSrcFile) {
5393
if (!fs.existsSync(pathOrSrcFile))
@@ -172,16 +212,6 @@ function createMap(arr) {
172212
return result;
173213
}, {});
174214
}
175-
function runWithDefault(run, val, def) {
176-
if (val == void 0) {
177-
if (def != void 0) {
178-
run(def);
179-
}
180-
}
181-
else {
182-
run(val);
183-
}
184-
}
185215
function prettyJSON(object) {
186216
var cache = [];
187217
var value = JSON.stringify(object, function (key, value) {

lib/main/tsconfig/tsconfig.ts

Lines changed: 108 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
11
///ts:ref=globals
22
/// <reference path="../../globals.ts"/> ///ts:ref:generated
33

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
48
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
1010
outdir?: string; // Redirect output structure to this directory
11-
1211
noimplicitany?: boolean; // Error on inferred `any` type
1312
removecomments?: boolean; // Do not emit comments in output
14-
1513
sourcemap?: boolean; // Generates SourceMaps (.map files)
1614
sourceroot?: string; // Optionally specifies the location where debugger should locate TypeScript source files after deployment
1715
maproot?: string; // Optionally Specifies the location where debugger should locate map files after deployment
1816
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;
1944
}
2045

2146
interface TypeScriptProjectRawSpecification {
@@ -67,52 +92,88 @@ export var defaults: ts.CompilerOptions = {
6792
};
6893

6994
// 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;
94143
}
95144

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+
}
98153

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+
}
103161

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+
}
108164

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);
114167

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;
116177
}
117178

118179
/** 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 } {
295356
}, <{ [string: string]: boolean }>{});
296357
}
297358

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-
311359
function prettyJSON(object: any): string {
312360
var cache = [];
313361
var value = JSON.stringify(object,

lib/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"target": "es5",
44
"module": "commonjs",
55
"declaration": false,
6-
"noimplicitany": false,
7-
"removecomments": true
6+
"noImplicitAny": false,
7+
"removeComments": true
88
},
99
"filesGlob": [
1010
"./**/*.ts"
@@ -45,4 +45,4 @@
4545
"./worker/parent.ts",
4646
"./worker/workerProcess.ts"
4747
]
48-
}
48+
}

0 commit comments

Comments
 (0)