Skip to content

Commit

Permalink
Fix bug with parsing @unchecked Sendable enum
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Aug 2, 2022
1 parent 26ffaab commit 7c64a78
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1756,7 +1756,12 @@ public func tokenize(_ source: String) -> [Token] {
case "case":
if let keywordIndex = index(of: .keyword, before: scopeIndex) {
var keyword = tokens[keywordIndex]
if case .keyword("where") = keyword,
if keyword == .keyword("where"),
let keywordIndex = index(of: .keyword, before: keywordIndex)
{
keyword = tokens[keywordIndex]
}
if keyword.isAttribute,
let keywordIndex = index(of: .keyword, before: keywordIndex)
{
keyword = tokens[keywordIndex]
Expand Down
9 changes: 9 additions & 0 deletions Tests/RulesTests+Spacing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ class SpacingTests: RulesTests {
testFormatting(for: input, rule: FormatRules.spaceAroundParens)
}

func testSpaceBetweenUncheckedAndSendable() {
let input = """
enum Foo: @unchecked Sendable {
case bar
}
"""
testFormatting(for: input, rule: FormatRules.spaceAroundParens)
}

func testSpaceBetweenParenAndAs() {
let input = "(foo.bar) as? String"
testFormatting(for: input, rule: FormatRules.spaceAroundParens, exclude: ["redundantParens"])
Expand Down
23 changes: 23 additions & 0 deletions Tests/TokenizerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3935,6 +3935,29 @@ class TokenizerTests: XCTestCase {
XCTAssertEqual(tokenize(input), output)
}

func testUncheckedSendableEnum() {
let input = "enum Foo: @unchecked Sendable { case bar }"
let output: [Token] = [
.keyword("enum"),
.space(" "),
.identifier("Foo"),
.delimiter(":"),
.space(" "),
.keyword("@unchecked"),
.space(" "),
.identifier("Sendable"),
.space(" "),
.startOfScope("{"),
.space(" "),
.keyword("case"),
.space(" "),
.identifier("bar"),
.space(" "),
.endOfScope("}"),
]
XCTAssertEqual(tokenize(input), output)
}

// MARK: dot prefix

func testEnumValueInDictionaryLiteral() {
Expand Down

0 comments on commit 7c64a78

Please sign in to comment.