Skip to content

Commit e00ef9f

Browse files
committed
[WIP] Update test cases & run swift-format
1 parent ef06459 commit e00ef9f

File tree

6 files changed

+43
-36
lines changed

6 files changed

+43
-36
lines changed

CodeGeneration/Sources/SyntaxSupport/ExprNodes.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public let EXPR_NODES: [Node] = [
184184
),
185185
]
186186
),
187-
187+
188188
// the canImport expr in if config expression
189189
Node(
190190
name: "CanImportExpr",
@@ -216,7 +216,7 @@ public let EXPR_NODES: [Node] = [
216216
Child(
217217
name: "RightParen",
218218
kind: .token(choices: [.token(tokenKind: "RightParenToken")])
219-
)
219+
),
220220
]
221221
),
222222

Sources/SwiftParser/Expressions.swift

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -761,16 +761,15 @@ extension Parser {
761761
leadingExpr = self.parseDottedExpressionSuffix(leadingExpr)
762762
continue
763763
}
764-
765-
764+
766765
// canImport expression
767766
if let leadingIdentifier = leadingExpr.as(RawIdentifierExprSyntax.self),
768-
leadingIdentifier.identifier.tokenText == "canImport" {
767+
leadingIdentifier.identifier.tokenText == "canImport"
768+
{
769769
leadingExpr = self.parseCanImportExpression(leadingIdentifier)
770770
continue
771771
}
772-
773-
772+
774773
// If there is an expr-call-suffix, parse it and form a call.
775774
if let lparen = self.consume(if: TokenSpec(.leftParen, allowAtStartOfLine: false)) {
776775
let args = self.parseArgumentListElements(pattern: pattern)
@@ -2022,7 +2021,7 @@ extension Parser {
20222021
} while keepGoing != nil && loopProgress.evaluate(currentToken)
20232022
return result
20242023
}
2025-
2024+
20262025
mutating func parseTupleExprElement(pattern: PatternContext) -> RawTupleExprElementSyntax {
20272026
let unexpectedBeforeLabel: RawUnexpectedNodesSyntax?
20282027
let label: RawTokenSyntax?
@@ -2054,9 +2053,9 @@ extension Parser {
20542053
} else {
20552054
expr = self.parseExpression(pattern: pattern)
20562055
}
2057-
2056+
20582057
let comma = self.consume(if: .comma)
2059-
2058+
20602059
return RawTupleExprElementSyntax(
20612060
unexpectedBeforeLabel,
20622061
label: label,
@@ -2066,7 +2065,7 @@ extension Parser {
20662065
arena: self.arena
20672066
)
20682067
}
2069-
2068+
20702069
}
20712070

20722071
extension Parser {
@@ -2566,16 +2565,16 @@ extension Parser {
25662565
extension Parser {
25672566
mutating func parseCanImportExpression(_ canImportIdentifierExpr: RawIdentifierExprSyntax) -> RawExprSyntax {
25682567
let (unexpectedBeforeLeftParen, leftParen) = self.expect(.leftParen)
2569-
2568+
25702569
let (unexpectedBeforeImportPath, importPath) = self.expect(.identifier)
2571-
2570+
25722571
let comma = self.consume(if: .comma)
25732572
var versionTupleExpr: RawTupleExprElementSyntax?
2574-
2573+
25752574
if comma != nil {
25762575
versionTupleExpr = self.parseTupleExprElement(pattern: .none)
2577-
2578-
if versionTupleExpr?.label == nil {
2576+
2577+
if versionTupleExpr?.label == nil {
25792578
versionTupleExpr = RawTupleExprElementSyntax(
25802579
versionTupleExpr?.unexpectedBeforeLabel,
25812580
label: self.missingToken(.identifier),
@@ -2588,11 +2587,11 @@ extension Parser {
25882587
versionTupleExpr?.unexpectedAfterTrailingComma,
25892588
arena: self.arena
25902589
)
2591-
} else if !["_version", "_underlyingVersion"].map({SyntaxText.init($0)}).contains(versionTupleExpr?.label?.tokenText) {
2590+
} else if !["_version", "_underlyingVersion"].map({ SyntaxText.init($0) }).contains(versionTupleExpr?.label?.tokenText) {
25922591
versionTupleExpr = RawTupleExprElementSyntax(
2593-
versionTupleExpr?.unexpectedBeforeLabel,
2592+
RawUnexpectedNodesSyntax(combining: versionTupleExpr?.unexpectedBeforeLabel, versionTupleExpr?.label, arena: self.arena),
25942593
label: self.missingToken(.identifier),
2595-
RawUnexpectedNodesSyntax(combining: versionTupleExpr?.unexpectedBetweenLabelAndColon, versionTupleExpr?.label, arena: self.arena),
2594+
versionTupleExpr?.unexpectedBetweenLabelAndColon,
25962595
colon: versionTupleExpr?.colon,
25972596
versionTupleExpr?.unexpectedBetweenColonAndExpression,
25982597
expression: versionTupleExpr!.expression,
@@ -2603,9 +2602,9 @@ extension Parser {
26032602
)
26042603
}
26052604
}
2606-
2605+
26072606
let (unexpectedBeforeRightParen, rightParen) = self.expect(.rightParen)
2608-
2607+
26092608
return RawExprSyntax(
26102609
RawCanImportExprSyntax(
26112610
canImportKeyword: canImportIdentifierExpr.identifier,

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import SwiftDiagnostics
1414
import SwiftParser
1515
@_spi(RawSyntax) import SwiftSyntax
16+
import Foundation
1617

1718
fileprivate func getTokens(between first: TokenSyntax, and second: TokenSyntax) -> [TokenSyntax] {
1819
var tokens: [TokenSyntax] = []
@@ -513,27 +514,33 @@ public class ParseDiagnosticsGenerator: SyntaxAnyVisitor {
513514
}
514515
return .visitChildren
515516
}
516-
517+
517518
public override func visit(_ node: CanImportExprSyntax) -> SyntaxVisitorContinueKind {
518519
if shouldSkip(node) {
519520
return .skipChildren
520521
}
521-
522+
522523
if let versionTuple = node.version {
523-
524-
let allowedLabel = ["_version", "_underlyingVersion"]
525-
guard let label = versionTuple.label,
526-
allowedLabel.contains(label.text) else {
524+
if let label = versionTuple.label,
525+
label.presence == .missing
526+
{
527527
addDiagnostic(
528528
versionTuple,
529529
.canImportWrongSecondParameterLabel,
530530
handledNodes: [versionTuple.id]
531531
)
532-
return .visitChildren
533532
}
534-
535533
}
536-
534+
535+
if let unexpectedBeforeRightParen = node.unexpectedBetweenVersionAndRightParen {
536+
addDiagnostic(
537+
unexpectedBeforeRightParen,
538+
.canImportWrongParameterNumber,
539+
handledNodes: [unexpectedBeforeRightParen.id]
540+
)
541+
542+
}
543+
537544
return .visitChildren
538545
}
539546

Tests/SwiftParserTest/Assertions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import XCTest
1414
@_spi(RawSyntax) import SwiftSyntax
15-
@_spi(Testing) @_spi(RawSyntax) import SwiftParser
15+
@_spi(Testing)@_spi(RawSyntax) import SwiftParser
1616
@_spi(RawSyntax) import SwiftParserDiagnostics
1717
import SwiftDiagnostics
1818
import _SwiftSyntaxTestSupport

Tests/SwiftParserTest/DeclarationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
@_spi(RawSyntax) import SwiftSyntax
14-
@_spi(Testing) @_spi(RawSyntax) import SwiftParser
14+
@_spi(Testing)@_spi(RawSyntax) import SwiftParser
1515
import SwiftSyntaxBuilder
1616
import SwiftBasicFormat
1717
import XCTest

Tests/SwiftParserTest/translated/IfconfigExprTests.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ final class IfconfigExprTests: XCTestCase {
330330
#endif
331331
""",
332332
diagnostics: [
333-
DiagnosticSpec(message: "expected value in function call", fixIts: ["insert value"])
333+
DiagnosticSpec(message: "2nd parameter of canImport should be labeled as _version or _underlyingVersion")
334334
]
335335
)
336336
}
@@ -351,12 +351,13 @@ final class IfconfigExprTests: XCTestCase {
351351
func testIfconfigExpr25() {
352352
assertParse(
353353
"""
354-
#if canImport(A, 2.2, 1.1)
354+
#if canImport(A, 1️⃣2.2, 2️⃣1.1)
355355
let a = 1
356356
#endif
357357
""",
358358
diagnostics: [
359-
// TODO: Old parser expected error on line 1: canImport can take only two parameters
359+
DiagnosticSpec(locationMarker: "1️⃣", message: "2nd parameter of canImport should be labeled as _version or _underlyingVersion"),
360+
DiagnosticSpec(locationMarker: "2️⃣", message: "canImport can take only two parameters"),
360361
]
361362
)
362363
}
@@ -369,7 +370,7 @@ final class IfconfigExprTests: XCTestCase {
369370
#endif
370371
""",
371372
diagnostics: [
372-
DiagnosticSpec(message: "expected value in function call", fixIts: ["insert value"])
373+
DiagnosticSpec(message: "expected value in 'canImport' expression in if config expression", fixIts: ["insert value"])
373374
]
374375
)
375376
}

0 commit comments

Comments
 (0)