Skip to content

Commit

Permalink
Merge pull request #98 from emilniklas/master
Browse files Browse the repository at this point in the history
Add ability to resolve "extends" directive in tsconfig.json
  • Loading branch information
kulshekhar committed Jan 18, 2017
2 parents 78c353c + 50495c4 commit 4ab2db9
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 40 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -28,7 +28,8 @@
"author": "Kulshekhar Kabra <kulshekhar@users.noreply.github.com> (https://github.com/kulshekhar)",
"contributors": [
"Ihor Chulinda <ichulinda@gmail.com> (https://github.com/Igmat)",
"OJ Kwon <kwon.ohjoong@gmail.com> (https://github.com/kwonoj)"
"OJ Kwon <kwon.ohjoong@gmail.com> (https://github.com/kwonoj)",
"Emil Persson <emil.n.persson@gmail.com> (https://github.com/emilniklas)"
],
"license": "MIT",
"bugs": {
Expand Down
11 changes: 9 additions & 2 deletions src/utils.ts
Expand Up @@ -73,7 +73,14 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
config = 'tsconfig.json';
}
if (typeof config === 'string') {
config = require(path.resolve(config)).compilerOptions;
const configPath = path.resolve(config);
const external = require(configPath);
config = external.compilerOptions || {};

if (typeof external.extends === 'string') {
const parentConfigPath = path.join(path.dirname(configPath), external.extends);
config = Object.assign({}, require(parentConfigPath).compilerOptions, config);
}
}
config.module = config.module || tsc.ModuleKind.CommonJS;
config.jsx = config.jsx || tsc.JsxEmit.React;
Expand All @@ -89,4 +96,4 @@ export function getTSConfig(globals, collectCoverage: boolean = false) {
}

return tsc.convertCompilerOptionsFromJson(config, undefined).options;
}
}
23 changes: 12 additions & 11 deletions tests/__tests__/tsconfig-default.spec.ts
@@ -1,5 +1,6 @@
import { } from 'jest';
import { } from 'node';
import * as ts from 'typescript'

jest.mock('path');

Expand All @@ -15,11 +16,11 @@ describe('get default ts config', () => {
const result = getTSConfig();

expect(result).toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': false,
'jsx': 2
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -28,11 +29,11 @@ describe('get default ts config', () => {
const result = getTSConfig();

expect(result).not.toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': true,
'jsx': 2
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -41,8 +42,8 @@ describe('get default ts config', () => {
const result = getTSConfig();

expect(result).not.toEqual ({
'module': 1,
'jsx': 2
'module': ts.ModuleKind.CommonJS,
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -62,4 +63,4 @@ describe('get default ts config', () => {
expect(result).toEqual(resultNullContent);
});

});
});
23 changes: 12 additions & 11 deletions tests/__tests__/tsconfig-inline.spec.ts
@@ -1,5 +1,6 @@
import { } from 'jest';
import { } from 'node';
import * as ts from 'typescript';

jest.mock('path');

Expand All @@ -20,8 +21,8 @@ describe('get inline ts config', () => {
});

expect(result).toEqual ({
'module': 1,
'jsx': 2
'module': ts.ModuleKind.CommonJS,
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -35,11 +36,11 @@ describe('get inline ts config', () => {
});

expect(result).not.toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': false,
'jsx': 2
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -53,12 +54,12 @@ describe('get inline ts config', () => {
});

expect(result).not.toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': true,
'jsx': 2
'jsx': ts.JsxEmit.React
});
});

});
});
54 changes: 43 additions & 11 deletions tests/__tests__/tsconfig-string.spec.ts
@@ -1,5 +1,6 @@
import { } from 'jest';
import { } from 'node';
import * as ts from 'typescript';

jest.mock('path');

Expand All @@ -17,11 +18,11 @@ describe('get ts config from string', () => {
});

expect(result).toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': true,
'jsx': 2
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -32,11 +33,11 @@ describe('get ts config from string', () => {
});

expect(result).not.toEqual ({
'target': 2,
'module': 1,
'moduleResolution': 2,
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': false,
'jsx': 2
'jsx': ts.JsxEmit.React
});
});

Expand All @@ -47,9 +48,40 @@ describe('get ts config from string', () => {
});

expect(result).not.toEqual ({
'module': 1,
'jsx': 2
'target': ts.ScriptTarget.ES5,
'jsx': ts.JsxEmit.React
});
});

});
it('should correctly resolve the "extends" directive', () => {
const {getTSConfig} = require('../../src/utils');
const result = getTSConfig({
'__TS_CONFIG__': 'extends-tsconfig.json'
});

expect(result).toEqual ({
'target': ts.ScriptTarget.ES2015,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': true,
'jsx': ts.JsxEmit.React
});
});

it('should correctly override any config in the "extends" directive', () => {
const {getTSConfig} = require('../../src/utils');
const result = getTSConfig({
'__TS_CONFIG__': 'extends-with-overrides-tsconfig.json'
});

expect(result).toEqual ({
'target': ts.ScriptTarget.ES5,
'module': ts.ModuleKind.CommonJS,
'moduleResolution': ts.ModuleResolutionKind.NodeJs,
'noEmitOnError': true,
'jsx': ts.JsxEmit.React,
'noImplicitAny': true
});
});

});
12 changes: 8 additions & 4 deletions tests/__ts-jest-mocks__/path.ts
Expand Up @@ -3,7 +3,9 @@ import { } from 'node';

interface MockedPath {
__setBaseDir(newBaseDir);
resolve(args);
resolve(...args);
dirname(...args);
join(...args);
}

const path = require.requireActual('path');
Expand All @@ -19,11 +21,13 @@ function __setBaseDir(newBaseDir) {

// A custom version of `readdirSync` that reads from the special mocked out
// file list set via __setMockFiles
function resolve(args) {
return path.resolve(baseDir, args);
function resolve(...args) {
return path.resolve(baseDir, ...args);
}

mockedPath.__setBaseDir = __setBaseDir;
mockedPath.resolve = resolve;
mockedPath.dirname = path.dirname;
mockedPath.join = path.join;

module.exports = mockedPath;
module.exports = mockedPath;
3 changes: 3 additions & 0 deletions tests/tsconfig-test/extends-tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "./my-tsconfig.json"
}
7 changes: 7 additions & 0 deletions tests/tsconfig-test/extends-with-overrides-tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "./my-tsconfig.json",
"compilerOptions": {
"target": "ES5",
"noImplicitAny": true
}
}

0 comments on commit 4ab2db9

Please sign in to comment.