[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 | "use strict";
|
---|
| 6 |
|
---|
| 7 | const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
|
---|
| 8 | const splitIntoLines = require("./helpers/splitIntoLines");
|
---|
| 9 | const getGeneratedSourceInfo = require("./helpers/getGeneratedSourceInfo");
|
---|
| 10 | const Source = require("./Source");
|
---|
| 11 | const splitIntoPotentialTokens = require("./helpers/splitIntoPotentialTokens");
|
---|
| 12 |
|
---|
| 13 | class OriginalSource extends Source {
|
---|
| 14 | constructor(value, name) {
|
---|
| 15 | super();
|
---|
| 16 | const isBuffer = Buffer.isBuffer(value);
|
---|
| 17 | this._value = isBuffer ? undefined : value;
|
---|
| 18 | this._valueAsBuffer = isBuffer ? value : undefined;
|
---|
| 19 | this._name = name;
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | getName() {
|
---|
| 23 | return this._name;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | source() {
|
---|
| 27 | if (this._value === undefined) {
|
---|
| 28 | this._value = this._valueAsBuffer.toString("utf-8");
|
---|
| 29 | }
|
---|
| 30 | return this._value;
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | buffer() {
|
---|
| 34 | if (this._valueAsBuffer === undefined) {
|
---|
| 35 | this._valueAsBuffer = Buffer.from(this._value, "utf-8");
|
---|
| 36 | }
|
---|
| 37 | return this._valueAsBuffer;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | map(options) {
|
---|
| 41 | return getMap(this, options);
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | sourceAndMap(options) {
|
---|
| 45 | return getSourceAndMap(this, options);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * @param {object} options options
|
---|
| 50 | * @param {function(string, number, number, number, number, number, number): void} onChunk called for each chunk of code
|
---|
| 51 | * @param {function(number, string, string)} onSource called for each source
|
---|
| 52 | * @param {function(number, string)} onName called for each name
|
---|
| 53 | * @returns {void}
|
---|
| 54 | */
|
---|
| 55 | streamChunks(options, onChunk, onSource, onName) {
|
---|
| 56 | if (this._value === undefined) {
|
---|
| 57 | this._value = this._valueAsBuffer.toString("utf-8");
|
---|
| 58 | }
|
---|
| 59 | onSource(0, this._name, this._value);
|
---|
| 60 | const finalSource = !!(options && options.finalSource);
|
---|
| 61 | if (!options || options.columns !== false) {
|
---|
| 62 | // With column info we need to read all lines and split them
|
---|
| 63 | const matches = splitIntoPotentialTokens(this._value);
|
---|
| 64 | let line = 1;
|
---|
| 65 | let column = 0;
|
---|
| 66 | if (matches !== null) {
|
---|
| 67 | for (const match of matches) {
|
---|
| 68 | const isEndOfLine = match.endsWith("\n");
|
---|
| 69 | if (isEndOfLine && match.length === 1) {
|
---|
| 70 | if (!finalSource) onChunk(match, line, column, -1, -1, -1, -1);
|
---|
| 71 | } else {
|
---|
| 72 | const chunk = finalSource ? undefined : match;
|
---|
| 73 | onChunk(chunk, line, column, 0, line, column, -1);
|
---|
| 74 | }
|
---|
| 75 | if (isEndOfLine) {
|
---|
| 76 | line++;
|
---|
| 77 | column = 0;
|
---|
| 78 | } else {
|
---|
| 79 | column += match.length;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 | return {
|
---|
| 84 | generatedLine: line,
|
---|
| 85 | generatedColumn: column,
|
---|
| 86 | source: finalSource ? this._value : undefined
|
---|
| 87 | };
|
---|
| 88 | } else if (finalSource) {
|
---|
| 89 | // Without column info and with final source we only
|
---|
| 90 | // need meta info to generate mapping
|
---|
| 91 | const result = getGeneratedSourceInfo(this._value);
|
---|
| 92 | const { generatedLine, generatedColumn } = result;
|
---|
| 93 | if (generatedColumn === 0) {
|
---|
| 94 | for (let line = 1; line < generatedLine; line++)
|
---|
| 95 | onChunk(undefined, line, 0, 0, line, 0, -1);
|
---|
| 96 | } else {
|
---|
| 97 | for (let line = 1; line <= generatedLine; line++)
|
---|
| 98 | onChunk(undefined, line, 0, 0, line, 0, -1);
|
---|
| 99 | }
|
---|
| 100 | return result;
|
---|
| 101 | } else {
|
---|
| 102 | // Without column info, but also without final source
|
---|
| 103 | // we need to split source by lines
|
---|
| 104 | let line = 1;
|
---|
| 105 | const matches = splitIntoLines(this._value);
|
---|
| 106 | let match;
|
---|
| 107 | for (match of matches) {
|
---|
| 108 | onChunk(finalSource ? undefined : match, line, 0, 0, line, 0, -1);
|
---|
| 109 | line++;
|
---|
| 110 | }
|
---|
| 111 | return matches.length === 0 || match.endsWith("\n")
|
---|
| 112 | ? {
|
---|
| 113 | generatedLine: matches.length + 1,
|
---|
| 114 | generatedColumn: 0,
|
---|
| 115 | source: finalSource ? this._value : undefined
|
---|
| 116 | }
|
---|
| 117 | : {
|
---|
| 118 | generatedLine: matches.length,
|
---|
| 119 | generatedColumn: match.length,
|
---|
| 120 | source: finalSource ? this._value : undefined
|
---|
| 121 | };
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | updateHash(hash) {
|
---|
| 126 | if (this._valueAsBuffer === undefined) {
|
---|
| 127 | this._valueAsBuffer = Buffer.from(this._value, "utf-8");
|
---|
| 128 | }
|
---|
| 129 | hash.update("OriginalSource");
|
---|
| 130 | hash.update(this._valueAsBuffer);
|
---|
| 131 | hash.update(this._name || "");
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | module.exports = OriginalSource;
|
---|