Skip to content

Commit

Permalink
Prevent SwiftSyntax string interpolation errors in tests (realm#5258)
Browse files Browse the repository at this point in the history
Previously this was logged to the console when running tests:

```
[ParseError] Parsing a `DeclSyntax` node from string interpolation produced the following parsing errors.
Set a breakpoint in `SyntaxParseable.logStringInterpolationParsingError()` to debug the failure.
```

And slightly improve the style of the expanded `@SwiftSyntaxRule` macro.
  • Loading branch information
jpsim authored Oct 3, 2023
1 parent f6e5f77 commit e30065e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
42 changes: 20 additions & 22 deletions Source/SwiftLintCoreMacros/SwiftSyntaxRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,31 @@ struct SwiftSyntaxRule: ExtensionMacro {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}
\(createFoldingPreprocessor(from: node.foldArgument))
}
""")
]
"""),
try createFoldingPreprocessor(type: type, foldArgument: node.foldArgument)
].compactMap { $0 }
}

private static func createFoldingPreprocessor(from foldArgument: ExprSyntax?) -> DeclSyntax {
guard let foldArgument else {
return ""
private static func createFoldingPreprocessor(
type: some TypeSyntaxProtocol,
foldArgument: ExprSyntax?
) throws -> ExtensionDeclSyntax? {
guard
let foldArgument,
let booleanLiteral = foldArgument.as(BooleanLiteralExprSyntax.self)?.literal,
booleanLiteral.text == "true"
else {
return nil
}
if let booleanLiteral = foldArgument.as(BooleanLiteralExprSyntax.self)?.literal {
if booleanLiteral.text == "true" {
return """
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
"""
}
if booleanLiteral.text == "false" {
return ""
}
}
return """
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
if \(foldArgument) { file.foldedSyntaxTree } else { nil }

return try ExtensionDeclSyntax("""
extension \(type) {
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
}
"""
""")
}
}

Expand Down
13 changes: 4 additions & 9 deletions Tests/MacroTests/SwiftSyntaxRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class SwiftSyntaxRuleTests: XCTestCase {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}
}
""",
macros: macros
Expand All @@ -40,7 +39,6 @@ final class SwiftSyntaxRuleTests: XCTestCase {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}
}
""",
macros: macros
Expand All @@ -60,6 +58,9 @@ final class SwiftSyntaxRuleTests: XCTestCase {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}
}
extension Hello {
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
file.foldedSyntaxTree
}
Expand All @@ -70,6 +71,7 @@ final class SwiftSyntaxRuleTests: XCTestCase {
}

func testArbitraryFoldArgument() {
// Silently fail because the macro definition explicitly requires a bool
assertMacroExpansion(
"""
@SwiftSyntaxRule(foldExpressions: variable)
Expand All @@ -82,13 +84,6 @@ final class SwiftSyntaxRuleTests: XCTestCase {
func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {
Visitor(viewMode: .sourceAccurate)
}
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
if variable {
file.foldedSyntaxTree
} else {
nil
}
}
}
""",
macros: macros
Expand Down

0 comments on commit e30065e

Please sign in to comment.