Skip to content

Commit

Permalink
Fix class mistaken for class let
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed May 20, 2023
1 parent f5c988e commit 336b2b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Sources/ParsingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,19 @@ extension Formatter {
}
}

/// Returns true if the token at specified index is a modifier
func isModifier(at index: Int) -> Bool {
guard let token = token(at: index), token.isModifierKeyword else {
return false
}
if token == .keyword("class"),
let nextToken = next(.nonSpaceOrCommentOrLinebreak, after: index)
{
return nextToken.isDeclarationTypeKeyword || nextToken.isModifierKeyword
}
return true
}

/// Returns true if the modifiers list for the given declaration contain a
/// modifier matching the specified predicate
func modifiersForDeclaration(at index: Int, contains: (Int, String) -> Bool) -> Bool {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6138,7 +6138,7 @@ public struct _FormatRules {
}

// Skip modifiers
while keyword.isModifierKeyword {
while formatter.isModifier(at: keywordIndex) {
guard let nextIndex = formatter.index(of: .keyword, after: keywordIndex) else {
break
}
Expand Down
14 changes: 14 additions & 0 deletions Tests/RulesTests+Wrapping.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3887,6 +3887,20 @@ class WrappingTests: RulesTests {
testFormatting(for: input, rule: FormatRules.wrapAttributes, options: options)
}

func testClassAttributeNotMistakenForClassLet() {
let input = """
@objc final class MyClass: NSObject {}
let myClass = MyClass()
"""
let output = """
@objc
final class MyClass: NSObject {}
let myClass = MyClass()
"""
let options = FormatOptions(typeAttributes: .prevLine)
testFormatting(for: input, output, rule: FormatRules.wrapAttributes, options: options)
}

func testClassImportAttributeNotTreatedAsType() {
let input = """
@testable import class Framework.Foo
Expand Down

0 comments on commit 336b2b4

Please sign in to comment.