123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- 'use strict';
- const pathutils = require('./pathutils');
- const {
- GREATEST_LOWER_BOUND,
- LEAST_UPPER_BOUND
- } = require('source-map').SourceMapConsumer;
- function originalEndPositionFor(sourceMap, generatedEnd) {
-
-
-
-
-
- const beforeEndMapping = originalPositionTryBoth(
- sourceMap,
- generatedEnd.line,
- generatedEnd.column - 1
- );
- if (beforeEndMapping.source === null) {
- return null;
- }
-
-
-
-
-
- const afterEndMapping = sourceMap.generatedPositionFor({
- source: beforeEndMapping.source,
- line: beforeEndMapping.line,
- column: beforeEndMapping.column + 1,
- bias: LEAST_UPPER_BOUND
- });
- if (
-
-
- afterEndMapping.line === null ||
-
-
-
- sourceMap.originalPositionFor(afterEndMapping).line !==
- beforeEndMapping.line
- ) {
- return {
- source: beforeEndMapping.source,
- line: beforeEndMapping.line,
- column: Infinity
- };
- }
-
- return sourceMap.originalPositionFor(afterEndMapping);
- }
- function originalPositionTryBoth(sourceMap, line, column) {
- const mapping = sourceMap.originalPositionFor({
- line,
- column,
- bias: GREATEST_LOWER_BOUND
- });
- if (mapping.source === null) {
- return sourceMap.originalPositionFor({
- line,
- column,
- bias: LEAST_UPPER_BOUND
- });
- } else {
- return mapping;
- }
- }
- function isInvalidPosition(pos) {
- return (
- !pos ||
- typeof pos.line !== 'number' ||
- typeof pos.column !== 'number' ||
- pos.line < 0 ||
- pos.column < 0
- );
- }
- function getMapping(sourceMap, generatedLocation, origFile) {
- if (!generatedLocation) {
- return null;
- }
- if (
- isInvalidPosition(generatedLocation.start) ||
- isInvalidPosition(generatedLocation.end)
- ) {
- return null;
- }
- const start = originalPositionTryBoth(
- sourceMap,
- generatedLocation.start.line,
- generatedLocation.start.column
- );
- let end = originalEndPositionFor(sourceMap, generatedLocation.end);
-
- if (!(start && end)) {
- return null;
- }
- if (!(start.source && end.source)) {
- return null;
- }
- if (start.source !== end.source) {
- return null;
- }
-
- if (start.line === null || start.column === null) {
- return null;
- }
-
- if (end.line === null || end.column === null) {
- return null;
- }
- if (start.line === end.line && start.column === end.column) {
- end = sourceMap.originalPositionFor({
- line: generatedLocation.end.line,
- column: generatedLocation.end.column,
- bias: LEAST_UPPER_BOUND
- });
- end.column -= 1;
- }
- return {
- source: pathutils.relativeTo(start.source, origFile),
- loc: {
- start: {
- line: start.line,
- column: start.column
- },
- end: {
- line: end.line,
- column: end.column
- }
- }
- };
- }
- module.exports = getMapping;
|