Macros
The following macros are available globally.
-
Macro to be attached to rule configurations. It generates the configuration parsing logic automatically based on the defined
@ConfigurationElements.Declaration
Swift
@attached(member, names: named(apply) , named(Parent)) public macro AutoConfigParser() = #externalMacro( module: "SwiftLintCoreMacros", type: "AutoConfigParser" ) -
Macro that lets an enum with a
Stringraw type automatically conform toAcceptableByConfigurationElement.Declaration
Swift
@attached(extension, conformances: AcceptableByConfigurationElement, names: named(init(fromAny:context:) ), named(asOption)) public macro AcceptableByConfigurationElement() = #externalMacro( module: "SwiftLintCoreMacros", type: "AcceptableByConfigurationElement" ) -
Macro that adds a conformance to the
SwiftSyntaxRuleprotocol and a defaultmakeVisitor(file:)implementation that creates a visitor defined in the rule struct.Declaration
Swift
@attached(extension, conformances: SwiftSyntaxRule, SwiftSyntaxCorrectableRule, OptInRule, Rule, names: named(makeVisitor(file:) ), named(preprocess(file:)), named(makeRewriter(file:))) public macro SwiftSyntaxRule(foldExpressions: Bool = false, explicitRewriter: Bool = false, correctable: Bool = false, optIn: Bool = false) = #externalMacro( module: "SwiftLintCoreMacros", type: "SwiftSyntaxRule" )Parameters
foldExpressionsSetting it to
trueadds an implementation ofpreprocess(file:)which folds expressions before they are passed to the visitor.explicitRewriterSet it to
trueto add amakeRewriter(file:)implementation which creates a rewriter defined in the rule struct. In this case, the rule automatically conforms toSwiftSyntaxCorrectableRule.correctableSet it to
trueto make the rule conform toSwiftSyntaxCorrectableRulewithout an explicit rewriter.optInSet it to
trueto make the rule conform toOptInRule. -
Macro that expands an array into an array of
Examples. Elements are usually string literals, but an existingExampleexpression (e.g."code".asExample(configuration:)) works too.The parameter is typed
[Example](ExampleisExpressibleByStringInterpolation), so an unsupported element such as anIntfails as a plain type error on the literal you wrote rather than as a macro-expansion failure.Declaration
-
Macro that expands a dictionary into a dictionary of
Examples. Keys and values are usually string literals, but an existingExampleexpression works too.Both key and value are typed
Example(which isExpressibleByStringInterpolationandHashable), so an unsupported key or value fails as a plain type error on the literal you wrote rather than as a macro-expansion failure.Declaration
-
example(configuration:testMultiByteOffsets: testWrappingInComment: testWrappingInString: testDisableCommand: testOnLinux: testOnWindows: excludeFromDocumentation: body: ) Macro that allows to define an example for a rule. It generates an
Exampleinstance with the provided code, configuration and other options. Valuable is that the code passed as the example’s body is compiled, thus making sure that the example is valid Swift code.Declaration
Swift
@freestanding(expression) public macro example(configuration: [String : any Sendable]? = nil, testMultiByteOffsets: Bool = true, testWrappingInComment: Bool = true, testWrappingInString: Bool = true, testDisableCommand: Bool = true, testOnLinux: Bool = true, testOnWindows: Bool = true, excludeFromDocumentation: Bool = false, body: () -> Void) -> Example = #externalMacro( module: "SwiftLintCoreMacros", type: "Example" )Parameters
configurationThe untyped configuration to apply to the rule, if deviating from the default configuration.
testMultiByteOffsetsWhether the example should be tested by prepending multi-byte grapheme clusters.
testWrappingInCommentWhether test shall verify that the example wrapped in a comment doesn’t trigger.
testWrappingInStringWhether tests shall verify that the example wrapped into a string doesn’t trigger.
testDisableCommandWhether tests shall verify that the disabled rule (comment in the example) doesn’t trigger.
testOnLinuxWhether the example should be tested on Linux.
testOnWindowsWhether the example should be tested on Windows.
excludeFromDocumentationWhether the example should be excluded from the rule’s documentation.
bodyThe body of the example, which is compiled to ensure that it is valid Swift code.
View on GitHub
Install in Dash
Macros Reference