Skip to content

Commit c55b6cd

Browse files
committed
feat(tsconfig): load tsconfig.json
1 parent 1ba99ea commit c55b6cd

File tree

7 files changed

+83
-15
lines changed

7 files changed

+83
-15
lines changed

guess.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
var fs = require('fs');
12
var path = require('path');
23

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

@@ -10,7 +13,34 @@ if (packageData &&
1013
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.ts';
1114
}
1215

16+
var compilerOptions = findAndParseTsConfig(process.cwd());
17+
1318
require('./index')({
1419
cwd: process.cwd(),
15-
pattern: pattern
20+
pattern: pattern,
21+
compilerOptions: compilerOptions
1622
});
23+
24+
function findAndParseTsConfig(cwd) {
25+
var tsconfigPath = ts.findConfigFile(process.cwd());
26+
if (!tsconfigPath) {
27+
return null;
28+
}
29+
30+
var compilerOptions = null;
31+
var parsed = ts.parseConfigFileTextToJson(tsconfigPath, fs.readFileSync(tsconfigPath, 'utf8'));
32+
if (parsed.error) {
33+
throw new Error(parsed.error.messageText);
34+
}
35+
36+
if (!parsed.config || !parsed.config.compilerOptions) {
37+
return null;
38+
}
39+
40+
var converted = ts.convertCompilerOptionsFromJson(parsed.config.compilerOptions, process.cwd());
41+
if (converted.errors && converted.errors.length > 0) {
42+
var msg = converted.errors.map(function(e) {return e.messageText}).join(', ');
43+
throw new Error(msg);
44+
}
45+
return converted.options;
46+
}

index.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ var TypeScriptSimple = require('typescript-simple').TypeScriptSimple;
99
function espowerTypeScript(options) {
1010
var separator = (options.pattern.lastIndexOf('/', 0) === 0) ? '' : '/';
1111
var pattern = options.cwd + separator + options.pattern;
12-
// TODO: load tsconfig.json
13-
var tsconfig = options.tsconfig || {};
14-
var compilerOptions = tsconfig.compilerOptions || {};
15-
var tss = new TypeScriptSimple(compilerOptions, false);
12+
var tss = new TypeScriptSimple(options.compilerOptions, false);
1613

1714
require.extensions['.ts'] = function(localModule, filepath) {
1815
var result = tss.compile(fs.readFileSync(filepath, 'utf-8'));

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)