Skip to content

Commit 71f113e

Browse files
authored
Merge pull request #377 from zuohaocheng/master
Support some options from TypeScript 2.0
2 parents 96185a3 + 91df497 commit 71f113e

14 files changed

+197
-3
lines changed

Gruntfile.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -696,6 +696,16 @@ module.exports = function (grunt) {
696696
noImplicitUseStrict: true
697697
}
698698
},
699+
new_TypeScript_2_0_Features: {
700+
test: true,
701+
testExecute: commandLineAssertions.new_TypeScript_2_0_Features,
702+
src: 'test/simple/ts/**/*.ts',
703+
options: {
704+
strictNullChecks: true,
705+
noImplicitThis: true,
706+
lib: ['es2017']
707+
}
708+
},
699709
allowJs_CompileWorks: {
700710
test: true,
701711
src: ['test/allowJs/allowJsConsumer.ts',

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Grunt-ts provides explicit support for most `tsc` switches. Any arbitrary switc
7474
|--inlineSources|[inlineSources](#inlinesources)|Emit the TypeScript source alongside the sourcemaps within a single file; requires `--inlineSourceMap` to be set.|
7575
|--isolatedModules|[isolatedModules](#isolatedmodules)|Ensures that the output is safe to only emit single files by making cases that break single-file transpilation an error|
7676
|--jsx|[jsx](#jsx)|Specifies the JSX code generation style: 'preserve' or 'react'|
77+
|--lib|[lib](#lib)|List of library files to be included in the compilation.|
7778
|--locale|[locale](#locale)|Specify locale for error messages.|
7879
|--mapRoot LOCATION|[mapRoot](#maproot)|Specifies the location where debugger should locate map files instead of generated locations.|
7980
|--module KIND|[module](#module)|Specify module style for code generation|
@@ -86,6 +87,7 @@ Grunt-ts provides explicit support for most `tsc` switches. Any arbitrary switc
8687
|--noImplicitAny|[noImplicitAny](#noimplicitany)|Warn on expressions and declarations with an implied `any` type.|
8788
|--noImplicitUseStrict|[noImplicitUseStrict](#noimplicitusestrict)|Warn on expressions and declarations with an implied `any` type.|
8889
|--noImplicitReturns|[noImplicitReturns](#noimplicitreturns)|Report error when not all code paths in function return a value.|
90+
|--noImplicitThis|[noImplicitThis](#noImplicitThis)|Raise error on `this` expressions with an implied `any` type.|
8991
|--noLib|[noLib](#nolib)|Do not automatically include lib.d.ts is compilation context.|
9092
|--noResolve|[noResolve](#noresolve)|Do not add triple-slash references or module import targets to the compilation context.|
9193
|--out FILE|[out](#out)|Concatenate and emit output to a single file.|
@@ -96,6 +98,7 @@ Grunt-ts provides explicit support for most `tsc` switches. Any arbitrary switc
9698
|--removeComments|[removeComments](#removecomments)|Configures if comments should be included in the output|
9799
|--rootDir|[rootDir](#rootdir)|Allows override of common root folder calculated by `--outDir`.|
98100
|--skipDefaultLibCheck|[skipDefaultLibCheck](#skipdefaultlibcheck)|Don't check a user-defined default lib file's validity.|
101+
|--strictNullChecks|[strictNullChecks](#strictNullChecks)|Enables strict null checking mode.|
99102
|--sourceMap|[sourceMap](#sourcemap)|Generates corresponding `.map` file|
100103
|--sourceRoot LOCATION|[sourceRoot](#sourceroot)|Specifies the location where debugger should locate TypeScript files instead of source locations.|
101104
|--stripInternal|[stripInternal](#stripinternal)|does not emit members marked as @internal.|
@@ -137,6 +140,7 @@ For file ordering, look at [JavaScript Generation](#javascript-generation).
137140
|[inlineSources](#inlinesources)|option|`true`, `false` (default) Emit the TypeScript source alongside the sourcemaps within a single file; If enabled, will automatically enable `inlineSourceMap` and `sourceMap`.|
138141
|[isolatedModules](#isolatedmodules)|option|`true`, `false` (default) Ensures that the output is safe to only emit single files by making cases that break single-file transpilation an error.|
139142
|[jsx](#jsx)|option|`'preserve'`, `'react'`, (TypeScript default is `'react'`). If `'preserve'`, TypeScript will emit `.jsx`; if `'react'`, TypeScript will transpile and emit `.js` files.|
143+
|[lib](#lib)|option|`string[]`. List of library files to be included in the compilation. If `--lib` is not specified a default library is injected.|
140144
|[locale](#locale)|option|`string` - specify locale for error messages|
141145
|[mapRoot](#maproot)|option|`string` - root for referencing `.js.map` files in JS|
142146
|[module](#module)|option|default is none (`''`), but can be set to `'amd'`, `'commonjs'`, `'system'`, or other values.|
@@ -148,6 +152,7 @@ For file ordering, look at [JavaScript Generation](#javascript-generation).
148152
|[noFallthroughCasesInSwitch](#nofallthroughcasesinswitch)|option|`true`, `false` (default) - Report errors for fallthrough cases in switch statement.|
149153
|[noImplicitAny](#noimplicitany)|option|`true`, `false` (default) - enable for stricter type checking|
150154
|[noImplicitReturns](#noimplicitreturns)|option|`true`, `false` (default) - Report error when not all code paths in function return a value.|
155+
|[noImplicitThis](#noImplicitThis)|option|`true`, `false` (default) - Raise error on this expressions with an implied `any` type.|
151156
|[noLib](#nolib)|option|`true`, `false` (default) - do not automatically include lib.d.ts in compilation context|
152157
|[noResolve](#noresolve)|option|`true`, `false` (default) - for deprecated version of TypeScript|
153158
|[options](#grunt-ts-target-options)|target||
@@ -162,6 +167,7 @@ For file ordering, look at [JavaScript Generation](#javascript-generation).
162167
|[skipDefaultLibCheck](#skipdefaultlibcheck)|option|`true`, `false` (default) - Don't check a user-defined default lib file's validity.|
163168
|[sourceRoot](#sourceroot)|option|`string` - root for referencing TS files in `.js.map`|
164169
|[sourceMap](#sourcemap)|option|`true` (default), `false` - indicates if source maps should be generated (`.js.map`)|
170+
|[strictNullChecks](#strictNullChecks)|option|`true`, `false` (default) - Enables strict null checking mode.|
165171
|[stripInternal](#stripinternal)|option|`true`, `false` (default) - does not emit members marked as @internal.|
166172
|[suppressExcessPropertyErrors](#suppressexcesspropertyerrors)|option|`false` (default), `true` - indicates if TypeScript should disable strict object literal assignment checking (experimental)|
167173
|[suppressImplicitAnyIndexErrors](#suppressimplicitanyindexerrors)|option|`false` (default), `true` - indicates if TypeScript should allow access to properties of an object by string indexer when `--noImplicitAny` is active, even if TypeScript doesn't know about them.|
@@ -941,6 +947,20 @@ grunt.initConfig({
941947
});
942948
````
943949

950+
#### lib
951+
952+
List of library files to be included in the compilation. If `--lib` is not specified a default library is injected.
953+
954+
````javascript
955+
grunt.initConfig({
956+
ts: {
957+
options: {
958+
lib: ['es2015']
959+
}
960+
}
961+
});
962+
````
963+
944964
#### locale
945965

946966
Specify culture string for error messages - will pass the `--locale` switch. Requires appropriate TypeScript error messages file to be present (see TypeScript documentation for more details).
@@ -1158,6 +1178,26 @@ grunt.initConfig({
11581178
});
11591179
````
11601180

1181+
#### noImplicitThis
1182+
1183+
````javascript
1184+
true | false (default)
1185+
````
1186+
1187+
Set to true to pass `--noImplicitThis` to the compiler. Requires more strict type checking. Raise error on `this` expressions with an implied `any` type.
1188+
1189+
````javascript
1190+
grunt.initConfig({
1191+
ts: {
1192+
default: {
1193+
options: {
1194+
noImplicitThis: true
1195+
}
1196+
}
1197+
}
1198+
});
1199+
````
1200+
11611201
#### noLib
11621202

11631203
````javascript
@@ -1310,6 +1350,26 @@ grunt.initConfig({
13101350
});
13111351
````
13121352

1353+
#### strictNullChecks
1354+
1355+
````javascript
1356+
true | false (default)
1357+
````
1358+
1359+
In strict null checking mode, the `null` and `undefined` values are not in the domain of every type and are only assignable to themselves and `any` (the one exception being that `undefined` is also assignable to `void`).
1360+
1361+
````javascript
1362+
grunt.initConfig({
1363+
ts: {
1364+
default: {
1365+
options: {
1366+
strictNullChecks: true
1367+
}
1368+
}
1369+
}
1370+
});
1371+
````
1372+
13131373
#### sourceMap
13141374

13151375
````javascript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"peerDependencies": {
5252
"grunt": ">=0.4.0",
53-
"typescript": "^1.8.0 || 2.x.x-dev.0"
53+
"typescript": ">=1.8.0"
5454
},
5555
"devDependencies": {
5656
"grunt": "~0.4.0",

tasks/modules/compile.js

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/compile.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,44 @@ export function compileAllFiles(options: IGruntTSOptions, compilationInfo: IGrun
287287
if (options.noImplicitUseStrict) {
288288
args.push('--noImplicitUseStrict');
289289
}
290+
if (options.strictNullChecks) {
291+
args.push('--strictNullChecks');
292+
}
293+
if (options.noImplicitThis) {
294+
args.push('--noImplicitThis');
295+
}
296+
if (options.lib) {
297+
let possibleOptions = [
298+
'es5',
299+
'es6',
300+
'es2015',
301+
'es7',
302+
'es2016',
303+
'es2017',
304+
'dom',
305+
'webworker',
306+
'scripthost',
307+
'es2015.core',
308+
'es2015.collection',
309+
'es2015.generator',
310+
'es2015.iterable',
311+
'es2015.promise',
312+
'es2015.proxy',
313+
'es2015.reflect',
314+
'es2015.symbol',
315+
'es2015.symbol.wellknown',
316+
'es2016.array.include',
317+
'es2017.object',
318+
'es2017.sharedmemory'
319+
];
320+
321+
options.lib.forEach(function(option) {
322+
if (possibleOptions.indexOf(option) === -1) {
323+
console.warn(`WARNING: Option "lib" does not support ${option} `.magenta);
324+
}
325+
});
326+
args.push('--lib', options.lib.join(','));
327+
}
290328

291329
args.push('--target', options.target.toUpperCase());
292330

tasks/modules/defaults.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/defaults.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ const TypeScriptDefaults: IGruntTSOptions = {
6363
forceConsistentCasingInFileNames: false,
6464
allowJs: false,
6565
noImplicitUseStrict: false,
66+
strictNullChecks: false,
67+
noImplicitThis: false,
68+
lib: null,
6669
rootDir: null,
6770
warnings: [],
6871
errors: []

tasks/modules/interfaces.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,12 @@ interface ITaskOptions {
115115
allowJs: boolean;
116116
/** Do not emit "use strict" directives in module output. */
117117
noImplicitUseStrict: boolean;
118+
/** Enables strict null checking mode */
119+
strictNullChecks: boolean;
120+
/** Raise error on this expressions with an implied 'any' type. */
121+
noImplicitThis: boolean;
122+
/** List of library files to be included in the compilation */
123+
lib: Array<string>;
118124
}
119125

120126
interface IVisualStudioProjectSupport {

tasks/modules/optionsResolver.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/optionsResolver.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const propertiesFromTarget = ['amdloader', 'baseDir', 'html', 'htmlOutDir', 'htm
2424
'preserveConstEnums', 'removeComments', 'sourceRoot', 'sourceMap', 'stripInternal', 'suppressExcessPropertyErrors',
2525
'suppressImplicitAnyIndexErrors', 'target', 'verbose', 'jsx', 'moduleResolution', 'experimentalAsyncFunctions', 'rootDir',
2626
'emitGruntEvents', 'reactNamespace', 'skipDefaultLibCheck', 'pretty', 'allowUnusedLabels', 'noImplicitReturns',
27-
'noFallthroughCasesInSwitch', 'allowUnreachableCode', 'forceConsistentCasingInFileNames', 'allowJs', 'noImplicitUseStrict'],
27+
'noFallthroughCasesInSwitch', 'allowUnreachableCode', 'forceConsistentCasingInFileNames', 'allowJs', 'noImplicitUseStrict',
28+
'strictNullChecks', 'noImplicitThis', 'lib'],
2829
delayTemplateExpansion = ['htmlModuleTemplate', 'htmlVarTemplate', 'htmlOutputTemplate'];
2930

3031
let templateProcessor: (templateString: string, options: any) => string = null;

tasks/modules/tsconfig.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tasks/modules/tsconfig.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ function applyCompilerOptions(applyTo: IGruntTSOptions, projectSpec: ITSConfigFi
241241
'inlineSources',
242242
'jsx',
243243
// we do not support listFiles.
244+
'lib',
244245
'locale',
245246
'mapRoot',
246247
'module',
@@ -252,6 +253,7 @@ function applyCompilerOptions(applyTo: IGruntTSOptions, projectSpec: ITSConfigFi
252253
'noFallthroughCasesInSwitch',
253254
'noImplicitAny',
254255
'noImplicitReturns',
256+
'noImplicitThis',
255257
'noImplicitUseStrict',
256258
'noLib',
257259
'noResolve',
@@ -264,6 +266,7 @@ function applyCompilerOptions(applyTo: IGruntTSOptions, projectSpec: ITSConfigFi
264266
'removeComments',
265267
'rootDir',
266268
'skipDefaultLibCheck',
269+
'strictNullChecks',
267270
'sourceMap',
268271
'sourceRoot',
269272
'stripInternal',

test/commandLineAssertions.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/commandLineAssertions.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,22 @@ export const new_TypeScript_1_8_Features: ICompilePromise = (strings, options) =
609609
});
610610
};
611611

612+
export const new_TypeScript_2_0_Features: ICompilePromise = (strings, options) => {
613+
return new Promise(function(resolve, reject) {
614+
615+
const command = strings[1].replace(/\\/g,'/');
612616

617+
if (command.indexOf(`--strictNullChecks`) > -1 &&
618+
command.indexOf(`--noImplicitThis`) > -1 &&
619+
command.indexOf(`--lib es2017`) > -1) {
620+
resolve({
621+
code: 0,
622+
output: ""
623+
});
624+
}
625+
throw `expected to see all of the new TypeScript 2.0 values in the command line and didn't. Got this: ${command}`;
626+
});
627+
};
613628

614629
export const test_noLib = simpleCommandLineCheck("--noLib");
615630
export const test_emitBOM = simpleCommandLineCheck("--emitBOM");

0 commit comments

Comments
 (0)