babel: [babel-generator] Generated code doesn’t match with source with input source-maps

Input Code

I’ve put up a repo here

The relevant code is here

Expected Behavior

I would have expected the source-map from babel-generator to map from the original TypeScript source to the generated JS.

Current Behavior

The source-map doesn’t appear to have any relation to the original code.

Here are some examples of the code looked-up through the source-maps:

 Original: import * as React fr
Transform: var React=require("
 Generate: "use strict";

 Original: function MyComponent
Transform: function MyComponen
 Generate:  require("react");

As you can see, the transform step does a good job of mapping to the original code, but the generate step just produces something completely different.

Context

I’m trying to use TypeScript for our React Native project and have inline source-maps propagate properly through Babel. I would prefer to not write the intermediate JS (produced by tsc) to disk.

I’m not sure if I’m doing something wrong, if this has to do with how React Native uses transform and the generator, or if there’s a bug with babel-generator.

Your Environment

software version
Babel 6.23.1
node 6.2.1
npm 4.1.1
Operating System macOS 10.12.2

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 3
  • Comments: 16 (5 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, as in the OP’s reproduced code. babel/core is merging source maps. So this is just an enhancement and shouldn’t be blocking.

Any news on this?

I think I came across a similar issue while trying to write a jest preprocessor for TS -> Babel and getting code coverage right.

I ended up not passing inputSourceMap into babel, and using source-map-merge to merge the source maps outputted by the TS compiler and babel respectively.

Perhaps babel should use this module for merging input source maps rather than it’s own merge code? From debugging the issue before I resorted to source-map-merge the merge code inside babel is here:

https://github.com/babel/babel/blob/master/packages/babel-core/src/transformation/file/index.js#L368