From e30065e632e44cfc16125e3f0446161888ccb09b Mon Sep 17 00:00:00 2001 From: JP Simard Date: Tue, 3 Oct 2023 10:53:41 -0400 Subject: [PATCH] Prevent SwiftSyntax string interpolation errors in tests (#5258) 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. --- .../SwiftLintCoreMacros/SwiftSyntaxRule.swift | 42 +++++++++---------- Tests/MacroTests/SwiftSyntaxRuleTests.swift | 13 ++---- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/Source/SwiftLintCoreMacros/SwiftSyntaxRule.swift b/Source/SwiftLintCoreMacros/SwiftSyntaxRule.swift index 2340f7b1ad..8d307157db 100644 --- a/Source/SwiftLintCoreMacros/SwiftSyntaxRule.swift +++ b/Source/SwiftLintCoreMacros/SwiftSyntaxRule.swift @@ -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 + } } - """ + """) } } diff --git a/Tests/MacroTests/SwiftSyntaxRuleTests.swift b/Tests/MacroTests/SwiftSyntaxRuleTests.swift index 6dcdb14b0f..f9dbda1355 100644 --- a/Tests/MacroTests/SwiftSyntaxRuleTests.swift +++ b/Tests/MacroTests/SwiftSyntaxRuleTests.swift @@ -20,7 +20,6 @@ final class SwiftSyntaxRuleTests: XCTestCase { func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { Visitor(viewMode: .sourceAccurate) } - } """, macros: macros @@ -40,7 +39,6 @@ final class SwiftSyntaxRuleTests: XCTestCase { func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { Visitor(viewMode: .sourceAccurate) } - } """, macros: macros @@ -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 } @@ -70,6 +71,7 @@ final class SwiftSyntaxRuleTests: XCTestCase { } func testArbitraryFoldArgument() { + // Silently fail because the macro definition explicitly requires a bool assertMacroExpansion( """ @SwiftSyntaxRule(foldExpressions: variable) @@ -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