Skip to content

Commit d3cf462

Browse files
author
Simon Holthausen
committed
DEM UNDERSCOREZZ
1 parent fead372 commit d3cf462

File tree

2 files changed

+115
-105
lines changed

2 files changed

+115
-105
lines changed

src/mapping.js

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,68 @@ import { decode } from 'sourcemap-codec';
22

33
class GeneratedFragmentMapper {
44
constructor(
5-
generatedCode,
6-
tagInfo,
5+
generated_code,
6+
tag_info,
77
) {
8-
this.generatedCode = generatedCode;
9-
this.tagInfo = tagInfo;
8+
this.generated_code = generated_code;
9+
this.tag_info = tag_info;
1010
}
1111

12-
getPositionRelativeToFragment(positionRelativeToFile) {
13-
const fragmentOffset = this.offsetInFragment(offsetAt(positionRelativeToFile, this.generatedCode));
14-
return positionAt(fragmentOffset, this.tagInfo.generatedContent);
12+
get_position_relative_to_fragment(positionRelativeToFile) {
13+
const fragment_offset = this.offset_in_fragment(offset_at(positionRelativeToFile, this.generated_code));
14+
return position_at(fragment_offset, this.tag_info.generated_content);
1515
}
1616

17-
offsetInFragment(offset) {
18-
return offset - this.tagInfo.generatedStart
17+
offset_in_fragment(offset) {
18+
return offset - this.tag_info.generated_start
1919
}
2020
}
2121

2222
class OriginalFragmentMapper {
2323
constructor(
24-
originalCode,
25-
tagInfo,
24+
original_code,
25+
tag_info,
2626
) {
27-
this.originalCode = originalCode;
28-
this.tagInfo = tagInfo;
27+
this.original_code = original_code;
28+
this.tag_info = tag_info;
2929
}
3030

31-
getPositionRelativeToFile(positionRelativeToFragment) {
32-
const parentOffset = this.offsetInParent(offsetAt(positionRelativeToFragment, this.tagInfo.originalContent));
33-
return positionAt(parentOffset, this.originalCode);
31+
get_position_relative_to_file(positionRelativeToFragment) {
32+
const parent_offset = this.offset_in_parent(offset_at(positionRelativeToFragment, this.tag_info.original_content));
33+
return position_at(parent_offset, this.original_code);
3434
}
3535

36-
offsetInParent(offset) {
37-
return this.tagInfo.originalStart + offset;
36+
offset_in_parent(offset) {
37+
return this.tag_info.original_start + offset;
3838
}
3939
}
4040

4141
class SourceMapper {
42-
constructor(rawSourceMap) {
43-
this.rawSourceMap = rawSourceMap;
42+
constructor(raw_source_map) {
43+
this.raw_source_map = raw_source_map;
4444
}
4545

46-
getOriginalPosition(generatedPosition) {
47-
if (generatedPosition.line < 0) {
46+
getOriginalPosition(generated_position) {
47+
if (generated_position.line < 0) {
4848
return { line: -1, column: -1 };
4949
}
5050

5151
// Lazy-load
5252
if (!this.decoded) {
53-
this.decoded = decode(JSON.parse(this.rawSourceMap).mappings);
53+
this.decoded = decode(JSON.parse(this.raw_source_map).mappings);
5454
}
5555

56-
let line = generatedPosition.line;
57-
let column = generatedPosition.column;
56+
let line = generated_position.line;
57+
let column = generated_position.column;
5858

59-
let lineMatch = this.decoded[generatedPosition.line];
60-
while (line >= 0 && (!lineMatch || !lineMatch.length)) {
59+
let line_match = this.decoded[generated_position.line];
60+
while (line >= 0 && (!line_match || !line_match.length)) {
6161
line -= 1;
62-
lineMatch = this.decoded[generatedPosition];
63-
if (lineMatch && lineMatch.length) {
62+
line_match = this.decoded[generated_position];
63+
if (line_match && line_match.length) {
6464
return {
65-
line: lineMatch[lineMatch.length - 1][2],
66-
column: lineMatch[lineMatch.length - 1][3]
65+
line: line_match[line_match.length - 1][2],
66+
column: line_match[line_match.length - 1][3]
6767
};
6868
}
6969
}
@@ -72,66 +72,66 @@ class SourceMapper {
7272
return { line: -1, column: -1 };
7373
}
7474

75-
const columnMatch = lineMatch.find((col, idx) =>
76-
idx + 1 === lineMatch.length ||
77-
(col[0] <= column && lineMatch[idx + 1][0] > column)
75+
const column_match = line_match.find((col, idx) =>
76+
idx + 1 === line_match.length ||
77+
(col[0] <= column && line_match[idx + 1][0] > column)
7878
);
7979

8080
return {
81-
line: columnMatch[2],
82-
column: columnMatch[3],
81+
line: column_match[2],
82+
column: column_match[3],
8383
};
8484
}
8585
}
8686

8787
export class DocumentMapper {
88-
constructor(originalCode, generatedCode, diffs) {
89-
this.originalCode = originalCode;
90-
this.generatedCode = generatedCode;
88+
constructor(original_code, generated_code, diffs) {
89+
this.original_code = original_code;
90+
this.generated_code = generated_code;
9191
this.diffs = diffs;
9292
this.mappers = diffs.map(diff => {
9393
return {
94-
start: diff.generatedStart,
95-
end: diff.generatedEnd,
94+
start: diff.generated_start,
95+
end: diff.generated_end,
9696
diff: diff.diff,
97-
generatedFragmentMapper: new GeneratedFragmentMapper(generatedCode, diff),
98-
sourceMapper: new SourceMapper(diff.map),
99-
originalFragmentMapper: new OriginalFragmentMapper(originalCode, diff)
97+
generated_fragment_mapper: new GeneratedFragmentMapper(generated_code, diff),
98+
source_mapper: new SourceMapper(diff.map),
99+
original_fragment_mapper: new OriginalFragmentMapper(original_code, diff)
100100
}
101101
});
102102
}
103103

104-
getOriginalPosition(generatedPosition) {
105-
generatedPosition = { line: generatedPosition.line - 1, column: generatedPosition.column };
106-
const offset = offsetAt(generatedPosition, this.generatedCode);
107-
let originalOffset = offset;
104+
get_original_position(generated_position) {
105+
generated_position = { line: generated_position.line - 1, column: generated_position.column };
106+
const offset = offset_at(generated_position, this.generated_code);
107+
let original_offset = offset;
108108
for (const mapper of this.mappers) {
109109
if (offset >= mapper.start && offset <= mapper.end) {
110-
return this.map(mapper, generatedPosition);
110+
return this.map(mapper, generated_position);
111111
}
112112
if (offset > mapper.end) {
113-
originalOffset -= mapper.diff;
113+
original_offset -= mapper.diff;
114114
}
115115
}
116-
const originalPosition = positionAt(originalOffset, this.originalCode);
117-
return this.toESLintPosition(originalPosition);
116+
const original_position = position_at(original_offset, this.original_code);
117+
return this.to_ESLint_position(original_position);
118118
}
119119

120120
map(mapper, generatedPosition) {
121121
// Map the position to be relative to the transpiled fragment
122-
const positionInTranspiledFragment = mapper.generatedFragmentMapper.getPositionRelativeToFragment(
122+
const position_in_transpiled_fragment = mapper.generated_fragment_mapper.get_position_relative_to_fragment(
123123
generatedPosition
124124
);
125125
// Map the position, using the sourcemap, to the original position in the source fragment
126-
const positionInOriginalFragment = mapper.sourceMapper.getOriginalPosition(
127-
positionInTranspiledFragment
126+
const position_in_original_fragment = mapper.source_mapper.getOriginalPosition(
127+
position_in_transpiled_fragment
128128
);
129129
// Map the position to be in the original fragment's parent
130-
const originalPosition = mapper.originalFragmentMapper.getPositionRelativeToFile(positionInOriginalFragment);
131-
return this.toESLintPosition(originalPosition);
130+
const original_position = mapper.original_fragment_mapper.get_position_relative_to_file(position_in_original_fragment);
131+
return this.to_ESLint_position(original_position);
132132
}
133133

134-
toESLintPosition(position) {
134+
to_ESLint_position(position) {
135135
// ESLint line/column is 1-based
136136
return { line: position.line + 1, column: position.column + 1 };
137137
}
@@ -143,35 +143,35 @@ export class DocumentMapper {
143143
* @param position Line and character position
144144
* @param text The text for which the offset should be retrived
145145
*/
146-
function offsetAt(position, text) {
147-
const lineOffsets = getLineOffsets(text);
146+
function offset_at(position, text) {
147+
const line_offsets = get_line_offsets(text);
148148

149-
if (position.line >= lineOffsets.length) {
149+
if (position.line >= line_offsets.length) {
150150
return text.length;
151151
} else if (position.line < 0) {
152152
return 0;
153153
}
154154

155-
const lineOffset = lineOffsets[position.line];
156-
const nextLineOffset =
157-
position.line + 1 < lineOffsets.length ? lineOffsets[position.line + 1] : text.length;
155+
const line_offset = line_offsets[position.line];
156+
const next_line_offset =
157+
position.line + 1 < line_offsets.length ? line_offsets[position.line + 1] : text.length;
158158

159-
return clamp(nextLineOffset, lineOffset, lineOffset + position.column);
159+
return clamp(next_line_offset, line_offset, line_offset + position.column);
160160
}
161161

162-
function positionAt(offset, text) {
162+
function position_at(offset, text) {
163163
offset = clamp(offset, 0, text.length);
164164

165-
const lineOffsets = getLineOffsets(text);
165+
const line_offsets = get_line_offsets(text);
166166
let low = 0;
167-
let high = lineOffsets.length;
167+
let high = line_offsets.length;
168168
if (high === 0) {
169-
return Position.create(0, offset);
169+
return { line: 0, column: offset };
170170
}
171171

172172
while (low < high) {
173173
const mid = Math.floor((low + high) / 2);
174-
if (lineOffsets[mid] > offset) {
174+
if (line_offsets[mid] > offset) {
175175
high = mid;
176176
} else {
177177
low = mid + 1;
@@ -181,30 +181,30 @@ function positionAt(offset, text) {
181181
// low is the least x for which the line offset is larger than the current offset
182182
// or array.length if no line offset is larger than the current offset
183183
const line = low - 1;
184-
return { line, column: offset - lineOffsets[line] };
184+
return { line, column: offset - line_offsets[line] };
185185
}
186186

187-
function getLineOffsets(text) {
188-
const lineOffsets = [];
189-
let isLineStart = true;
187+
function get_line_offsets(text) {
188+
const line_offsets = [];
189+
let is_line_start = true;
190190

191191
for (let i = 0; i < text.length; i++) {
192-
if (isLineStart) {
193-
lineOffsets.push(i);
194-
isLineStart = false;
192+
if (is_line_start) {
193+
line_offsets.push(i);
194+
is_line_start = false;
195195
}
196196
const ch = text.charAt(i);
197-
isLineStart = ch === '\r' || ch === '\n';
197+
is_line_start = ch === '\r' || ch === '\n';
198198
if (ch === '\r' && i + 1 < text.length && text.charAt(i + 1) === '\n') {
199199
i++;
200200
}
201201
}
202202

203-
if (isLineStart && text.length > 0) {
204-
lineOffsets.push(text.length);
203+
if (is_line_start && text.length > 0) {
204+
line_offsets.push(text.length);
205205
}
206206

207-
return lineOffsets;
207+
return line_offsets;
208208
}
209209

210210
function clamp(num, min, max) {

0 commit comments

Comments
 (0)