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