Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent SwiftSyntax string interpolation errors in tests #5258

Merged
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
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