Skip to content

Commit

Permalink
Fix --nilInit insert being applied to as keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
rakuyoMo committed Jul 15, 2024
1 parent 4433c16 commit f2af2f3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,8 @@ public struct _FormatRules {
if formatter.index(of: .endOfStatement, in: index + 1 ..< optionalIndex) != nil {
return
}
if !formatter.tokens[optionalIndex - 1].isSpaceOrCommentOrLinebreak {
let previousToken = formatter.tokens[optionalIndex - 1]
if !previousToken.isSpaceOrCommentOrLinebreak && previousToken != .keyword("as") {
let equalsIndex = formatter.index(of: .nonSpaceOrLinebreak, after: optionalIndex, if: {
$0 == .operator("=", .infix)
})
Expand Down
10 changes: 10 additions & 0 deletions Tests/RulesTests+Redundancy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2266,6 +2266,16 @@ class RedundancyTests: RulesTests {
options: options)
}

func testNoInsertNilInitInAs() {
let input = """
let json: Any = ["key": 1]
var jsonObject = json as? [String: Int]
"""
let options = FormatOptions(nilInit: .insert)
testFormatting(for: input, rule: FormatRules.redundantNilInit,
options: options)
}

// MARK: - redundantLet

func testRemoveRedundantLet() {
Expand Down

0 comments on commit f2af2f3

Please sign in to comment.