split up babel core into multiple modules
This commit is contained in:
66
packages/babel-generator/src/source-map.js
Normal file
66
packages/babel-generator/src/source-map.js
Normal file
@@ -0,0 +1,66 @@
|
||||
import sourceMap from "source-map";
|
||||
import * as t from "babel-types";
|
||||
|
||||
/**
|
||||
* Build a sourcemap.
|
||||
*/
|
||||
|
||||
export default class SourceMap {
|
||||
constructor(position, opts, code) {
|
||||
this.position = position;
|
||||
this.opts = opts;
|
||||
|
||||
if (opts.sourceMaps) {
|
||||
this.map = new sourceMap.SourceMapGenerator({
|
||||
file: opts.sourceMapTarget,
|
||||
sourceRoot: opts.sourceRoot
|
||||
});
|
||||
|
||||
this.map.setSourceContent(opts.sourceFileName, code);
|
||||
} else {
|
||||
this.map = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sourcemap.
|
||||
*/
|
||||
|
||||
get() {
|
||||
var map = this.map;
|
||||
if (map) {
|
||||
return map.toJSON();
|
||||
} else {
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Mark a node's generated position, and add it to the sourcemap.
|
||||
*/
|
||||
|
||||
mark(node, type) {
|
||||
var loc = node.loc;
|
||||
if (!loc) return; // no location info
|
||||
|
||||
var map = this.map;
|
||||
if (!map) return; // no source map
|
||||
|
||||
if (t.isProgram(node) || t.isFile(node)) return; // illegal mapping nodes
|
||||
|
||||
var position = this.position;
|
||||
|
||||
var generated = {
|
||||
line: position.line,
|
||||
column: position.column
|
||||
};
|
||||
|
||||
var original = loc[type];
|
||||
|
||||
map.addMapping({
|
||||
source: this.opts.sourceFileName,
|
||||
generated: generated,
|
||||
original: original
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user