Skip to content

Commit 758af13

Browse files
committed
Merge pull request #5 from power-assert-js/tsconfig
feat(tsconfig): load tsconfig.json
2 parents 1ba99ea + 091ce3d commit 758af13

File tree

8 files changed

+101
-18
lines changed

8 files changed

+101
-18
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ $ mocha --compilers ts:espower-typescript/guess spec/**/*.ts
4141

4242
Note: `'espower-typescript/guess'` is inspired by [intelli-espower-loader](https://github.com/azu/intelli-espower-loader)
4343

44-
### tsconfig.json
44+
### tsconfig.json and CompilerOptions
4545

46-
TODO
46+
If [tsconfig.json](https://github.com/Microsoft/TypeScript/wiki/tsconfig.json) is in cwd, `guess` load it automatically.
47+
48+
Note: only `compilerOptions` field is applied.
4749

4850
## License
4951

guess.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
var fs = require('fs');
12
var path = require('path');
23

4+
var ts = require('typescript');
5+
36
var pattern = 'test/**/*.ts';
4-
var packageData = require(path.join(process.cwd(), 'package.json'));
7+
var cwd = process.cwd();
8+
var packageData = require(path.join(cwd, 'package.json'));
59

610
if (packageData &&
711
typeof packageData.directories === 'object' &&
@@ -10,7 +14,30 @@ if (packageData &&
1014
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.ts';
1115
}
1216

17+
var tsconfigPath = ts.findConfigFile(cwd);
18+
var tsconfigBasepath = null;
19+
var compilerOptions = null;
20+
if (tsconfigPath) {
21+
compilerOptions = parseTsConfig(tsconfigPath);
22+
tsconfigBasepath = path.dirname(tsconfigPath);
23+
}
24+
1325
require('./index')({
14-
cwd: process.cwd(),
15-
pattern: pattern
26+
cwd: cwd,
27+
pattern: pattern,
28+
compilerOptions: compilerOptions,
29+
basepath: tsconfigBasepath
1630
});
31+
32+
function parseTsConfig(tsconfigPath) {
33+
var parsed = ts.parseConfigFileTextToJson(tsconfigPath, fs.readFileSync(tsconfigPath, 'utf8'));
34+
if (parsed.error) {
35+
throw new Error(parsed.error.messageText);
36+
}
37+
38+
if (!parsed.config || !parsed.config.compilerOptions) {
39+
return null;
40+
}
41+
42+
return parsed.config.compilerOptions;
43+
}

index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ var fs = require('fs');
44

55
var espowerSource = require('espower-source');
66
var minimatch = require('minimatch');
7+
var ts = require('typescript');
78
var TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
89

910
function espowerTypeScript(options) {
1011
var separator = (options.pattern.lastIndexOf('/', 0) === 0) ? '' : '/';
1112
var pattern = options.cwd + separator + options.pattern;
12-
// TODO: load tsconfig.json
13-
var tsconfig = options.tsconfig || {};
14-
var compilerOptions = tsconfig.compilerOptions || {};
13+
var compilerOptions = convertCompilerOptions(options.compilerOptions, options.basepath);
1514
var tss = new TypeScriptSimple(compilerOptions, false);
1615

1716
require.extensions['.ts'] = function(localModule, filepath) {
@@ -23,4 +22,18 @@ function espowerTypeScript(options) {
2322
};
2423
}
2524

25+
function convertCompilerOptions(compilerOptions, basepath) {
26+
if (!compilerOptions) {
27+
return null;
28+
}
29+
30+
var basepath = basepath || process.cwd();
31+
var converted = ts.convertCompilerOptionsFromJson(compilerOptions, basepath);
32+
if (converted.errors && converted.errors.length > 0) {
33+
var msg = converted.errors.map(function(e) {return e.messageText}).join(', ');
34+
throw new Error(msg);
35+
}
36+
return converted.options;
37+
}
38+
2639
module.exports = espowerTypeScript;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"dependencies": {
2929
"espower-source": "^1.0.0",
3030
"minimatch": "^3.0.0",
31+
"typescript": "1.8.0-dev.20151106",
3132
"typescript-simple": "^3.0.2"
3233
}
3334
}

test/demo-es6-import.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import hello from './lib/hello';
2+
3+
let assert = require('power-assert');
4+
5+
class Person {
6+
constructor(public name: string, public age: number) {
7+
}
8+
getAge(): string {
9+
return this.age;
10+
}
11+
}
12+
13+
describe('Person', () => {
14+
let alice = new Person('alice', 3);
15+
let bob = new Person('bob', 5);
16+
it('#getAge', () => {
17+
assert(alice.getAge() === 3)
18+
})
19+
it('#name', () => {
20+
assert(alice.name === 'alice')
21+
})
22+
// failed
23+
it('#mistake', () => {
24+
assert(alice.name === bob.name)
25+
})
26+
// failed
27+
it('hello', () => {
28+
assert(hello() === 'whoa!');
29+
});
30+
})

test/demo.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import hello from './lib/hello';
1+
// Run with Node.js v5+
2+
3+
'use strict';
24

35
let assert = require('power-assert');
46

@@ -14,17 +16,17 @@ describe('Person', () => {
1416
let alice = new Person('alice', 3);
1517
let bob = new Person('bob', 5);
1618
it('#getAge', () => {
17-
assert(alice.getAge() === 3)
18-
})
19+
assert(alice.getAge() === 3);
20+
});
1921
it('#name', () => {
20-
assert(alice.name === 'alice')
21-
})
22+
assert(alice.name === 'alice');
23+
});
2224
// failed
2325
it('#mistake', () => {
24-
assert(alice.name === bob.name)
25-
})
26+
assert(alice.name === bob.name);
27+
});
2628
// failed
27-
it('hello', () => {
28-
assert(hello() === 'whoa!');
29+
it('arrow function', () => {
30+
assert(alice.name === (() => 1));
2931
});
30-
})
32+
});

test/to_be_instrumented_test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
let assert = require('power-assert')
24
let expect = require('expect.js')
35

tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES5",
4+
"noImplicitAny": true
5+
}
6+
}

0 commit comments

Comments
 (0)