Skip to content

Transpile .js files as well if allowJs:true exists in tsconfig.json #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
test/**/*.js
/node_modules
test/test-outdir/**/*.js
25 changes: 13 additions & 12 deletions guess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@ var fs = require('fs');
var path = require('path');

var ts = require('typescript');

var pattern = 'test/**/*.ts';
var cwd = process.cwd();
var packageData = require(path.join(cwd, 'package.json'));

if (packageData &&
typeof packageData.directories === 'object' &&
typeof packageData.directories.test === 'string') {
var testDir = packageData.directories.test;
pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + '**/*.ts';
}

var tsconfigPath = ts.findConfigFile(cwd, fs.existsSync);
var tsconfigBasepath = null;
var compilerOptions = null;
if (tsconfigPath) {
compilerOptions = parseTsConfig(tsconfigPath);
tsconfigBasepath = path.dirname(tsconfigPath);
}
var allowJs = (compilerOptions && compilerOptions.allowJs);

var cwd = process.cwd();
var packageData = require(path.join(cwd, 'package.json'));
var testDir;
if (packageData &&
typeof packageData.directories === 'object' &&
typeof packageData.directories.test === 'string') {
testDir = packageData.directories.test;
} else {
testDir = 'test';
}
var pattern = testDir + ((testDir.lastIndexOf('/', 0) === 0) ? '' : '/') + (allowJs ? '**/*.{js,ts}' : '**/*.ts');

require('./index')({
cwd: cwd,
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function espowerTypeScript(options) {
var pattern = cwd + separator + options.pattern;
var compilerOptions = convertCompilerOptions(options.compilerOptions, options.basepath || cwd);
var tss = new TypeScriptSimple(compilerOptions, false);
var allowJs = (compilerOptions && compilerOptions.allowJs);

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

require.extensions['.ts'] = loadTypeScript;
require.extensions['.tsx'] = loadTypeScript;
if (allowJs) {
require.extensions['.js'] = loadTypeScript;
}
}

function convertCompilerOptions(compilerOptions, basepath) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"main": "index.js",
"scripts": {
"demo": "mocha --require './guess' test/demo.ts",
"test": "mocha --require './guess' test/*_test.ts && npm run test:outdir",
"test": "mocha --require './guess' test/*_test.ts && npm run test:outdir && npm run test:allowJs",
"test:allowJs": "cd test/test-allow-js && mocha --require ../../guess test/*_test.js",
"test:outdir": "cd test/test-outdir && mocha --require ../../guess test/*_test.ts"
},
"dependencies": {
Expand Down
9 changes: 9 additions & 0 deletions test/test-allow-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "test-allow-js",
"version": "1.0.0",
"description": "This is a dummy file. See ../../package.json",
"devDependencies": {
},
"dependencies": {
}
}
29 changes: 29 additions & 0 deletions test/test-allow-js/test/to_be_instrumented_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

import assert = require('assert')
import expect = require('expect.js')
import MyComponent from '../lib/mycomponent.tsx';

describe('test for allowJs option', function() {
beforeEach(function() {
this.expectPowerAssertMessage = (body: () => void, expectedLines: string) => {
try {
body();
expect().fail('AssertionError should be thrown');
} catch(e) {
expect(e.message.split('\n').slice(2, -1).join('\n')).to.eql(expectedLines);
}
}
});

it('equal with Literal and Identifier: assert.equal(1, minusOne)', function() {
let minusOne: number = -1;
let expected: string =
` assert.equal(1, minusOne)
|
-1 `;
this.expectPowerAssertMessage(() => {
assert.equal(1, minusOne);
}, expected);
});
});
13 changes: 13 additions & 0 deletions test/test-allow-js/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"allowJs": true,
"outDir": "build",
"module": "commonjs",
"target": "ES5",
"noImplicitAny": true,
"jsx": "react"
},
"exclude": [
"node_modules"
]
}