mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
Evaluate arguments to boolean values
This commit is contained in:
@@ -19,58 +19,48 @@ enum SwiftSyntaxRule: ExtensionMacro {
|
||||
}
|
||||
"""
|
||||
),
|
||||
try makeExtension(dependingOn: node.foldArgument, in: context, with: """
|
||||
extension \(type) {
|
||||
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||
file.foldedSyntaxTree
|
||||
try node.foldArgument(context).ifTrue(
|
||||
try ExtensionDeclSyntax("""
|
||||
extension \(type) {
|
||||
func preprocess(file: SwiftLintFile) -> SourceFileSyntax? {
|
||||
file.foldedSyntaxTree
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
"""
|
||||
)
|
||||
),
|
||||
try makeExtension(dependingOn: node.explicitRewriterArgument, in: context, with: """
|
||||
extension \(type): SwiftSyntaxCorrectableRule {
|
||||
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
|
||||
Rewriter(configuration: configuration, file: file)
|
||||
try node.explicitRewriterArgument(context).ifTrue(
|
||||
try ExtensionDeclSyntax("""
|
||||
extension \(type): SwiftSyntaxCorrectableRule {
|
||||
func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter<ConfigurationType>? {
|
||||
Rewriter(configuration: configuration, file: file)
|
||||
}
|
||||
}
|
||||
}
|
||||
"""
|
||||
"""
|
||||
)
|
||||
),
|
||||
].compactMap { $0 }
|
||||
}
|
||||
|
||||
private static func makeExtension(
|
||||
dependingOn argument: ExprSyntax?,
|
||||
in context: some MacroExpansionContext,
|
||||
with content: SyntaxNodeString
|
||||
) throws -> ExtensionDeclSyntax? {
|
||||
if let argument {
|
||||
if argument.isBooleanLiteral {
|
||||
if argument.isTrueLiteral {
|
||||
return try ExtensionDeclSyntax(content)
|
||||
}
|
||||
} else {
|
||||
context.diagnose(SwiftLintCoreMacroError.noBooleanLiteral.diagnose(at: argument))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
private extension AttributeSyntax {
|
||||
var foldArgument: ExprSyntax? {
|
||||
findArgument(withName: "foldExpressions")
|
||||
func foldArgument(_ context: some MacroExpansionContext) -> Bool {
|
||||
findArgument(withName: "foldExpressions", in: context)
|
||||
}
|
||||
|
||||
var explicitRewriterArgument: ExprSyntax? {
|
||||
findArgument(withName: "explicitRewriter")
|
||||
func explicitRewriterArgument(_ context: some MacroExpansionContext) -> Bool {
|
||||
findArgument(withName: "explicitRewriter", in: context)
|
||||
}
|
||||
|
||||
private func findArgument(withName name: String) -> ExprSyntax? {
|
||||
private func findArgument(withName name: String, in context: some MacroExpansionContext) -> Bool {
|
||||
if case let .argumentList(args) = arguments, let first = args.first(where: { $0.label?.text == name }) {
|
||||
first.expression
|
||||
} else {
|
||||
nil
|
||||
let expr = first.expression
|
||||
if expr.isBooleanLiteral {
|
||||
return expr.isTrueLiteral
|
||||
}
|
||||
context.diagnose(SwiftLintCoreMacroError.noBooleanLiteral.diagnose(at: expr))
|
||||
}
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,3 +73,9 @@ private extension ExprSyntax {
|
||||
`as`(BooleanLiteralExprSyntax.self)?.literal.text == "true"
|
||||
}
|
||||
}
|
||||
|
||||
private extension Bool {
|
||||
func ifTrue<P: SyntaxProtocol>(_ result: @autoclosure () throws -> P) rethrows -> P? {
|
||||
self ? try result() : nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user