Skip to content

Commit 7c4ed24

Browse files
committed
feat: transpile .js files as well if allowJs:true exists in tsconfig.json
1 parent a9d9365 commit 7c4ed24

File tree

7 files changed

+72
-14
lines changed

7 files changed

+72
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
test/**/*.js
1+
/node_modules
2+
test/test-outdir/**/*.js

guess.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,26 @@ var fs = require('fs');
22
var path = require('path');
33

44
var ts = require('typescript');
5-
6-
var pattern = 'test/**/*.ts';
7-
var cwd = process.cwd();
8-
var packageData = require(path.join(cwd, 'package.json'));
9-
10-
if (packageData &&
11-
typeof packageData.directories === 'object' &&
12-
typeof packageData.directories.test === 'string') {
13-
var testDir = packageData.directories.test;
14-
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.ts';
15-
}
16-
175
var tsconfigPath = ts.findConfigFile(cwd, fs.existsSync);
186
var tsconfigBasepath = null;
197
var compilerOptions = null;
208
if (tsconfigPath) {
219
compilerOptions = parseTsConfig(tsconfigPath);
2210
tsconfigBasepath = path.dirname(tsconfigPath);
2311
}
12+
var allowJs = (compilerOptions && compilerOptions.allowJs);
13+
14+
var cwd = process.cwd();
15+
var packageData = require(path.join(cwd, 'package.json'));
16+
var testDir;
17+
if (packageData &&
18+
typeof packageData.directories === 'object' &&
19+
typeof packageData.directories.test === 'string') {
20+
testDir = packageData.directories.test;
21+
} else {
22+
testDir = 'test';
23+
}
24+
var pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + (allowJs ? '**/*.{js,ts}' : '**/*.ts');
2425

2526
require('./index')({
2627
cwd: cwd,

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function espowerTypeScript(options) {
1414
var pattern = cwd + separator + options.pattern;
1515
var compilerOptions = convertCompilerOptions(options.compilerOptions, options.basepath || cwd);
1616
var tss = new TypeScriptSimple(compilerOptions, false);
17+
var allowJs = (compilerOptions && compilerOptions.allowJs);
1718

1819
function loadTypeScript(localModule, filepath) {
1920
var result = tss.compile(fs.readFileSync(filepath, 'utf-8'), path.relative(cwd, filepath));
@@ -25,6 +26,9 @@ function espowerTypeScript(options) {
2526

2627
require.extensions['.ts'] = loadTypeScript;
2728
require.extensions['.tsx'] = loadTypeScript;
29+
if (allowJs) {
30+
require.extensions['.js'] = loadTypeScript;
31+
}
2832
}
2933

3034
function convertCompilerOptions(compilerOptions, basepath) {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"main": "index.js",
1010
"scripts": {
1111
"demo": "mocha --require './guess' test/demo.ts",
12-
"test": "mocha --require './guess' test/*_test.ts && npm run test:outdir",
12+
"test": "mocha --require './guess' test/*_test.ts && npm run test:outdir && npm run test:allowJs",
13+
"test:allowJs": "cd test/test-allow-js && mocha --require ../../guess test/*_test.js",
1314
"test:outdir": "cd test/test-outdir && mocha --require ../../guess test/*_test.ts"
1415
},
1516
"dependencies": {

test/test-allow-js/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "test-allow-js",
3+
"version": "1.0.0",
4+
"description": "This is a dummy file. See ../../package.json",
5+
"devDependencies": {
6+
},
7+
"dependencies": {
8+
}
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
import assert = require('assert')
4+
import expect = require('expect.js')
5+
import MyComponent from '../lib/mycomponent.tsx';
6+
7+
describe('test for allowJs option', function() {
8+
beforeEach(function() {
9+
this.expectPowerAssertMessage = (body: () => void, expectedLines: string) => {
10+
try {
11+
body();
12+
expect().fail('AssertionError should be thrown');
13+
} catch(e) {
14+
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
15+
}
16+
}
17+
});
18+
19+
it('equal with Literal and Identifier: assert.equal(1, minusOne)', function() {
20+
let minusOne: number = -1;
21+
let expected: string =
22+
` assert.equal(1, minusOne)
23+
|
24+
-1 `;
25+
this.expectPowerAssertMessage(() => {
26+
assert.equal(1, minusOne);
27+
}, expected);
28+
});
29+
});

test/test-allow-js/tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"outDir": "build",
5+
"module": "commonjs",
6+
"target": "ES5",
7+
"noImplicitAny": true,
8+
"jsx": "react"
9+
},
10+
"exclude": [
11+
"node_modules"
12+
]
13+
}

0 commit comments

Comments
 (0)