How to use the source-map.SourceMapConsumer.GREATEST_LOWER_BOUND function in source-map

To help you get started, we’ve selected a few source-map examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github firefox-devtools / devtools-core / packages / devtools-source-map / src / source-map.js View on Github external
if (!COMPUTED_SPANS.has(map)) {
    COMPUTED_SPANS.add(map);
    map.computeColumnSpans();
  }

  // We want to use 'allGeneratedPositionsFor' to get the _first_ generated
  // location, but it hard-codes SourceMapConsumer.LEAST_UPPER_BOUND as the
  // bias, making it search in the wrong direction for this usecase.
  // To work around this, we use 'generatedPositionFor' and then look up the
  // exact original location, making any bias value unnecessary, and then
  // use that location for the call to 'allGeneratedPositionsFor'.
  const genPos = map.generatedPositionFor({
    source: originalSource.url,
    line: location.line,
    column: location.column == null ? 0 : location.column,
    bias: SourceMapConsumer.GREATEST_LOWER_BOUND
  });
  if (genPos.line === null) return [];

  const positions = map.allGeneratedPositionsFor(map.originalPositionFor({
    line: genPos.line,
    column: genPos.column,
  }));

  return positions
    .map(mapping => ({
      line: mapping.line,
      columnStart: mapping.column,
      columnEnd: mapping.lastColumn,
    }))
    .sort((a, b) => {
      const line = a.line - b.line;
github microsoft / vscode-chrome-debug-core / src / sourceMaps / sourceMap.ts View on Github external
public authoredPositionFor(line: number, column: number): Maybe {
        // source-map lib uses 1-indexed lines.
        line++;

        const lookupArgs = {
            line,
            column,
            bias: SourceMapConsumer.LEAST_UPPER_BOUND
        };

        let position = this._smc.originalPositionFor(lookupArgs);
        if (!position.source) {
            // If it can't find a match, it returns a mapping with null props. Try looking the other direction.
            lookupArgs.bias = SourceMapConsumer.GREATEST_LOWER_BOUND;
            position = this._smc.originalPositionFor(lookupArgs);
        }

        if (position.source) {
            // file:/// -> absolute path
            position.source = utils.canonicalizeUrl(position.source);

            // Convert back to original case
            position.source = this._authoredPathCaseMap.get(position.source) || position.source;

            // Back to 0-indexed lines
            position.line--;

            return position;
        } else {
            return null;
github linkedin / css-blocks / packages / @css-blocks / core / src / SourceLocation.ts View on Github external
function pickBestSourceRange(configuration: Configuration, filename: string, source: string, consumer: ReturnType , start: SourcePosition, end: SourcePosition): MappedSourceRange | SourceRange {
  let generated = { filename, start, end, source };
  // Fun fact! The positions returned by the source map consumer use a 1-based
  // index for lines, and a 0-based index for columns.
  let startOrigin: NullableMappedPosition = consumer.originalPositionFor({ line: start.line, column: start.column - 1, bias: SourceMapConsumer.LEAST_UPPER_BOUND});
  let endOrigin: NullableMappedPosition = consumer.originalPositionFor({ line: end.line, column: end.column - 1, bias: SourceMapConsumer.LEAST_UPPER_BOUND});
  // If the start and end locations of the origins are in different files,
  // we try different biases to see if we can get something that agrees.
  if (startOrigin.source && startOrigin.source !== endOrigin.source) {
    let startOrigin2: NullableMappedPosition = consumer.originalPositionFor({ line: start.line, column: start.column - 1, bias: SourceMapConsumer.GREATEST_LOWER_BOUND });
    let endOrigin2: NullableMappedPosition = consumer.originalPositionFor({ line: end.line, column: end.column - 1, bias: SourceMapConsumer.GREATEST_LOWER_BOUND });
    if (startOrigin2.source && startOrigin2.source === endOrigin.source) {
      startOrigin = startOrigin2;
    } else if (endOrigin2.source && startOrigin.source === endOrigin2.source) {
      endOrigin = endOrigin2;
    } else if (startOrigin2.source && endOrigin2.source && startOrigin2.source === endOrigin2.source) {
      startOrigin = startOrigin2;
      endOrigin = endOrigin2;
    } else if (startOrigin.line !== null && startOrigin.column !== null) {
      // pick the starting file's location and add a character.
      endOrigin = { ...startOrigin, ...{ column: startOrigin.column + 1 } };
    } else if (endOrigin.line !== null && endOrigin.column !== null && endOrigin.column > 0) {
      // pick the ending file's location and remove a character.
      startOrigin = { ...endOrigin, ...{ column: endOrigin.column - 1 } };
    } else {
      // I don't think we'll get here
      return { filename, start, end };
github firefox-devtools / vscode-firefox-debug / src / adapter / firefox / sourceMaps / info.ts View on Github external
import * as url from 'url';
import { Log } from '../../util/log';
import { isWindowsPlatform as detectWindowsPlatform } from '../../../common/util';
import { ISourceActorProxy, SourceActorProxy } from '../actorProxy/source';
import { SourceMapConsumer, BasicSourceMapConsumer, MappingItem } from 'source-map';
import { UrlLocation, LocationWithColumn } from '../../location';

let GREATEST_LOWER_BOUND = SourceMapConsumer.GREATEST_LOWER_BOUND;
let LEAST_UPPER_BOUND = SourceMapConsumer.LEAST_UPPER_BOUND;

const isWindowsPlatform = detectWindowsPlatform();
const windowsAbsolutePathRegEx = /^[a-zA-Z]:[\/\\]/;

declare module "source-map" {
	interface MappingItem {
		lastGeneratedColumn?: number | null;
	}
}

const log = Log.create('SourceMappingInfo');

export class SourceMappingInfo {

	private columnSpansComputed = false;
github firefox-devtools / debugger / packages / devtools-source-map / src / source-map.js View on Github external
if (!map) {
    return;
  }

  const start = map.generatedPositionFor({
    source: originalSource.url,
    line: 1,
    column: 0,
    bias: SourceMapConsumer.LEAST_UPPER_BOUND
  });

  const end = map.generatedPositionFor({
    source: originalSource.url,
    line: Number.MAX_SAFE_INTEGER,
    column: Number.MAX_SAFE_INTEGER,
    bias: SourceMapConsumer.GREATEST_LOWER_BOUND
  });

  return {
    start,
    end
  };
}