@@ -1147,13 +1147,20 @@ namespace ts {
1147
1147
// position and whose end is greater than the position.
1148
1148
1149
1149
1150
+ // There are more sophisticated end tests later, but this one is very fast
1151
+ // and allows us to skip a bunch of work
1152
+ const end = children [ middle ] . getEnd ( ) ;
1153
+ if ( end < position ) {
1154
+ return Comparison . LessThan ;
1155
+ }
1156
+
1150
1157
const start = allowPositionInLeadingTrivia ? children [ middle ] . getFullStart ( ) : children [ middle ] . getStart ( sourceFile , /*includeJsDoc*/ true ) ;
1151
1158
if ( start > position ) {
1152
1159
return Comparison . GreaterThan ;
1153
1160
}
1154
1161
1155
1162
// first element whose start position is before the input and whose end position is after or equal to the input
1156
- if ( nodeContainsPosition ( children [ middle ] ) ) {
1163
+ if ( nodeContainsPosition ( children [ middle ] , start , end ) ) {
1157
1164
if ( children [ middle - 1 ] ) {
1158
1165
// we want the _first_ element that contains the position, so left-recur if the prior node also contains the position
1159
1166
if ( nodeContainsPosition ( children [ middle - 1 ] ) ) {
@@ -1181,13 +1188,16 @@ namespace ts {
1181
1188
return current ;
1182
1189
}
1183
1190
1184
- function nodeContainsPosition ( node : Node ) {
1185
- const start = allowPositionInLeadingTrivia ? node . getFullStart ( ) : node . getStart ( sourceFile , /*includeJsDoc*/ true ) ;
1191
+ function nodeContainsPosition ( node : Node , start ?: number , end ?: number ) {
1192
+ end ??= node . getEnd ( ) ;
1193
+ if ( end < position ) {
1194
+ return false ;
1195
+ }
1196
+ start ??= allowPositionInLeadingTrivia ? node . getFullStart ( ) : node . getStart ( sourceFile , /*includeJsDoc*/ true ) ;
1186
1197
if ( start > position ) {
1187
1198
// If this child begins after position, then all subsequent children will as well.
1188
1199
return false ;
1189
1200
}
1190
- const end = node . getEnd ( ) ;
1191
1201
if ( position < end || ( position === end && ( node . kind === SyntaxKind . EndOfFileToken || includeEndPosition ) ) ) {
1192
1202
return true ;
1193
1203
}
0 commit comments