123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- const getGeneratedSourceInfo = require("./getGeneratedSourceInfo");
- const getSource = require("./getSource");
- const readMappings = require("./readMappings");
- const splitIntoLines = require("./splitIntoLines");
- const streamChunksOfSourceMapFull = (
- source,
- sourceMap,
- onChunk,
- onSource,
- onName
- ) => {
- const lines = splitIntoLines(source);
- if (lines.length === 0) {
- return {
- generatedLine: 1,
- generatedColumn: 0
- };
- }
- const { sources, sourcesContent, names, mappings } = sourceMap;
- for (let i = 0; i < sources.length; i++) {
- onSource(
- i,
- getSource(sourceMap, i),
- (sourcesContent && sourcesContent[i]) || undefined
- );
- }
- if (names) {
- for (let i = 0; i < names.length; i++) {
- onName(i, names[i]);
- }
- }
- const lastLine = lines[lines.length - 1];
- const lastNewLine = lastLine.endsWith("\n");
- const finalLine = lastNewLine ? lines.length + 1 : lines.length;
- const finalColumn = lastNewLine ? 0 : lastLine.length;
- let currentGeneratedLine = 1;
- let currentGeneratedColumn = 0;
- let mappingActive = false;
- let activeMappingSourceIndex = -1;
- let activeMappingOriginalLine = -1;
- let activeMappingOriginalColumn = -1;
- let activeMappingNameIndex = -1;
- const onMapping = (
- generatedLine,
- generatedColumn,
- sourceIndex,
- originalLine,
- originalColumn,
- nameIndex
- ) => {
- if (mappingActive && currentGeneratedLine <= lines.length) {
- let chunk;
- const mappingLine = currentGeneratedLine;
- const mappingColumn = currentGeneratedColumn;
- const line = lines[currentGeneratedLine - 1];
- if (generatedLine !== currentGeneratedLine) {
- chunk = line.slice(currentGeneratedColumn);
- currentGeneratedLine++;
- currentGeneratedColumn = 0;
- } else {
- chunk = line.slice(currentGeneratedColumn, generatedColumn);
- currentGeneratedColumn = generatedColumn;
- }
- if (chunk) {
- onChunk(
- chunk,
- mappingLine,
- mappingColumn,
- activeMappingSourceIndex,
- activeMappingOriginalLine,
- activeMappingOriginalColumn,
- activeMappingNameIndex
- );
- }
- mappingActive = false;
- }
- if (generatedLine > currentGeneratedLine && currentGeneratedColumn > 0) {
- if (currentGeneratedLine <= lines.length) {
- const chunk = lines[currentGeneratedLine - 1].slice(
- currentGeneratedColumn
- );
- onChunk(
- chunk,
- currentGeneratedLine,
- currentGeneratedColumn,
- -1,
- -1,
- -1,
- -1
- );
- }
- currentGeneratedLine++;
- currentGeneratedColumn = 0;
- }
- while (generatedLine > currentGeneratedLine) {
- if (currentGeneratedLine <= lines.length) {
- onChunk(
- lines[currentGeneratedLine - 1],
- currentGeneratedLine,
- 0,
- -1,
- -1,
- -1,
- -1
- );
- }
- currentGeneratedLine++;
- }
- if (generatedColumn > currentGeneratedColumn) {
- if (currentGeneratedLine <= lines.length) {
- const chunk = lines[currentGeneratedLine - 1].slice(
- currentGeneratedColumn,
- generatedColumn
- );
- onChunk(
- chunk,
- currentGeneratedLine,
- currentGeneratedColumn,
- -1,
- -1,
- -1,
- -1
- );
- }
- currentGeneratedColumn = generatedColumn;
- }
- if (
- sourceIndex >= 0 &&
- (generatedLine < finalLine ||
- (generatedLine === finalLine && generatedColumn < finalColumn))
- ) {
- mappingActive = true;
- activeMappingSourceIndex = sourceIndex;
- activeMappingOriginalLine = originalLine;
- activeMappingOriginalColumn = originalColumn;
- activeMappingNameIndex = nameIndex;
- }
- };
- readMappings(mappings, onMapping);
- onMapping(finalLine, finalColumn, -1, -1, -1, -1);
- return {
- generatedLine: finalLine,
- generatedColumn: finalColumn
- };
- };
- const streamChunksOfSourceMapLinesFull = (
- source,
- sourceMap,
- onChunk,
- onSource,
- _onName
- ) => {
- const lines = splitIntoLines(source);
- if (lines.length === 0) {
- return {
- generatedLine: 1,
- generatedColumn: 0
- };
- }
- const { sources, sourcesContent, mappings } = sourceMap;
- for (let i = 0; i < sources.length; i++) {
- onSource(
- i,
- getSource(sourceMap, i),
- (sourcesContent && sourcesContent[i]) || undefined
- );
- }
- let currentGeneratedLine = 1;
- const onMapping = (
- generatedLine,
- _generatedColumn,
- sourceIndex,
- originalLine,
- originalColumn,
- _nameIndex
- ) => {
- if (
- sourceIndex < 0 ||
- generatedLine < currentGeneratedLine ||
- generatedLine > lines.length
- ) {
- return;
- }
- while (generatedLine > currentGeneratedLine) {
- if (currentGeneratedLine <= lines.length) {
- onChunk(
- lines[currentGeneratedLine - 1],
- currentGeneratedLine,
- 0,
- -1,
- -1,
- -1,
- -1
- );
- }
- currentGeneratedLine++;
- }
- if (generatedLine <= lines.length) {
- onChunk(
- lines[generatedLine - 1],
- generatedLine,
- 0,
- sourceIndex,
- originalLine,
- originalColumn,
- -1
- );
- currentGeneratedLine++;
- }
- };
- readMappings(mappings, onMapping);
- for (; currentGeneratedLine <= lines.length; currentGeneratedLine++) {
- onChunk(
- lines[currentGeneratedLine - 1],
- currentGeneratedLine,
- 0,
- -1,
- -1,
- -1,
- -1
- );
- }
- const lastLine = lines[lines.length - 1];
- const lastNewLine = lastLine.endsWith("\n");
- const finalLine = lastNewLine ? lines.length + 1 : lines.length;
- const finalColumn = lastNewLine ? 0 : lastLine.length;
- return {
- generatedLine: finalLine,
- generatedColumn: finalColumn
- };
- };
- const streamChunksOfSourceMapFinal = (
- source,
- sourceMap,
- onChunk,
- onSource,
- onName
- ) => {
- const result = getGeneratedSourceInfo(source);
- const { generatedLine: finalLine, generatedColumn: finalColumn } = result;
- if (finalLine === 1 && finalColumn === 0) return result;
- const { sources, sourcesContent, names, mappings } = sourceMap;
- for (let i = 0; i < sources.length; i++) {
- onSource(
- i,
- getSource(sourceMap, i),
- (sourcesContent && sourcesContent[i]) || undefined
- );
- }
- if (names) {
- for (let i = 0; i < names.length; i++) {
- onName(i, names[i]);
- }
- }
- let mappingActiveLine = 0;
- const onMapping = (
- generatedLine,
- generatedColumn,
- sourceIndex,
- originalLine,
- originalColumn,
- nameIndex
- ) => {
- if (
- generatedLine >= finalLine &&
- (generatedColumn >= finalColumn || generatedLine > finalLine)
- ) {
- return;
- }
- if (sourceIndex >= 0) {
- onChunk(
- undefined,
- generatedLine,
- generatedColumn,
- sourceIndex,
- originalLine,
- originalColumn,
- nameIndex
- );
- mappingActiveLine = generatedLine;
- } else if (mappingActiveLine === generatedLine) {
- onChunk(undefined, generatedLine, generatedColumn, -1, -1, -1, -1);
- mappingActiveLine = 0;
- }
- };
- readMappings(mappings, onMapping);
- return result;
- };
- const streamChunksOfSourceMapLinesFinal = (
- source,
- sourceMap,
- onChunk,
- onSource,
- _onName
- ) => {
- const result = getGeneratedSourceInfo(source);
- const { generatedLine, generatedColumn } = result;
- if (generatedLine === 1 && generatedColumn === 0) {
- return {
- generatedLine: 1,
- generatedColumn: 0
- };
- }
- const { sources, sourcesContent, mappings } = sourceMap;
- for (let i = 0; i < sources.length; i++) {
- onSource(
- i,
- getSource(sourceMap, i),
- (sourcesContent && sourcesContent[i]) || undefined
- );
- }
- const finalLine = generatedColumn === 0 ? generatedLine - 1 : generatedLine;
- let currentGeneratedLine = 1;
- const onMapping = (
- generatedLine,
- _generatedColumn,
- sourceIndex,
- originalLine,
- originalColumn,
- _nameIndex
- ) => {
- if (
- sourceIndex >= 0 &&
- currentGeneratedLine <= generatedLine &&
- generatedLine <= finalLine
- ) {
- onChunk(
- undefined,
- generatedLine,
- 0,
- sourceIndex,
- originalLine,
- originalColumn,
- -1
- );
- currentGeneratedLine = generatedLine + 1;
- }
- };
- readMappings(mappings, onMapping);
- return result;
- };
- module.exports = (
- source,
- sourceMap,
- onChunk,
- onSource,
- onName,
- finalSource,
- columns
- ) => {
- if (columns) {
- return finalSource
- ? streamChunksOfSourceMapFinal(
- source,
- sourceMap,
- onChunk,
- onSource,
- onName
- )
- : streamChunksOfSourceMapFull(
- source,
- sourceMap,
- onChunk,
- onSource,
- onName
- );
- } else {
- return finalSource
- ? streamChunksOfSourceMapLinesFinal(
- source,
- sourceMap,
- onChunk,
- onSource,
- onName
- )
- : streamChunksOfSourceMapLinesFull(
- source,
- sourceMap,
- onChunk,
- onSource,
- onName
- );
- }
- };
|