2
2
"use strict" ;
3
3
import ObjectAssign from "object-assign" ;
4
4
import StructuredSource from "structured-source" ;
5
+
5
6
export default class StringSource {
6
7
constructor ( node ) {
7
8
this . rootNode = node ;
@@ -21,7 +22,7 @@ export default class StringSource {
21
22
// intermediate = trim decoration from Original
22
23
// e.g.) [2, 5]
23
24
intermediate: [start, end]
24
- // generaged value = "Str"
25
+ // generated value = "Str"
25
26
// e.g.) [0, 3]
26
27
generated : [start, end]
27
28
}]
@@ -43,24 +44,27 @@ export default class StringSource {
43
44
/**
44
45
* @deprecated use originalPositionFromPosition instead of
45
46
* @param generatedPosition
47
+ * @param {boolean } isEnd - is the position end of the node?
48
+
46
49
* @returns {Object }
47
50
*/
48
- originalPositionFor ( generatedPosition ) {
49
- return this . originalPositionFromPosition ( generatedPosition ) ;
51
+ originalPositionFor ( generatedPosition , isEnd ) {
52
+ return this . originalPositionFromPosition ( generatedPosition , isEnd ) ;
50
53
}
51
54
52
55
/**
53
56
* get original index from generated index value
54
57
* @param {number } generatedIndex - position is a index value.
58
+ * @param {boolean } isEnd - is the position end of the node?
55
59
* @returns {number|undefined } original
56
60
*/
57
- originalIndexFromIndex ( generatedIndex ) {
61
+ originalIndexFromIndex ( generatedIndex , isEnd = false ) {
58
62
let hitTokenMaps = this . tokenMaps . filter ( ( tokenMap , index ) => {
59
63
const generated = tokenMap . generated ;
60
64
const nextTokenMap = this . tokenMaps [ index + 1 ] ;
61
65
const nextGenerated = nextTokenMap ? nextTokenMap . generated : null ;
62
66
if ( nextGenerated ) {
63
- if ( generated [ 0 ] <= generatedIndex && generatedIndex < nextGenerated [ 0 ] ) {
67
+ if ( generated [ 0 ] <= generatedIndex && generatedIndex <= nextGenerated [ 0 ] ) {
64
68
return true ;
65
69
}
66
70
} else {
@@ -72,57 +76,70 @@ export default class StringSource {
72
76
if ( hitTokenMaps . length === 0 ) {
73
77
return ;
74
78
}
75
- // a bcd
76
- // b = index 1
77
- // original `a` bcd
78
- // originalRange [3, 7]
79
- // adjustedStart = 1
80
- // b's index = 3 + 1
81
- let hitTokenMap = hitTokenMaps [ 0 ] ;
82
- // <----------->\[<------------->|text]
79
+
80
+ /**
81
+ * **Str**ABC
82
+ * |
83
+ * |
84
+ * generatedIndex
85
+ *
86
+ * If isEnd is true, generatedIndex is end of **Str** node.
87
+ * If isEnd is false, generatedIndex is index of ABC node.
88
+ */
89
+
90
+ const hitTokenMap = isEnd ? hitTokenMaps [ 0 ] : hitTokenMaps [ hitTokenMaps . length - 1 ] ;
91
+ // <----------->[<------------->|text]
83
92
// ^ ^
84
93
// position-generated intermediate-origin
85
- let outAdjust = generatedIndex - hitTokenMap . generated [ 0 ] ;
86
- let inAdjust = hitTokenMap . intermediate [ 0 ] - hitTokenMap . original [ 0 ] ;
87
- return outAdjust + inAdjust + hitTokenMap . original [ 0 ] ;
94
+
95
+ // <-------------->[<------------->|text]
96
+ // | |
97
+ // outer adjust _
98
+ // inner adjust = 1
99
+ const outerAdjust = generatedIndex - hitTokenMap . generated [ 0 ] ;
100
+ const innerAdjust = hitTokenMap . intermediate [ 0 ] - hitTokenMap . original [ 0 ] ;
101
+ return outerAdjust + innerAdjust + hitTokenMap . original [ 0 ] ;
88
102
}
89
103
90
104
/**
91
105
* get original position from generated position
92
106
* @param {object } position
107
+ * @param {boolean } isEnd - is the position end of the node?
93
108
* @returns {object } original position
94
109
*/
95
- originalPositionFromPosition ( position ) {
110
+ originalPositionFromPosition ( position , isEnd = false ) {
96
111
if ( typeof position . line === "undefined" || typeof position . column === "undefined" ) {
97
112
throw new Error ( "position.{line, column} should not undefined: " + JSON . stringify ( position ) ) ;
98
113
}
99
- var generatedIndex = this . generatedSource . positionToIndex ( position ) ;
114
+ const generatedIndex = this . generatedSource . positionToIndex ( position ) ;
100
115
if ( isNaN ( generatedIndex ) ) {
101
116
// Not Found
102
117
return ;
103
118
}
104
- let originalIndex = this . originalIndexFromIndex ( generatedIndex ) ;
105
- return this . originalSource . indexToPosition ( originalIndex ) ;
119
+ const originalIndex = this . originalIndexFromIndex ( generatedIndex , isEnd ) ;
120
+ return this . originalSource . indexToPosition ( originalIndex , isEnd ) ;
106
121
}
107
122
108
123
/**
109
124
* get original index from generated position
110
125
* @param {object } generatedPosition
126
+ * @param {boolean } isEnd - is the position end of the node?
111
127
* @returns {number } original index
112
128
*/
113
- originalIndexFromPosition ( generatedPosition ) {
129
+ originalIndexFromPosition ( generatedPosition , isEnd = false ) {
114
130
const originalPosition = this . originalPositionFromPosition ( generatedPosition ) ;
115
- return this . originalSource . positionToIndex ( originalPosition ) ;
131
+ return this . originalSource . positionToIndex ( originalPosition , isEnd ) ;
116
132
}
117
133
118
134
/**
119
135
* get original position from generated index
120
136
* @param {number } generatedIndex
137
+ * @param {boolean } isEnd - is the position end of the node?
121
138
* @return {object } original position
122
139
*/
123
- originalPositionFromIndex ( generatedIndex ) {
140
+ originalPositionFromIndex ( generatedIndex , isEnd = false ) {
124
141
let originalIndex = this . originalIndexFromIndex ( generatedIndex ) ;
125
- return this . originalSource . indexToPosition ( originalIndex ) ;
142
+ return this . originalSource . indexToPosition ( originalIndex , isEnd ) ;
126
143
}
127
144
128
145
0 commit comments