Skip to content

Commit 6cf70ba

Browse files
Copilotjakebailey
andcommitted
Add additional defensive nil checks in formatting functions
Added nil checks before calling getStartLineAndCharacterForNode in childStartsOnTheSameLineWithElseInIfStatement and childIsUnindentedBranchOfConditionalExpression to be more defensive. Co-authored-by: jakebailey <[email protected]>
1 parent 372c04d commit 6cf70ba

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

internal/format/indent.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ func findFirstNonWhitespaceCharacterAndColumn(startPos int, endPos int, sourceFi
242242
func childStartsOnTheSameLineWithElseInIfStatement(parent *ast.Node, child *ast.Node, childStartLine int, sourceFile *ast.SourceFile) bool {
243243
if parent.Kind == ast.KindIfStatement && parent.AsIfStatement().ElseStatement == child {
244244
elseKeyword := astnav.FindPrecedingToken(sourceFile, child.Pos())
245+
if elseKeyword == nil {
246+
return false
247+
}
245248
debug.AssertIsDefined(elseKeyword)
246249
elseKeywordStartLine, _ := getStartLineAndCharacterForNode(elseKeyword, sourceFile)
247250
return elseKeywordStartLine == childStartLine
@@ -533,8 +536,12 @@ func childIsUnindentedBranchOfConditionalExpression(parent *ast.Node, child *ast
533536
// ? 1 : ( L1: whenTrue indented because it's on a new line
534537
// 0 L2: indented two stops, one because whenTrue was indented
535538
// ); and one because of the parentheses spanning multiple lines
536-
trueStartLine, _ := getStartLineAndCharacterForNode(parent.AsConditionalExpression().WhenTrue, sourceFile)
537-
trueEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, parent.AsConditionalExpression().WhenTrue.End())
539+
whenTrue := parent.AsConditionalExpression().WhenTrue
540+
if whenTrue == nil {
541+
return false
542+
}
543+
trueStartLine, _ := getStartLineAndCharacterForNode(whenTrue, sourceFile)
544+
trueEndLine, _ := scanner.GetECMALineAndCharacterOfPosition(sourceFile, whenTrue.End())
538545
return conditionEndLine == trueStartLine && trueEndLine == childStartLine
539546
}
540547
}

0 commit comments

Comments
 (0)