Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/SwiftParser/Availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,13 @@ extension Parser {
(unexpectedBeforePlatform, platform) = self.expect(.identifier)
}

let unexpectedComparison: RawUnexpectedNodesSyntax?
if let greaterThanOrEqualTo = self.consumeIfContextualPunctuator(">=") {
unexpectedComparison = RawUnexpectedNodesSyntax([greaterThanOrEqualTo], arena: self.arena)
} else {
unexpectedComparison = nil
}

let version: RawVersionTupleSyntax?
if self.at(.integerLiteral, .floatingLiteral) {
version = self.parseVersionTuple()
Expand All @@ -228,6 +235,7 @@ extension Parser {
return RawAvailabilityVersionRestrictionSyntax(
unexpectedBeforePlatform,
platform: platform,
unexpectedComparison,
version: version,
arena: self.arena
)
Expand Down
19 changes: 19 additions & 0 deletions Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,25 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
return .visitChildren
}

public override func visit(_ node: AvailabilityVersionRestrictionSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
}
if let unexpected = node.unexpectedBetweenPlatformAndVersion,
unexpected.onlyToken(where: { $0.tokenKind == .binaryOperator(">=") }) != nil
{
addDiagnostic(
unexpected,
.versionComparisonNotNeeded,
fixIts: [
FixIt(message: RemoveNodesFixIt(unexpected), changes: .makeMissing(unexpected))
],
handledNodes: [unexpected.id]
)
}
return .visitChildren
}

public override func visit(_ node: ConditionElementSyntax) -> SyntaxVisitorContinueKind {
if shouldSkip(node) {
return .skipChildren
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ extension DiagnosticMessage where Self == StaticParserError {
public static var unexpectedSemicolon: Self {
.init("unexpected ';' separator")
}
public static var versionComparisonNotNeeded: Self {
.init("version comparison not needed")
}
}

// MARK: - Diagnostics (please sort alphabetically)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,12 @@ final class AvailabilityQueryTests: XCTestCase {
}
""",
diagnostics: [
// TODO: (good first issue) Old parser expected error on line 1: version comparison not needed, Fix-It replacements: 19 - 22 = ''
DiagnosticSpec(message: "unexpected code '>= 10.51, *' in availability condition")
]
DiagnosticSpec(message: "version comparison not needed", fixIts: ["remove '>='"])
],
fixedSource: """
if #available(OSX 10.51, *) {
}
"""
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,7 @@ final class AvailabilityQueryUnavailabilityTests: XCTestCase {
}
""",
diagnostics: [
// TODO: (good first issue) Old parser expected error on line 2: version comparison not needed, Fix-It replacements: 21 - 24 = ''
DiagnosticSpec(message: "unexpected code '>= 10.51' in availability condition")
DiagnosticSpec(message: "version comparison not needed", fixIts: ["remove '>='"])
]
)
}
Expand Down