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