| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const RawSource = require("./RawSource");
|
|---|
| 9 | const Source = require("./Source");
|
|---|
| 10 | const { getMap, getSourceAndMap } = require("./helpers/getFromStreamChunks");
|
|---|
| 11 | const streamChunks = require("./helpers/streamChunks");
|
|---|
| 12 |
|
|---|
| 13 | /** @typedef {import("./Source").HashLike} HashLike */
|
|---|
| 14 | /** @typedef {import("./Source").MapOptions} MapOptions */
|
|---|
| 15 | /** @typedef {import("./Source").RawSourceMap} RawSourceMap */
|
|---|
| 16 | /** @typedef {import("./Source").SourceAndMap} SourceAndMap */
|
|---|
| 17 | /** @typedef {import("./Source").SourceValue} SourceValue */
|
|---|
| 18 | /** @typedef {import("./helpers/getGeneratedSourceInfo").GeneratedSourceInfo} GeneratedSourceInfo */
|
|---|
| 19 | /** @typedef {import("./helpers/streamChunks").OnChunk} OnChunk */
|
|---|
| 20 | /** @typedef {import("./helpers/streamChunks").OnName} OnName */
|
|---|
| 21 | /** @typedef {import("./helpers/streamChunks").OnSource} OnSource */
|
|---|
| 22 | /** @typedef {import("./helpers/streamChunks").Options} Options */
|
|---|
| 23 |
|
|---|
| 24 | // `/\n/g` (no lookahead) lets V8's regex compiler take its
|
|---|
| 25 | // literal-character fast path; the previous `/\n(?=.|\s)/g` form
|
|---|
| 26 | // disabled it. Output stays identical because `buildPrefixed` strips
|
|---|
| 27 | // the spurious trailing prefix when the input ended with a newline.
|
|---|
| 28 | const NEWLINE_REGEX = /\n/g;
|
|---|
| 29 |
|
|---|
| 30 | /**
|
|---|
| 31 | * Prepend `prefix` and insert `prefix` after every newline that has
|
|---|
| 32 | * content following — the original `/\n(?=.|\s)/g` semantics, but
|
|---|
| 33 | * implemented as a fast-path regex + tail-strip.
|
|---|
| 34 | * @param {string} prefix prefix
|
|---|
| 35 | * @param {string} node underlying source string
|
|---|
| 36 | * @returns {string} prefixed string
|
|---|
| 37 | */
|
|---|
| 38 | const buildPrefixed = (prefix, node) => {
|
|---|
| 39 | if (prefix.length === 0) return node;
|
|---|
| 40 | const replaced = node.replace(NEWLINE_REGEX, `\n${prefix}`);
|
|---|
| 41 | const len = node.length;
|
|---|
| 42 | // `/\n/g` matches the trailing newline too, so the replace appended
|
|---|
| 43 | // a spurious prefix at the end. Trim it.
|
|---|
| 44 | if (len > 0 && node.charCodeAt(len - 1) === 10) {
|
|---|
| 45 | return prefix + replaced.slice(0, replaced.length - prefix.length);
|
|---|
| 46 | }
|
|---|
| 47 | return prefix + replaced;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | class PrefixSource extends Source {
|
|---|
| 51 | /**
|
|---|
| 52 | * @param {string} prefix prefix
|
|---|
| 53 | * @param {string | Buffer | Source} source source
|
|---|
| 54 | */
|
|---|
| 55 | constructor(prefix, source) {
|
|---|
| 56 | super();
|
|---|
| 57 | /**
|
|---|
| 58 | * @private
|
|---|
| 59 | * @type {string}
|
|---|
| 60 | */
|
|---|
| 61 | this._prefix = prefix;
|
|---|
| 62 | /**
|
|---|
| 63 | * @private
|
|---|
| 64 | * @type {Source}
|
|---|
| 65 | */
|
|---|
| 66 | this._source =
|
|---|
| 67 | typeof source === "string" || Buffer.isBuffer(source)
|
|---|
| 68 | ? new RawSource(source, true)
|
|---|
| 69 | : source;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | getPrefix() {
|
|---|
| 73 | return this._prefix;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | original() {
|
|---|
| 77 | return this._source;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * @returns {SourceValue} source
|
|---|
| 82 | */
|
|---|
| 83 | source() {
|
|---|
| 84 | return buildPrefixed(
|
|---|
| 85 | this._prefix,
|
|---|
| 86 | /** @type {string} */ (this._source.source()),
|
|---|
| 87 | );
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | // buffer() / buffers() / size() inherit from Source.prototype.
|
|---|
| 91 | // Source.buffer() does Buffer.from(this.source(), "utf8") — cheaper
|
|---|
| 92 | // in CodSpeed instruction count than any safe override we tried
|
|---|
| 93 | // (caching is unsafe with mutable child; a JS-side splice loop
|
|---|
| 94 | // regressed ~5x in instruction count). Speeding up source() via the
|
|---|
| 95 | // simpler regex above lifts buffer(), buffers(), size(), and any
|
|---|
| 96 | // other `this.source()`-using path with no override needed.
|
|---|
| 97 |
|
|---|
| 98 | /**
|
|---|
| 99 | * @param {MapOptions=} options map options
|
|---|
| 100 | * @returns {RawSourceMap | null} map
|
|---|
| 101 | */
|
|---|
| 102 | map(options) {
|
|---|
| 103 | return getMap(this, options);
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /**
|
|---|
| 107 | * @param {MapOptions=} options map options
|
|---|
| 108 | * @returns {SourceAndMap} source and map
|
|---|
| 109 | */
|
|---|
| 110 | sourceAndMap(options) {
|
|---|
| 111 | return getSourceAndMap(this, options);
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * @param {Options} options options
|
|---|
| 116 | * @param {OnChunk} onChunk called for each chunk of code
|
|---|
| 117 | * @param {OnSource} onSource called for each source
|
|---|
| 118 | * @param {OnName} onName called for each name
|
|---|
| 119 | * @returns {GeneratedSourceInfo} generated source info
|
|---|
| 120 | */
|
|---|
| 121 | streamChunks(options, onChunk, onSource, onName) {
|
|---|
| 122 | const prefix = this._prefix;
|
|---|
| 123 | const prefixOffset = prefix.length;
|
|---|
| 124 | const linesOnly = Boolean(options && options.columns === false);
|
|---|
| 125 | const { generatedLine, generatedColumn, source } = streamChunks(
|
|---|
| 126 | this._source,
|
|---|
| 127 | options,
|
|---|
| 128 | (
|
|---|
| 129 | chunk,
|
|---|
| 130 | generatedLine,
|
|---|
| 131 | generatedColumn,
|
|---|
| 132 | sourceIndex,
|
|---|
| 133 | originalLine,
|
|---|
| 134 | originalColumn,
|
|---|
| 135 | nameIndex,
|
|---|
| 136 | ) => {
|
|---|
| 137 | if (generatedColumn !== 0) {
|
|---|
| 138 | // In the middle of the line, we just adject the column
|
|---|
| 139 | generatedColumn += prefixOffset;
|
|---|
| 140 | } else if (chunk !== undefined) {
|
|---|
| 141 | // At the start of the line, when we have source content
|
|---|
| 142 | // add the prefix as generated mapping
|
|---|
| 143 | // (in lines only mode we just add it to the original mapping
|
|---|
| 144 | // for performance reasons)
|
|---|
| 145 | if (linesOnly || sourceIndex < 0) {
|
|---|
| 146 | chunk = prefix + chunk;
|
|---|
| 147 | } else if (prefixOffset > 0) {
|
|---|
| 148 | onChunk(prefix, generatedLine, generatedColumn, -1, -1, -1, -1);
|
|---|
| 149 | generatedColumn += prefixOffset;
|
|---|
| 150 | }
|
|---|
| 151 | } else if (!linesOnly) {
|
|---|
| 152 | // Without source content, we only need to adject the column info
|
|---|
| 153 | // expect in lines only mode where prefix is added to original mapping
|
|---|
| 154 | generatedColumn += prefixOffset;
|
|---|
| 155 | }
|
|---|
| 156 | onChunk(
|
|---|
| 157 | chunk,
|
|---|
| 158 | generatedLine,
|
|---|
| 159 | generatedColumn,
|
|---|
| 160 | sourceIndex,
|
|---|
| 161 | originalLine,
|
|---|
| 162 | originalColumn,
|
|---|
| 163 | nameIndex,
|
|---|
| 164 | );
|
|---|
| 165 | },
|
|---|
| 166 | onSource,
|
|---|
| 167 | onName,
|
|---|
| 168 | );
|
|---|
| 169 | return {
|
|---|
| 170 | generatedLine,
|
|---|
| 171 | generatedColumn:
|
|---|
| 172 | generatedColumn === 0
|
|---|
| 173 | ? 0
|
|---|
| 174 | : prefixOffset + /** @type {number} */ (generatedColumn),
|
|---|
| 175 | source: source !== undefined ? buildPrefixed(prefix, source) : undefined,
|
|---|
| 176 | };
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /**
|
|---|
| 180 | * @param {HashLike} hash hash
|
|---|
| 181 | * @returns {void}
|
|---|
| 182 | */
|
|---|
| 183 | updateHash(hash) {
|
|---|
| 184 | hash.update("PrefixSource");
|
|---|
| 185 | this._source.updateHash(hash);
|
|---|
| 186 | hash.update(this._prefix);
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 |
|
|---|
| 190 | module.exports = PrefixSource;
|
|---|