File size: 2,021 Bytes
bc20498 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.fixLocations = fixLocations;
const traverse_1 = require("../traverse");
/** Fix locations */
function fixLocations(node, tokens, comments, offset, visitorKeys, ctx) {
if (offset === 0) {
return;
}
const traversed = new Set();
(0, traverse_1.traverseNodes)(node, {
visitorKeys,
enterNode: (n) => {
if (traversed.has(n)) {
return;
}
traversed.add(n);
if (traversed.has(n.range)) {
if (!traversed.has(n.loc)) {
// However, `Node#loc` may not be shared.
const locs = ctx.getConvertLocation({
start: n.range[0],
end: n.range[1],
});
applyLocs(n, locs);
traversed.add(n.loc);
}
}
else {
const start = n.range[0] + offset;
const end = n.range[1] + offset;
const locs = ctx.getConvertLocation({ start, end });
applyLocs(n, locs);
traversed.add(n.range);
traversed.add(n.loc);
}
},
leaveNode: Function.prototype,
});
for (const t of tokens) {
const start = t.range[0] + offset;
const end = t.range[1] + offset;
const locs = ctx.getConvertLocation({ start, end });
applyLocs(t, locs);
}
for (const t of comments) {
const start = t.range[0] + offset;
const end = t.range[1] + offset;
const locs = ctx.getConvertLocation({ start, end });
applyLocs(t, locs);
}
}
/**
* applyLocs
*/
function applyLocs(target, locs) {
target.loc = locs.loc;
target.range = locs.range;
if (typeof target.start === "number") {
delete target.start;
}
if (typeof target.end === "number") {
delete target.end;
}
}
|