Skip to content

Commit 6d260bc

Browse files
committed
Refactor
1 parent 27a2daf commit 6d260bc

File tree

3 files changed

+37
-40
lines changed

3 files changed

+37
-40
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,29 +1520,31 @@ extension Parser {
15201520
) -> RawVariableDeclSyntax {
15211521
let (unexpectedBeforeIntroducer, introducer) = self.eat(handle)
15221522
let hasTryBeforeIntroducer = unexpectedBeforeIntroducer?.containsToken(where: { TokenSpec(.try) ~= $0 }) ?? false
1523-
1524-
var attrs = attrs
1523+
15251524
var unexpectedBetweenBindingKeywordAndBindings: RawUnexpectedNodesSyntax?
1526-
1525+
var attrs = attrs
15271526
var elements = [RawPatternBindingSyntax]()
15281527
do {
15291528
var keepGoing: RawTokenSyntax? = nil
15301529
var loopProgress = LoopProgressCondition()
15311530
repeat {
1532-
1533-
if (attrs.attributes?.elements.count ?? 0) == 0, self.at(.atSign), let attributes = self.parseAttributeList() {
1534-
1535-
unexpectedBetweenBindingKeywordAndBindings = RawUnexpectedNodesSyntax([attributes], arena: self.arena)
1536-
1531+
1532+
if self.at(.atSign),
1533+
attrs.attributes?.isEmpty ?? true,
1534+
let recoveredAttributes = self.parseAttributeList() {
1535+
unexpectedBetweenBindingKeywordAndBindings = RawUnexpectedNodesSyntax([recoveredAttributes], arena: self.arena)
1536+
15371537
class TokenMissingMaker: SyntaxRewriter {
15381538
override func visit(_ token: TokenSyntax) -> TokenSyntax {
1539-
return TokenSyntax(token.tokenKind, presence: .missing)
1539+
return .init(token.tokenKind, presence: .missing)
15401540
}
15411541
}
1542-
1543-
let syntaxRewriter = TokenMissingMaker(arena: self.arena)
1544-
let missingAttributes = syntaxRewriter.visit(Syntax(raw: attributes.raw)).raw
1542+
let tokenMissingMaker = TokenMissingMaker(arena: self.arena)
1543+
let missingAttributes = tokenMissingMaker.visit(Syntax(raw: recoveredAttributes.raw)).raw
15451544
attrs.attributes = RawAttributeListSyntax(missingAttributes)
1545+
1546+
1547+
15461548
}
15471549

15481550
var (pattern, typeAnnotation) = self.parseTypedPattern()

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,29 +1170,24 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
11701170
moveFixIt: { MoveTokensAfterFixIt(movedTokens: $0, after: .equal) },
11711171
removeRedundantFixIt: { RemoveRedundantFixIt(removeTokens: $0) }
11721172
)
1173-
1174-
if let unexpected = node.unexpectedBetweenBindingKeywordAndBindings {
1175-
1176-
let fixit = FixIt(
1177-
message: MoveNodesInFrontOfFixIt(
1178-
movedNodes: [unexpected],
1179-
inFrontOf: node.bindingKeyword.tokenKind
1180-
),
1181-
changes: [
1182-
.makeMissing(unexpected),
1183-
.makePresent(node.attributes!, trailingTrivia: .space),
1184-
]
1185-
)
1186-
1187-
addDiagnostic(
1188-
unexpected,
1189-
.misplacedAttributeInVarDecl,
1190-
fixIts: [fixit],
1191-
handledNodes: [unexpected.id, node.attributes!.id]
1192-
)
1193-
1194-
}
1195-
1173+
1174+
if let attributes = node.attributes,
1175+
attributes.isMissingAllTokens,
1176+
let unexpected = node.unexpectedBetweenBindingKeywordAndBindings {
1177+
1178+
let fixit = FixIt(message: MoveNodesInFrontOfFixIt(movedNodes: [unexpected], inFrontOf: node.bindingKeyword.tokenKind),
1179+
changes: [
1180+
.makeMissing(unexpected),
1181+
.makePresent(attributes, trailingTrivia: .space)
1182+
])
1183+
1184+
addDiagnostic(unexpected,
1185+
.misplacedAttributeInVarDecl,
1186+
fixIts: [fixit],
1187+
handledNodes: [attributes.id, unexpected.id])
1188+
1189+
}
1190+
11961191
return .visitChildren
11971192
}
11981193

Tests/SwiftParserTest/AttributeTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -570,21 +570,21 @@ final class AttributeTests: XCTestCase {
570570
"""
571571
)
572572
}
573-
574-
func testMisplacedAttributeInVar() {
573+
574+
func testMisplacedAttributeInVariableDecl() {
575575
AssertParse(
576576
"""
577577
struct A {
578-
var 1️⃣@Bar i: Foo
578+
var 1️⃣@State name: String
579579
}
580580
""",
581581
diagnostics: [
582-
DiagnosticSpec(locationMarker: "1️⃣", message: "misplaced attribute in variable declaration", fixIts: ["move attributes in front of 'var'"])
582+
DiagnosticSpec(message: "misplaced attribute in variable declaration", fixIts: ["move attributes in front of 'var'"]),
583583
],
584584
fixedSource:
585585
"""
586586
struct A {
587-
@Bar var i: Foo
587+
@State var name: String
588588
}
589589
"""
590590
)

0 commit comments

Comments
 (0)