Skip to content

Commit b17a2c6

Browse files
authored
Introduce GetECMALineOfPosition to avoid unused rune counting (#2065)
1 parent 90bc90f commit b17a2c6

File tree

14 files changed

+57
-47
lines changed

14 files changed

+57
-47
lines changed

internal/checker/grammarchecks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,8 @@ func (c *Checker) checkGrammarArrowFunction(node *ast.Node, file *ast.SourceFile
798798
}
799799

800800
equalsGreaterThanToken := arrowFunc.EqualsGreaterThanToken
801-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.Pos())
802-
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, equalsGreaterThanToken.End())
801+
startLine := scanner.GetECMALineOfPosition(file, equalsGreaterThanToken.Pos())
802+
endLine := scanner.GetECMALineOfPosition(file, equalsGreaterThanToken.End())
803803
return startLine != endLine && c.grammarErrorOnNode(equalsGreaterThanToken, diagnostics.Line_terminator_not_permitted_before_arrow)
804804
}
805805

internal/compiler/program.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ func (p *Program) getDiagnosticsWithPrecedingDirectives(sourceFile *ast.SourceFi
10821082
// Build map of directives by line number
10831083
directivesByLine := make(map[int]ast.CommentDirective)
10841084
for _, directive := range sourceFile.CommentDirectives {
1085-
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, directive.Loc.Pos())
1085+
line := scanner.GetECMALineOfPosition(sourceFile, directive.Loc.Pos())
10861086
directivesByLine[line] = directive
10871087
}
10881088
lineStarts := scanner.GetECMALineStarts(sourceFile)

internal/diagnosticwriter/diagnosticwriter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func writeCodeSnippet(writer io.Writer, sourceFile *ast.SourceFile, start int, l
9090
lastLineChar++ // When length is zero, squiggle the character right after the start position.
9191
}
9292

93-
lastLineOfFile, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, len(sourceFile.Text()))
93+
lastLineOfFile := scanner.GetECMALineOfPosition(sourceFile, len(sourceFile.Text()))
9494

9595
hasMoreThanFiveLines := lastLine-firstLine >= 4
9696
gutterWidth := len(strconv.Itoa(lastLine + 1))
@@ -357,7 +357,7 @@ func prettyPathForFileError(file *ast.SourceFile, fileErrors []*ast.Diagnostic,
357357
if file == nil || len(fileErrors) == 0 {
358358
return ""
359359
}
360-
line, _ := scanner.GetECMALineAndCharacterOfPosition(file, fileErrors[0].Loc().Pos())
360+
line := scanner.GetECMALineOfPosition(file, fileErrors[0].Loc().Pos())
361361
fileName := file.FileName()
362362
if tspath.PathIsAbsolute(fileName) && tspath.PathIsAbsolute(formatOpts.CurrentDirectory) {
363363
fileName = tspath.ConvertToRelativePath(file.FileName(), formatOpts.ComparePathsOptions)

internal/format/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func FormatOnSemicolon(ctx context.Context, sourceFile *ast.SourceFile, position
146146
}
147147

148148
func FormatOnEnter(ctx context.Context, sourceFile *ast.SourceFile, position int) []core.TextChange {
149-
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position)
149+
line := scanner.GetECMALineOfPosition(sourceFile, position)
150150
if line == 0 {
151151
return nil
152152
}

internal/format/indent.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func getIndentationForNodeWorker(
6868
// }) looking at the relationship between the list and *first* list item.
6969
var listIndentsChild bool
7070
if firstListChild != nil {
71-
listLine, _ := getStartLineAndCharacterForNode(firstListChild, sourceFile)
71+
listLine := getStartLineForNode(firstListChild, sourceFile)
7272
listIndentsChild = listLine > containingListOrParentStartLine
7373
}
7474
actualIndentation := getActualIndentationForListItem(current, sourceFile, options, listIndentsChild)
@@ -134,7 +134,7 @@ func isArgumentAndStartLineOverlapsExpressionBeingCalled(parent *ast.Node, child
134134
return false
135135
}
136136
expressionOfCallExpressionEnd := parent.Expression().End()
137-
expressionOfCallExpressionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, expressionOfCallExpressionEnd)
137+
expressionOfCallExpressionEndLine := scanner.GetECMALineOfPosition(sourceFile, expressionOfCallExpressionEnd)
138138
return expressionOfCallExpressionEndLine == childStartLine
139139
}
140140

@@ -188,7 +188,7 @@ func deriveActualIndentationFromList(list *ast.NodeList, index int, sourceFile *
188188
continue
189189
}
190190
// skip list items that ends on the same line with the current list element
191-
prevEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, list.Nodes[i].End())
191+
prevEndLine := scanner.GetECMALineOfPosition(sourceFile, list.Nodes[i].End())
192192
if prevEndLine != line {
193193
return findColumnForFirstNonWhitespaceCharacterInLine(line, char, sourceFile, options)
194194
}
@@ -243,7 +243,7 @@ func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast.
243243
if parent.Kind == ast.KindIfStatement && parent.AsIfStatement().ElseStatement == child {
244244
elseKeyword := astnav.FindPrecedingToken(sourceFile, child.Pos())
245245
debug.AssertIsDefined(elseKeyword)
246-
elseKeywordStartLine, _ := getStartLineAndCharacterForNode(elseKeyword, sourceFile)
246+
elseKeywordStartLine := getStartLineForNode(elseKeyword, sourceFile)
247247
return elseKeywordStartLine == childStartLine
248248
}
249249
return false
@@ -253,6 +253,10 @@ func getStartLineAndCharacterForNode(n *ast.Node, sourceFile *ast.SourceFile) (l
253253
return scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
254254
}
255255

256+
func getStartLineForNode(n *ast.Node, sourceFile *ast.SourceFile) int {
257+
return scanner.GetECMALineOfPosition(sourceFile, scanner.GetTokenPosOfNode(n, sourceFile, false))
258+
}
259+
256260
func GetContainingList(node *ast.Node, sourceFile *ast.SourceFile) *ast.NodeList {
257261
if node.Parent == nil {
258262
return nil
@@ -438,8 +442,8 @@ func NodeWillIndentChild(settings *FormatCodeSettings, parent *ast.Node, child *
438442
return rangeIsOnOneLine(child.Loc, sourceFile)
439443
}
440444
if parent.Kind == ast.KindBinaryExpression && sourceFile != nil && childKind == ast.KindJsxElement {
441-
parentStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), parent.Pos()))
442-
childStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), child.Pos()))
445+
parentStartLine := scanner.GetECMALineOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), parent.Pos()))
446+
childStartLine := scanner.GetECMALineOfPosition(sourceFile, scanner.SkipTrivia(sourceFile.Text(), child.Pos()))
443447
return parentStartLine != childStartLine
444448
}
445449
if parent.Kind != ast.KindBinaryExpression {
@@ -515,7 +519,7 @@ func NodeWillIndentChild(settings *FormatCodeSettings, parent *ast.Node, child *
515519
// branch beginning on the line that the whenTrue branch ends.
516520
func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
517521
if parent.Kind == ast.KindConditionalExpression && (child == parent.AsConditionalExpression().WhenTrue || child == parent.AsConditionalExpression().WhenFalse) {
518-
conditionEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().Condition.End())
522+
conditionEndLine := scanner.GetECMALineOfPosition(sourceFile, parent.AsConditionalExpression().Condition.End())
519523
if child == parent.AsConditionalExpression().WhenTrue {
520524
return childStartLine == conditionEndLine
521525
} else {
@@ -526,8 +530,8 @@ func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast
526530
// ? 1 : ( L1: whenTrue indented because it's on a new line
527531
// 0 L2: indented two stops, one because whenTrue was indented
528532
// ); and one because of the parentheses spanning multiple lines
529-
trueStartLine, _ := getStartLineAndCharacterForNode(parent.AsConditionalExpression().WhenTrue, sourceFile)
530-
trueEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End())
533+
trueStartLine := getStartLineForNode(parent.AsConditionalExpression().WhenTrue, sourceFile)
534+
trueEndLine := scanner.GetECMALineOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End())
531535
return conditionEndLine == trueStartLine && trueEndLine == childStartLine
532536
}
533537
}
@@ -549,7 +553,7 @@ func argumentStartsOnSameLineAsPreviousArgument(parent *ast.Node, child *ast.Nod
549553
}
550554

551555
previousNode := parent.Arguments()[currentIndex-1]
552-
lineOfPreviousNode, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, previousNode.End())
556+
lineOfPreviousNode := scanner.GetECMALineOfPosition(sourceFile, previousNode.End())
553557
if childStartLine == lineOfPreviousNode {
554558
return true
555559
}

internal/format/rulecontext.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,8 @@ func isSemicolonDeletionContext(context *FormattingContext) bool {
584584
nextTokenStart = scanner.GetTokenPosOfNode(nextRealToken, context.SourceFile, false)
585585
}
586586

587-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, context.currentTokenSpan.Loc.Pos())
588-
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(context.SourceFile, nextTokenStart)
587+
startLine := scanner.GetECMALineOfPosition(context.SourceFile, context.currentTokenSpan.Loc.Pos())
588+
endLine := scanner.GetECMALineOfPosition(context.SourceFile, nextTokenStart)
589589
if startLine == endLine {
590590
return nextTokenKind == ast.KindCloseBraceToken || nextTokenKind == ast.KindEndOfFile
591591
}

internal/format/span.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func getOwnOrInheritedDelta(n *ast.Node, options *FormatCodeSettings, sourceFile
8585
previousLine := -1
8686
var child *ast.Node
8787
for n != nil {
88-
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, withTokenStart(n, sourceFile).Pos())
88+
line := scanner.GetECMALineOfPosition(sourceFile, withTokenStart(n, sourceFile).Pos())
8989
if previousLine != -1 && line != previousLine {
9090
break
9191
}
@@ -242,10 +242,10 @@ func (w *formatSpanWorker) execute(s *formattingScanner) []core.TextChange {
242242
w.formattingScanner.advance()
243243

244244
if w.formattingScanner.isOnToken() {
245-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, withTokenStart(w.enclosingNode, w.sourceFile).Pos())
245+
startLine := scanner.GetECMALineOfPosition(w.sourceFile, withTokenStart(w.enclosingNode, w.sourceFile).Pos())
246246
undecoratedStartLine := startLine
247247
if ast.HasDecorators(w.enclosingNode) {
248-
undecoratedStartLine, _ = scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(w.enclosingNode, w.sourceFile))
248+
undecoratedStartLine = scanner.GetECMALineOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(w.enclosingNode, w.sourceFile))
249249
}
250250

251251
w.processNode(w.enclosingNode, w.enclosingNode, startLine, undecoratedStartLine, w.initialIndentation, w.delta)
@@ -305,7 +305,7 @@ func (w *formatSpanWorker) execute(s *formattingScanner) []core.TextChange {
305305
if parent == nil {
306306
parent = w.previousParent
307307
}
308-
line, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, tokenInfo.Loc.Pos())
308+
line := scanner.GetECMALineOfPosition(w.sourceFile, tokenInfo.Loc.Pos())
309309
w.processPair(
310310
tokenInfo,
311311
line,
@@ -343,11 +343,11 @@ func (w *formatSpanWorker) processChildNode(
343343
}
344344

345345
childStartPos := scanner.GetTokenPosOfNode(child, w.sourceFile, false)
346-
childStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, childStartPos)
346+
childStartLine := scanner.GetECMALineOfPosition(w.sourceFile, childStartPos)
347347

348348
undecoratedChildStartLine := childStartLine
349349
if ast.HasDecorators(child) {
350-
undecoratedChildStartLine, _ = scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(child, w.sourceFile))
350+
undecoratedChildStartLine = scanner.GetECMALineOfPosition(w.sourceFile, getNonDecoratorTokenPosOfNode(child, w.sourceFile))
351351
}
352352

353353
// if child is a list item - try to get its indentation, only if parent is within the original range.
@@ -457,7 +457,7 @@ func (w *formatSpanWorker) processChildNodes(
457457
break
458458
} else if tokenInfo.token.Kind == listStartToken {
459459
// consume list start token
460-
startLine, _ = scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, tokenInfo.token.Loc.Pos())
460+
startLine = scanner.GetECMALineOfPosition(w.sourceFile, tokenInfo.token.Loc.Pos())
461461

462462
w.consumeTokenAndAdvanceScanner(tokenInfo, parent, parentDynamicIndentation, parent, false)
463463

@@ -578,7 +578,7 @@ func (w *formatSpanWorker) tryComputeIndentationForListItem(startPos int, endPos
578578
return inheritedIndentation
579579
}
580580
} else {
581-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, startPos)
581+
startLine := scanner.GetECMALineOfPosition(w.sourceFile, startPos)
582582
startLinePosition := GetLineStartPositionForPosition(startPos, w.sourceFile)
583583
column := FindFirstNonWhitespaceColumn(startLinePosition, startPos, w.sourceFile, w.formattingContext.Options)
584584
if startLine != parentStartLine || startPos == column {
@@ -747,7 +747,7 @@ func (w *formatSpanWorker) processRange(r TextRangeWithKind, rangeStartLine int,
747747
if !rangeHasError {
748748
if w.previousRange == NewTextRangeWithKind(0, 0, 0) {
749749
// trim whitespaces starting from the beginning of the span up to the current line
750-
originalStartLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, w.originalRange.Pos())
750+
originalStartLine := scanner.GetECMALineOfPosition(w.sourceFile, w.originalRange.Pos())
751751
w.trimTrailingWhitespacesForLines(originalStartLine, rangeStartLine, NewTextRangeWithKind(0, 0, 0))
752752
} else {
753753
lineAction = w.processPair(r, rangeStartLine, parent, w.previousRange, w.previousRangeStartLine, w.previousParent, contextNode, dynamicIndentation)
@@ -797,8 +797,8 @@ func (w *formatSpanWorker) trimTrailingWhitespacesForRemainingRange(trivias []Te
797797
}
798798

799799
func (w *formatSpanWorker) trimTrailingWitespacesForPositions(startPos int, endPos int, previousRange TextRangeWithKind) {
800-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, startPos)
801-
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, endPos)
800+
startLine := scanner.GetECMALineOfPosition(w.sourceFile, startPos)
801+
endLine := scanner.GetECMALineOfPosition(w.sourceFile, endPos)
802802

803803
w.trimTrailingWhitespacesForLines(startLine, endLine+1, previousRange)
804804
}
@@ -909,8 +909,8 @@ func (w *formatSpanWorker) indentTriviaItems(trivia []TextRangeWithKind, comment
909909

910910
func (w *formatSpanWorker) indentMultilineComment(commentRange core.TextRange, indentation int, firstLineIsIndented bool, indentFinalLine bool) {
911911
// split comment in lines
912-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, commentRange.Pos())
913-
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, commentRange.End())
912+
startLine := scanner.GetECMALineOfPosition(w.sourceFile, commentRange.Pos())
913+
endLine := scanner.GetECMALineOfPosition(w.sourceFile, commentRange.End())
914914

915915
if startLine == endLine {
916916
if !firstLineIsIndented {
@@ -1033,7 +1033,7 @@ func (w *formatSpanWorker) consumeTokenAndAdvanceScanner(currentTokenInfo tokenI
10331033
if lineAction == LineActionNone {
10341034
// indent token only if end line of previous range does not match start line of the token
10351035
if savePreviousRange != NewTextRangeWithKind(0, 0, 0) {
1036-
prevEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(w.sourceFile, savePreviousRange.Loc.End())
1036+
prevEndLine := scanner.GetECMALineOfPosition(w.sourceFile, savePreviousRange.Loc.End())
10371037
indentToken = lastTriviaWasNewLine && tokenStartLine != prevEndLine
10381038
}
10391039
} else {

internal/format/util.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
)
1111

1212
func rangeIsOnOneLine(node core.TextRange, file *ast.SourceFile) bool {
13-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.Pos())
14-
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.End())
13+
startLine := scanner.GetECMALineOfPosition(file, node.Pos())
14+
endLine := scanner.GetECMALineOfPosition(file, node.End())
1515
return startLine == endLine
1616
}
1717

@@ -77,7 +77,7 @@ func getCloseTokenForOpenToken(kind ast.Kind) ast.Kind {
7777

7878
func GetLineStartPositionForPosition(position int, sourceFile *ast.SourceFile) int {
7979
lineStarts := scanner.GetECMALineStarts(sourceFile)
80-
line, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, position)
80+
line := scanner.GetECMALineOfPosition(sourceFile, position)
8181
return int(lineStarts[line])
8282
}
8383

internal/ls/completions.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ func boolToPtr(v bool) *bool {
25182518
}
25192519

25202520
func getLineOfPosition(file *ast.SourceFile, pos int) int {
2521-
line, _ := scanner.GetECMALineAndCharacterOfPosition(file, pos)
2521+
line := scanner.GetECMALineOfPosition(file, pos)
25222522
return line
25232523
}
25242524

@@ -3515,8 +3515,8 @@ func getContextualKeywords(file *ast.SourceFile, contextToken *ast.Node, positio
35153515
// Source: https://tc39.es/proposal-import-assertions/
35163516
if contextToken != nil {
35173517
parent := contextToken.Parent
3518-
tokenLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, contextToken.End())
3519-
currentLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, position)
3518+
tokenLine := scanner.GetECMALineOfPosition(file, contextToken.End())
3519+
currentLine := scanner.GetECMALineOfPosition(file, position)
35203520
if (ast.IsImportDeclaration(parent) ||
35213521
ast.IsExportDeclaration(parent) && parent.ModuleSpecifier() != nil) &&
35223522
contextToken == parent.ModuleSpecifier() &&

internal/ls/lsutil/asi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func NodeIsASICandidate(node *ast.Node, file *ast.SourceFile) bool {
9898
return true
9999
}
100100

101-
startLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, node.End())
102-
endLine, _ := scanner.GetECMALineAndCharacterOfPosition(file, astnav.GetStartOfNode(nextToken, file, false /*includeJSDoc*/))
101+
startLine := scanner.GetECMALineOfPosition(file, node.End())
102+
endLine := scanner.GetECMALineOfPosition(file, astnav.GetStartOfNode(nextToken, file, false /*includeJSDoc*/))
103103
return startLine != endLine
104104
}

0 commit comments

Comments
 (0)