@@ -2,68 +2,68 @@ import { decode } from 'sourcemap-codec';
2
2
3
3
class GeneratedFragmentMapper {
4
4
constructor (
5
- generatedCode ,
6
- tagInfo ,
5
+ generated_code ,
6
+ tag_info ,
7
7
) {
8
- this . generatedCode = generatedCode ;
9
- this . tagInfo = tagInfo ;
8
+ this . generated_code = generated_code ;
9
+ this . tag_info = tag_info ;
10
10
}
11
11
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 ) ;
15
15
}
16
16
17
- offsetInFragment ( offset ) {
18
- return offset - this . tagInfo . generatedStart
17
+ offset_in_fragment ( offset ) {
18
+ return offset - this . tag_info . generated_start
19
19
}
20
20
}
21
21
22
22
class OriginalFragmentMapper {
23
23
constructor (
24
- originalCode ,
25
- tagInfo ,
24
+ original_code ,
25
+ tag_info ,
26
26
) {
27
- this . originalCode = originalCode ;
28
- this . tagInfo = tagInfo ;
27
+ this . original_code = original_code ;
28
+ this . tag_info = tag_info ;
29
29
}
30
30
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 ) ;
34
34
}
35
35
36
- offsetInParent ( offset ) {
37
- return this . tagInfo . originalStart + offset ;
36
+ offset_in_parent ( offset ) {
37
+ return this . tag_info . original_start + offset ;
38
38
}
39
39
}
40
40
41
41
class SourceMapper {
42
- constructor ( rawSourceMap ) {
43
- this . rawSourceMap = rawSourceMap ;
42
+ constructor ( raw_source_map ) {
43
+ this . raw_source_map = raw_source_map ;
44
44
}
45
45
46
- getOriginalPosition ( generatedPosition ) {
47
- if ( generatedPosition . line < 0 ) {
46
+ getOriginalPosition ( generated_position ) {
47
+ if ( generated_position . line < 0 ) {
48
48
return { line : - 1 , column : - 1 } ;
49
49
}
50
50
51
51
// Lazy-load
52
52
if ( ! this . decoded ) {
53
- this . decoded = decode ( JSON . parse ( this . rawSourceMap ) . mappings ) ;
53
+ this . decoded = decode ( JSON . parse ( this . raw_source_map ) . mappings ) ;
54
54
}
55
55
56
- let line = generatedPosition . line ;
57
- let column = generatedPosition . column ;
56
+ let line = generated_position . line ;
57
+ let column = generated_position . column ;
58
58
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 ) ) {
61
61
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 ) {
64
64
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 ]
67
67
} ;
68
68
}
69
69
}
@@ -72,66 +72,66 @@ class SourceMapper {
72
72
return { line : - 1 , column : - 1 } ;
73
73
}
74
74
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 )
78
78
) ;
79
79
80
80
return {
81
- line : columnMatch [ 2 ] ,
82
- column : columnMatch [ 3 ] ,
81
+ line : column_match [ 2 ] ,
82
+ column : column_match [ 3 ] ,
83
83
} ;
84
84
}
85
85
}
86
86
87
87
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 ;
91
91
this . diffs = diffs ;
92
92
this . mappers = diffs . map ( diff => {
93
93
return {
94
- start : diff . generatedStart ,
95
- end : diff . generatedEnd ,
94
+ start : diff . generated_start ,
95
+ end : diff . generated_end ,
96
96
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 )
100
100
}
101
101
} ) ;
102
102
}
103
103
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 ;
108
108
for ( const mapper of this . mappers ) {
109
109
if ( offset >= mapper . start && offset <= mapper . end ) {
110
- return this . map ( mapper , generatedPosition ) ;
110
+ return this . map ( mapper , generated_position ) ;
111
111
}
112
112
if ( offset > mapper . end ) {
113
- originalOffset -= mapper . diff ;
113
+ original_offset -= mapper . diff ;
114
114
}
115
115
}
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 ) ;
118
118
}
119
119
120
120
map ( mapper , generatedPosition ) {
121
121
// 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 (
123
123
generatedPosition
124
124
) ;
125
125
// 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
128
128
) ;
129
129
// 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 ) ;
132
132
}
133
133
134
- toESLintPosition ( position ) {
134
+ to_ESLint_position ( position ) {
135
135
// ESLint line/column is 1-based
136
136
return { line : position . line + 1 , column : position . column + 1 } ;
137
137
}
@@ -143,35 +143,35 @@ export class DocumentMapper {
143
143
* @param position Line and character position
144
144
* @param text The text for which the offset should be retrived
145
145
*/
146
- function offsetAt ( position , text ) {
147
- const lineOffsets = getLineOffsets ( text ) ;
146
+ function offset_at ( position , text ) {
147
+ const line_offsets = get_line_offsets ( text ) ;
148
148
149
- if ( position . line >= lineOffsets . length ) {
149
+ if ( position . line >= line_offsets . length ) {
150
150
return text . length ;
151
151
} else if ( position . line < 0 ) {
152
152
return 0 ;
153
153
}
154
154
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 ;
158
158
159
- return clamp ( nextLineOffset , lineOffset , lineOffset + position . column ) ;
159
+ return clamp ( next_line_offset , line_offset , line_offset + position . column ) ;
160
160
}
161
161
162
- function positionAt ( offset , text ) {
162
+ function position_at ( offset , text ) {
163
163
offset = clamp ( offset , 0 , text . length ) ;
164
164
165
- const lineOffsets = getLineOffsets ( text ) ;
165
+ const line_offsets = get_line_offsets ( text ) ;
166
166
let low = 0 ;
167
- let high = lineOffsets . length ;
167
+ let high = line_offsets . length ;
168
168
if ( high === 0 ) {
169
- return Position . create ( 0 , offset ) ;
169
+ return { line : 0 , column : offset } ;
170
170
}
171
171
172
172
while ( low < high ) {
173
173
const mid = Math . floor ( ( low + high ) / 2 ) ;
174
- if ( lineOffsets [ mid ] > offset ) {
174
+ if ( line_offsets [ mid ] > offset ) {
175
175
high = mid ;
176
176
} else {
177
177
low = mid + 1 ;
@@ -181,30 +181,30 @@ function positionAt(offset, text) {
181
181
// low is the least x for which the line offset is larger than the current offset
182
182
// or array.length if no line offset is larger than the current offset
183
183
const line = low - 1 ;
184
- return { line, column : offset - lineOffsets [ line ] } ;
184
+ return { line, column : offset - line_offsets [ line ] } ;
185
185
}
186
186
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 ;
190
190
191
191
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 ;
195
195
}
196
196
const ch = text . charAt ( i ) ;
197
- isLineStart = ch === '\r' || ch === '\n' ;
197
+ is_line_start = ch === '\r' || ch === '\n' ;
198
198
if ( ch === '\r' && i + 1 < text . length && text . charAt ( i + 1 ) === '\n' ) {
199
199
i ++ ;
200
200
}
201
201
}
202
202
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 ) ;
205
205
}
206
206
207
- return lineOffsets ;
207
+ return line_offsets ;
208
208
}
209
209
210
210
function clamp ( num , min , max ) {
0 commit comments