Skip to content
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

SourceMap does not work on multi-line chained function call. #325

Closed
yoshiokatsuneo opened this issue May 8, 2017 · 5 comments
Closed

Comments

@yoshiokatsuneo
Copy link

SourceMap does not work well if the error happens on chained function call.
I just made a simple project like below.

index.js:

require('ts-node').register();
require('./foo');

foo.ts:

let obj:any = null;
obj
.map();

tsconfig.js generation (default)

$ tsc --init

And, I got error message like below:

$ node ./node_modules/ts-node/dist/bin.js --compilerOptions '{"strict":true}' index.js 

/private/tmp/ts3/foo.ts:4
    .map();
    ^
TypeError: Cannot read property 'map' of null
    at Object.<anonymous> (/private/tmp/ts3/foo.ts:4:5)
    at Module._compile (module.js:570:32)
    at Module.m._compile (/private/tmp/ts3/node_modules/ts-node/src/index.ts:385:23)
    at Module.m._compile (/private/tmp/ts3/node_modules/ts-node/src/index.ts:385:23)
    at Module._extensions..js (module.js:579:10)
    at require.extensions.(anonymous function) (/private/tmp/ts3/node_modules/ts-node/src/index.ts:388:12)
    at Object.require.extensions.(anonymous function) [as .ts] (/private/tmp/ts3/node_modules/ts-node/src/index.ts:388:12)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

The message says, the error happens on line 4, column5.
But, the "foo.ts" does not have line 4, column 5. (The file is 3 lines).

The error does not happens if I set "strict: false" on tsconfig.json.

And, the error does not happens if the foo.js is like below(no multi-line chained call).

let obj:any = null;
obj.map();

I dig into the issue, and found that the issue looks happens when "originalPositionFor" of source-map npm returns null. "originalPositionFor" looks assuming source map contains for all lines' column 0. But ts-node may not have source map for all the lines.

https://github.com/mozilla/source-map/blob/75663e0187002920ad98ed1de21e54cb85114609/lib/source-map-consumer.js#L616

@blakeembrey
Copy link
Member

I'm honestly not sure if there's anything I can fix or do different in this package. The source map comes from TypeScript and the node support for source maps comes from source-map-support. I'll leave this open though, let me know if you figure something out! 😄

@yoshiokatsuneo
Copy link
Author

Possibly, ts-node can generate a new source-map for every line based on the source-map from typescript... But, I'm not sure whether this is the best solution...

@blakeembrey
Copy link
Member

What would the difference be from the source map TypeScript gives me? If there's a bug, it would need to be fixed in TypeScript (once we verify it's from there and not some other issue).

@yoshiokatsuneo
Copy link
Author

yoshiokatsuneo commented May 9, 2017

Let's say, TypeScript gives source map for line number 1,3,5. But, the source-map npm requires for all lines(1,2,3,4,5,6), and returns "null" for line number 2,4,6, if ts-node provides information only for line number 1,3,5.
So, ts-node may be able to interpolate for line number 2,4,6 from the information for line number 1,3,5.

(But, I'm not sure why source-map npm does not accept in this case...)

@cspotcode
Copy link
Collaborator

I've confirmed that the behavior described in this ticket no longer occurs. The line number is correct.
https://github.com/TypeStrong/ts-node-repros/tree/325
https://github.com/TypeStrong/ts-node-repros/runs/2816394149

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants