Skip to content

Commit b2adc0f

Browse files
author
David Sheldrick
committed
update babel ast source maps in place. Closes #39
1 parent e1481a6 commit b2adc0f

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: index.js

+22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const os = require('os')
66
const path = require('path')
77
const process = require('process')
88
const semver = require('semver')
9+
const traverse = require('babel-traverse')
10+
911
const TSCONFIG_PATH = process.env.TSCONFIG_PATH
1012

1113
let upstreamTransformer = null
@@ -47,6 +49,25 @@ function loadJsonFile(jsonFilename) {
4749
}
4850
}
4951

52+
// only used with RN >= 52
53+
function sourceMapAstInPlace(tsMap, babelAst) {
54+
const tsConsumer = new SourceMapConsumer(tsMap)
55+
traverse.default.cheap(babelAst, node => {
56+
if (node.loc) {
57+
const originalStart = tsConsumer.originalPositionFor(node.loc.start)
58+
if (originalStart.line) {
59+
node.loc.start.line = originalStart.line
60+
node.loc.start.column = originalStart.column
61+
}
62+
const originalEnd = tsConsumer.originalPositionFor(node.loc.end)
63+
if (originalEnd.line) {
64+
node.loc.start.line = originalEnd.line
65+
node.loc.start.column = originalEnd.column
66+
}
67+
}
68+
})
69+
}
70+
5071
function composeRawSourceMap(tsMap, babelMap) {
5172
const tsConsumer = new SourceMapConsumer(tsMap)
5273
const composedMap = []
@@ -192,6 +213,7 @@ module.exports.transform = function(src, filename, options) {
192213
})
193214

194215
if (reactNativeMinorVersion >= 52) {
216+
sourceMapAstInPlace(tsCompileResult.sourceMapText, babelCompileResult.ast)
195217
return babelCompileResult
196218
}
197219

0 commit comments

Comments
 (0)