How to use the source-map.SourceMapConsumer.with 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 angular / angular-cli / packages / angular_devkit / build_angular / src / utils / process-bundle.ts View on Github external
async function mergeSourceMapsFast(first: RawSourceMap, second: RawSourceMap) {
  const sourceRoot = first.sourceRoot;
  const generator = new SourceMapGenerator();

  // sourcemap package adds the sourceRoot to all position source paths if not removed
  delete first.sourceRoot;

  await SourceMapConsumer.with(first, null, originalConsumer => {
    return SourceMapConsumer.with(second, null, newConsumer => {
      newConsumer.eachMapping(mapping => {
        if (mapping.originalLine === null) {
          return;
        }
        const originalPosition = originalConsumer.originalPositionFor({
          line: mapping.originalLine,
          column: mapping.originalColumn,
        });
        if (
          originalPosition.line === null ||
          originalPosition.column === null ||
          originalPosition.source === null
        ) {
          return;
        }
github unblu / js-source-extractor / src / js-source-extractor.ts View on Github external
if (resourceUrl === null) {
            throw Error('No or invalid sourceMap and no or invalid resourceUrl provided');
        }

        rawMap = await loadMap(resourceUrl);
    }

    let srcIncludeEff: RegExp;
    if (typeof srcInclude !== 'undefined') {
        srcIncludeEff = srcInclude;
    } else {
        srcIncludeEff = /.*/;
    }


    await SourceMapConsumer.with(rawMap, null, (consumer) => {
        console.info(util.format("Source map references %d source files", consumer.sources.length));

        consumer.sources.forEach((sourceRef) => {
            if (!srcIncludeEff.test(sourceRef)) {
                console.log(util.format('Skipping %s: does not match include pattern'), sourceRef);
                return;
            }

            if (typeof srcExclude !== 'undefined' && srcExclude.test(sourceRef)) {
                console.log(util.format('Skipping %s: matched exclude pattern', sourceRef));
                return;
            }

            const sourceRefUrl = url.parse(sourceRef);
            let pathName = sourceRefUrl.pathname + (sourceRefUrl.query ? sourceRefUrl.query : '') + (sourceRefUrl.hash ? sourceRefUrl.hash : '');
            let fileName = pathName;
github roman01la / cljs-source-mapped-stack-traces / server / map-stack-trace.js View on Github external
linesInFrame,
  { publicDir }
) {
  const absFilePath = path.join(
    publicDir,
    file
      .split("/")
      .slice(3)
      .join("/")
      .replace(/\?.*$/, "")
  );
  const fileContents = fs.readFileSync(absFilePath, "utf8");
  const smPath = getSourceMapPath({ source: fileContents, file: absFilePath });
  const sm = JSON.parse(fs.readFileSync(smPath, "utf8"));

  const frame = await SourceMapConsumer.with(sm, null, consumer => {
    const frame = consumer.originalPositionFor({ line, column });
    frame._source = frame.source;
    if (!consumer.file.endsWith(".js.map")) {
      frame.source = smPath.replace(/\.js\.map$/, ".cljs");
      frame._source = frame.source.replace(publicDir, "");
    } else {
      frame._source = frame.source;
      frame.source = path.join(publicDir, frame.source);
    }

    return frame;
  });

  const shouldInclude =
    excludes.length === 0
      ? true
github GeekyAnts / vue-native-core / src / platforms / vue-native / scripts / transformerPlugin.js View on Github external
function sourceMapAstInPlace(tsMap, babelAst) {
  return SourceMapConsumer.with(
    tsMap,
    null,
    (consumer) => {
      traverse.cheap(babelAst, node => {
        if (node.loc) {
          const originalStart = consumer.originalPositionFor(node.loc.start);
          if (originalStart.line) {
            node.loc.start.line = originalStart.line;
            node.loc.start.column = originalStart.column;
          }
          const originalEnd = consumer.originalPositionFor(node.loc.end);
          if (originalEnd.line) {
            node.loc.end.line = originalEnd.line;
            node.loc.end.column = originalEnd.column;
          }
        }
github neo-one-suite / neo-one / packages / neo-one-client-switch / src / node / processTrace.ts View on Github external
}

        mutableTraceLines.push({
          line,
          file: source,
          token: sourceValue,
          column: column === null ? undefined : column,
        });
      }

      return mutableTraceLines;
    }

    const [currentAddress, currentSourceMap] = remainingSourceMaps[0];

    return SourceMapConsumer.with(currentSourceMap, undefined, async (consumer) =>
      withSourceMaps(
        {
          ...consumers,
          [currentAddress]: consumer,
        },
        remainingSourceMaps.slice(1),
      ),
    );
  }
github neo-one-suite / neo-one / packages / neo-one-client-switch / src / common / processTrace.ts View on Github external
}

        mutableTraceLines.push({
          line,
          file: onlyFileName ? path.basename(source) : source,
          token: sourceValue,
          column: column === null ? undefined : column,
        });
      }

      return mutableTraceLines;
    }

    const [currentAddress, currentSourceMap] = remainingSourceMaps[0];

    return SourceMapConsumer.with(currentSourceMap, undefined, async (consumer) =>
      withSourceMaps(
        {
          ...consumers,
          [currentAddress]: consumer,
        },
        remainingSourceMaps.slice(1),
      ),
    );
  }
github rokucommunity / vscode-brightscript-language / src / FileUtils.ts View on Github external
public async getSourceLocationFromSourcemap(filePathAbsolute: string, debuggerLineNumber: number, debuggerColumnNumber: number = 0): Promise {
        //look for a source map for this file
        let sourceMapPath = `${filePathAbsolute}.map`;

        //if we have a source map, use it
        if (await fsExtra.pathExists(sourceMapPath)) {
            let sourceMapText = (await fsExtra.readFile(sourceMapPath)).toString();
            let sourceMap = JSON.parse(sourceMapText);
            let position = await SourceMapConsumer.with(sourceMap, null, (consumer) => {
                return consumer.originalPositionFor({
                    line: debuggerLineNumber,
                    column: debuggerColumnNumber
                });
            });
            //get the path to the folder this source map lives in
            let folderPathForStagingFile = path.dirname(sourceMapPath);
            //get the absolute path to the source file
            let sourcePathAbsolute = path.resolve(folderPathForStagingFile, position.source);
            return {
                columnIndex: position.column,
                lineNumber: position.line,
                pathAbsolute: sourcePathAbsolute
            };
        }
    }
github UnwrittenFun / svelte-language-server / src / plugins / SveltePlugin.ts View on Github external
for (const fragment of preprocessor.fragments) {
        const newDiagnostics: Diagnostic[] = [];
        const fragmentDiagnostics: Diagnostic[] = [];
        for (let diag of diagnostics) {
            if (fragment.transpiled.isInFragment(diag.range.start)) {
                fragmentDiagnostics.push(diag);
            } else {
                newDiagnostics.push(diag);
            }
        }
        diagnostics = newDiagnostics;
        if (fragmentDiagnostics.length === 0) {
            continue;
        }

        await SourceMapConsumer.with(fragment.map, null, consumer => {
            for (const diag of fragmentDiagnostics) {
                diag.range = {
                    start: mapFragmentPositionBySourceMap(
                        fragment.source,
                        fragment.transpiled,
                        consumer,
                        diag.range.start,
                    ),
                    end: mapFragmentPositionBySourceMap(
                        fragment.source,
                        fragment.transpiled,
                        consumer,
                        diag.range.end,
                    ),
                };
            }
github source-academy / js-slang / src / index.ts View on Github external
value: sandboxedEval(transpiled)
      } as Result)
    } catch (error) {
      if (error instanceof RuntimeSourceError || error instanceof ExceptionError) {
        context.errors.push(error)
        return resolvedErrorPromise
      }
      const errorStack = error.stack
      const match = /:(\d+):(\d+)/.exec(errorStack)
      if (match === null) {
        context.errors.push(new ExceptionError(error, UNKNOWN_LOCATION))
        return resolvedErrorPromise
      }
      const line = Number(match![1])
      const column = Number(match![2])
      return SourceMapConsumer.with(
        line === 1 ? lastStatementSourceMapJson! : sourceMapJson!,
        null,
        consumer => {
          const { line: originalLine, column: originalColumn, name } = consumer.originalPositionFor(
            {
              line,
              column
            }
          )
          context.errors.push(
            convertNativeErrorToSourceError(error, originalLine, originalColumn, name)
          )
          return resolvedErrorPromise
        }
      )
    }