|
| 1 | +import { decode } from 'sourcemap-codec'; |
| 2 | + |
| 3 | +class GeneratedFragmentMapper { |
| 4 | + constructor(generated_code, diff) { |
| 5 | + this.generated_code = generated_code; |
| 6 | + this.diff = diff; |
| 7 | + } |
| 8 | + |
| 9 | + get_position_relative_to_fragment(position_relative_to_file) { |
| 10 | + const fragment_offset = this.offset_in_fragment(offset_at(position_relative_to_file, this.generated_code)); |
| 11 | + return position_at(fragment_offset, this.diff.generated_content); |
| 12 | + } |
| 13 | + |
| 14 | + offset_in_fragment(offset) { |
| 15 | + return offset - this.diff.generated_start |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +class OriginalFragmentMapper { |
| 20 | + constructor(original_code, diff) { |
| 21 | + this.original_code = original_code; |
| 22 | + this.diff = diff; |
| 23 | + } |
| 24 | + |
| 25 | + get_position_relative_to_file(position_relative_to_fragment) { |
| 26 | + const parent_offset = this.offset_in_parent(offset_at(position_relative_to_fragment, this.diff.original_content)); |
| 27 | + return position_at(parent_offset, this.original_code); |
| 28 | + } |
| 29 | + |
| 30 | + offset_in_parent(offset) { |
| 31 | + return this.diff.original_start + offset; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +class SourceMapper { |
| 36 | + constructor(raw_source_map) { |
| 37 | + this.raw_source_map = raw_source_map; |
| 38 | + } |
| 39 | + |
| 40 | + get_original_position(generated_position) { |
| 41 | + if (generated_position.line < 0) { |
| 42 | + return { line: -1, column: -1 }; |
| 43 | + } |
| 44 | + |
| 45 | + // Lazy-load |
| 46 | + if (!this.decoded) { |
| 47 | + this.decoded = decode(JSON.parse(this.raw_source_map).mappings); |
| 48 | + } |
| 49 | + |
| 50 | + let line = generated_position.line; |
| 51 | + let column = generated_position.column; |
| 52 | + |
| 53 | + let line_match = this.decoded[line]; |
| 54 | + while (line >= 0 && (!line_match || !line_match.length)) { |
| 55 | + line -= 1; |
| 56 | + line_match = this.decoded[line]; |
| 57 | + if (line_match && line_match.length) { |
| 58 | + return { |
| 59 | + line: line_match[line_match.length - 1][2], |
| 60 | + column: line_match[line_match.length - 1][3] |
| 61 | + }; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (line < 0) { |
| 66 | + return { line: -1, column: -1 }; |
| 67 | + } |
| 68 | + |
| 69 | + const column_match = line_match.find((col, idx) => |
| 70 | + idx + 1 === line_match.length || |
| 71 | + (col[0] <= column && line_match[idx + 1][0] > column) |
| 72 | + ); |
| 73 | + |
| 74 | + return { |
| 75 | + line: column_match[2], |
| 76 | + column: column_match[3], |
| 77 | + }; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +export class DocumentMapper { |
| 82 | + constructor(original_code, generated_code, diffs) { |
| 83 | + this.original_code = original_code; |
| 84 | + this.generated_code = generated_code; |
| 85 | + this.diffs = diffs; |
| 86 | + this.mappers = diffs.map(diff => { |
| 87 | + return { |
| 88 | + start: diff.generated_start, |
| 89 | + end: diff.generated_end, |
| 90 | + diff: diff.diff, |
| 91 | + generated_fragment_mapper: new GeneratedFragmentMapper(generated_code, diff), |
| 92 | + source_mapper: new SourceMapper(diff.map), |
| 93 | + original_fragment_mapper: new OriginalFragmentMapper(original_code, diff) |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + get_original_position(generated_position) { |
| 99 | + generated_position = { line: generated_position.line - 1, column: generated_position.column }; |
| 100 | + const offset = offset_at(generated_position, this.generated_code); |
| 101 | + let original_offset = offset; |
| 102 | + for (const mapper of this.mappers) { |
| 103 | + if (offset >= mapper.start && offset <= mapper.end) { |
| 104 | + return this.map(mapper, generated_position); |
| 105 | + } |
| 106 | + if (offset > mapper.end) { |
| 107 | + original_offset -= mapper.diff; |
| 108 | + } |
| 109 | + } |
| 110 | + const original_position = position_at(original_offset, this.original_code); |
| 111 | + return this.to_ESLint_position(original_position); |
| 112 | + } |
| 113 | + |
| 114 | + map(mapper, generated_position) { |
| 115 | + // Map the position to be relative to the transpiled fragment |
| 116 | + const position_in_transpiled_fragment = mapper.generated_fragment_mapper.get_position_relative_to_fragment( |
| 117 | + generated_position |
| 118 | + ); |
| 119 | + // Map the position, using the sourcemap, to the original position in the source fragment |
| 120 | + const position_in_original_fragment = mapper.source_mapper.get_original_position( |
| 121 | + position_in_transpiled_fragment |
| 122 | + ); |
| 123 | + // Map the position to be in the original fragment's parent |
| 124 | + const original_position = mapper.original_fragment_mapper.get_position_relative_to_file(position_in_original_fragment); |
| 125 | + return this.to_ESLint_position(original_position); |
| 126 | + } |
| 127 | + |
| 128 | + to_ESLint_position(position) { |
| 129 | + // ESLint line/column is 1-based |
| 130 | + return { line: position.line + 1, column: position.column + 1 }; |
| 131 | + } |
| 132 | + |
| 133 | +} |
| 134 | + |
| 135 | +/** |
| 136 | + * Get the offset of the line and character position |
| 137 | + * @param position Line and character position |
| 138 | + * @param text The text for which the offset should be retrieved |
| 139 | + */ |
| 140 | +function offset_at(position, text) { |
| 141 | + const line_offsets = get_line_offsets(text); |
| 142 | + |
| 143 | + if (position.line >= line_offsets.length) { |
| 144 | + return text.length; |
| 145 | + } else if (position.line < 0) { |
| 146 | + return 0; |
| 147 | + } |
| 148 | + |
| 149 | + const line_offset = line_offsets[position.line]; |
| 150 | + const next_line_offset = |
| 151 | + position.line + 1 < line_offsets.length ? line_offsets[position.line + 1] : text.length; |
| 152 | + |
| 153 | + return clamp(next_line_offset, line_offset, line_offset + position.column); |
| 154 | +} |
| 155 | + |
| 156 | +function position_at(offset, text) { |
| 157 | + offset = clamp(offset, 0, text.length); |
| 158 | + |
| 159 | + const line_offsets = get_line_offsets(text); |
| 160 | + let low = 0; |
| 161 | + let high = line_offsets.length; |
| 162 | + if (high === 0) { |
| 163 | + return { line: 0, column: offset }; |
| 164 | + } |
| 165 | + |
| 166 | + while (low < high) { |
| 167 | + const mid = Math.floor((low + high) / 2); |
| 168 | + if (line_offsets[mid] > offset) { |
| 169 | + high = mid; |
| 170 | + } else { |
| 171 | + low = mid + 1; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // low is the least x for which the line offset is larger than the current offset |
| 176 | + // or array.length if no line offset is larger than the current offset |
| 177 | + const line = low - 1; |
| 178 | + return { line, column: offset - line_offsets[line] }; |
| 179 | +} |
| 180 | + |
| 181 | +function get_line_offsets(text) { |
| 182 | + const line_offsets = []; |
| 183 | + let is_line_start = true; |
| 184 | + |
| 185 | + for (let i = 0; i < text.length; i++) { |
| 186 | + if (is_line_start) { |
| 187 | + line_offsets.push(i); |
| 188 | + is_line_start = false; |
| 189 | + } |
| 190 | + const ch = text.charAt(i); |
| 191 | + is_line_start = ch === '\r' || ch === '\n'; |
| 192 | + if (ch === '\r' && i + 1 < text.length && text.charAt(i + 1) === '\n') { |
| 193 | + i++; |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if (is_line_start && text.length > 0) { |
| 198 | + line_offsets.push(text.length); |
| 199 | + } |
| 200 | + |
| 201 | + return line_offsets; |
| 202 | +} |
| 203 | + |
| 204 | +function clamp(num, min, max) { |
| 205 | + return Math.max(min, Math.min(max, num)); |
| 206 | +} |
0 commit comments