Skip to content

Commit

Permalink
Merge pull request #398 from TypeStrong/support-tsconfig-extends
Browse files Browse the repository at this point in the history
Support tsconfig.json extends
  • Loading branch information
nycdotnet authored Mar 3, 2017
2 parents f261183 + 6abcf3a commit 1316c3b
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 46 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"ncp": "0.5.1",
"rimraf": "2.2.6",
"semver": "^5.3.0",
"strip-bom": "^2.0.0"
"strip-bom": "^2.0.0",
"jsmin2": "^1.2.1"
},
"peerDependencies": {
"grunt": "^1.0.1 || ^0.4.0",
Expand Down
2 changes: 2 additions & 0 deletions tasks/modules/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ interface ITSConfigFile {
exclude?: string[];
include?: string[];
filesGlob?: string[];
/** this tsconfig overrides settings in its parent tsconfig located here */
extends?: string;
}

// NOTE: This is from tsconfig.ts in atom-typescript
Expand Down
61 changes: 38 additions & 23 deletions tasks/modules/tsconfig.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 36 additions & 21 deletions tasks/modules/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as stripBom from 'strip-bom';
import * as _ from 'lodash';
import * as utils from './utils';
import * as ts from '../ts';
import * as jsmin from 'jsmin2';


let templateProcessor: (templateString: string, options: any) => string = null;
let globExpander: (globs: string[]) => string[] = null;
Expand Down Expand Up @@ -79,29 +81,42 @@ export function resolveAsync(applyTo: IGruntTSOptions,
(<ITSConfigSupport>applyTo.tsconfig).tsconfig = '.';
}
} else {
let projectFile = (<ITSConfigSupport>applyTo.tsconfig).tsconfig;
try {
var projectFileTextContent = fs.readFileSync(projectFile, 'utf8');
} catch (ex) {
if (ex && ex.code === 'ENOENT') {
return reject('Could not find file "' + projectFile + '".');
} else if (ex && ex.errno) {
return reject('Error ' + ex.errno + ' reading "' + projectFile + '".');
} else {
return reject('Error reading "' + projectFile + '": ' + JSON.stringify(ex));
}
}

try {
var projectSpec: ITSConfigFile;
const content = stripBom(projectFileTextContent);
if (content.trim() === '') {
projectSpec = {};
} else {
projectSpec = JSON.parse(content);
let projectSpec: ITSConfigFile = {extends: (<ITSConfigSupport>applyTo.tsconfig).tsconfig};
while (projectSpec.extends) {
const pathOfTsconfig = path.resolve(projectSpec.extends, '..');
try {
var projectFileTextContent = fs.readFileSync(projectSpec.extends, 'utf8');
} catch (ex) {
if (ex && ex.code === 'ENOENT') {
return reject('Could not find file "' + projectSpec.extends + '".');
} else if (ex && ex.errno) {
return reject('Error ' + ex.errno + ' reading "' + projectSpec.extends + '".');
} else {
return reject('Error reading "' + projectSpec.extends + '": ' + JSON.stringify(ex));
}
}
try {
const content = stripBom(projectFileTextContent);
if (content.trim() === '') {
// we are done.
projectSpec.extends = undefined;
} else {
const minifiedContent = jsmin(content);
const parentContent = JSON.parse(minifiedContent.code);
projectSpec = _.defaultsDeep(projectSpec, parentContent);
if (parentContent.extends) {
projectSpec.extends = path.resolve(pathOfTsconfig, parentContent.extends);
if (!_.endsWith(projectSpec.extends, '.json')) {
projectSpec.extends += '.json';
}
} else {
projectSpec.extends = undefined;
}
}
} catch (ex) {
return reject('Error parsing "' + projectSpec.extends + '". It may not be valid JSON in UTF-8.');
}
} catch (ex) {
return reject('Error parsing "' + projectFile + '". It may not be valid JSON in UTF-8.');
}

applyTo = handleBadConfiguration(applyTo, projectSpec);
Expand Down
14 changes: 14 additions & 0 deletions test/optionsResolverTests.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion test/optionsResolverTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,20 @@ export var tests : nodeunit.ITestGroup = {
test.strictEqual(result.warnings.length, 0, "expected zero warnings.");
test.done();
}).catch((err) => {test.ifError(err); test.done();});
}
},
"tsconfig with extends and nostrictnull works as expected": function (test) {
test.expect(4);
var cfg = getConfig("minimalist");
cfg.tsconfig = {
tsconfig: './test/tsconfig_artifact/extends/tsconfig.nostrictnull.json',
};
var result = or.resolveAsync(null, cfg).then(function (result) {
test.strictEqual(result.strictNullChecks, false, "expected to get strict null checks disabled.");
test.strictEqual(result.noImplicitAny, true, "expected to get noImplicitAny from configs/base.json.");
test.ok(result.CompilationTasks[0].src.indexOf('test/abtest/a.ts') > -1, "expected to see a.ts included.");
test.ok(result.CompilationTasks[0].src.indexOf('test/abtest/b.ts') > -1, "expected to see b.ts included.");
test.done();
}).catch(function (err) { test.ifError(err); test.done(); });
}
}
};
6 changes: 6 additions & 0 deletions test/tsconfig_artifact/extends/configs/base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true
}
}
7 changes: 7 additions & 0 deletions test/tsconfig_artifact/extends/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./configs/base",
"files": [
"../../abtest/a.ts",
"../../abtest/b.ts"
]
}
6 changes: 6 additions & 0 deletions test/tsconfig_artifact/extends/tsconfig.nostrictnull.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"strictNullChecks": false
}
}

0 comments on commit 1316c3b

Please sign in to comment.