diff --git a/.gitignore b/.gitignore index 636f55f..7b23664 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ /node_modules test/test-outdir/**/*.js +yarn.lock +*.log \ No newline at end of file diff --git a/index.js b/index.js index 2bd5c46..918daf0 100644 --- a/index.js +++ b/index.js @@ -5,10 +5,19 @@ const path = require('path'); const espowerSource = require('espower-source'); const minimatch = require('minimatch'); +const sourceMapSupport = require('source-map-support'); const tsNodeRegister = require('ts-node').register; +const sourceCache = {}; function espowerTypeScript(options, tsNodeOptions) { tsNodeRegister(tsNodeOptions); + + // install source-map-support again to correct the source-map + sourceMapSupport.install({ + environment: 'node', + retrieveFile: path => sourceCache[path], + }); + const {extensions = ['ts', 'tsx']} = options; extensions.forEach(ext => { espowerTsRegister(`.${ext}`, options); @@ -26,7 +35,9 @@ function espowerTsRegister(ext, options) { } const originalCompile = module._compile; module._compile = function(code, filepath) { - return originalCompile.call(this, espowerSource(code, filepath, options), filepath); + const newSource = espowerSource(code, filepath, options); + sourceCache[filepath] = newSource; + return originalCompile.call(this, newSource, filepath); }; return originalExtension(module, filepath); }; diff --git a/package.json b/package.json index b1907d9..7c34de7 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "dependencies": { "espower-source": "^2.3.0", "minimatch": "^3.0.3", + "source-map-support": "^0.5.9", "ts-node": "^7.0.0" }, "devDependencies": { diff --git a/test/lib/expectErrorStacksCorrect.ts b/test/lib/expectErrorStacksCorrect.ts new file mode 100644 index 0000000..71ea1a6 --- /dev/null +++ b/test/lib/expectErrorStacksCorrect.ts @@ -0,0 +1,12 @@ +import assert = require('assert'); + +export default function expectErrorStacksCorrect(line: number, column: number) { + try { + assert.fail('AssertionError should be thrown'); + } catch(e) { + const matches = e.stack.match(/expectErrorStacksCorrect\.ts:(\d+):(\d+)/); + assert(!!matches); + assert.equal(matches[1], line); + assert.equal(matches[2], column); + } +} \ No newline at end of file diff --git a/test/to_be_instrumented_test.ts b/test/to_be_instrumented_test.ts index 2127bae..c8a2b33 100644 --- a/test/to_be_instrumented_test.ts +++ b/test/to_be_instrumented_test.ts @@ -2,6 +2,7 @@ import assert = require('assert'); import expectPowerAssertMessage from './lib/expectPowerAssertMessage'; +import expectErrorStacksCorrect from './lib/expectErrorStacksCorrect'; import MyComponent from './lib/mycomponent'; describe('espower-typescript: ts', function() { @@ -39,6 +40,10 @@ describe('espower-typescript: ts', function() { }, expected); }); + it('error stack line number and column number should correct', function() { + expectErrorStacksCorrect(5, 12); + }); + it('jsx:react', function() { let expected = ` assert.equal(1, mycomponent_1.default())