Rule
public protocol Rule : Sendable
An executable value that can identify issues (violations) in Swift source code.
-
The type of the configuration used to configure this rule.
Declaration
Swift
associatedtype ConfigurationType : RuleConfiguration -
A verbose description of many of this rule’s properties.
Declaration
Swift
static var description: RuleDescription { get } -
This rule’s configuration.
Declaration
Swift
var configuration: ConfigurationType { get set } -
shouldLintEmptyFilesDefault implementationWhether this rule should be used on empty files. Defaults to
false.Default Implementation
Declaration
Swift
var shouldLintEmptyFiles: Bool { get } -
A default initializer for rules. All rules need to be trivially initializable.
Declaration
Swift
init() -
init(configuration:Default implementation) Creates a rule by applying its configuration.
Throws
Throws if the configuration didn’t match the expected format.
Default Implementation
Declaration
Swift
init(configuration: Any) throwsParameters
configurationThe untyped configuration value to apply.
-
createConfigurationDescription(exclusiveOptions:Default implementation) Create a description of how this rule has been configured to run.
Default Implementation
Declaration
Swift
func createConfigurationDescription(exclusiveOptions: Set<String>) -> RuleConfigurationDescriptionParameters
exclusiveOptionsA set of options that should be excluded from the description.
Return Value
A description of the rule’s configuration.
-
validate(file:Default implementationcompilerArguments: ) Executes the rule on a file and returns any violations to the rule’s expectations.
Default Implementation
Declaration
Swift
func validate(file: SwiftLintFile, compilerArguments: [String]) -> [StyleViolation]Parameters
fileThe file for which to execute the rule.
compilerArgumentsThe compiler arguments needed to compile this file.
Return Value
All style violations to the rule’s expectations.
-
Executes the rule on a file and returns any violations to the rule’s expectations.
Declaration
Swift
func validate(file: SwiftLintFile) -> [StyleViolation]Parameters
fileThe file for which to execute the rule.
Return Value
All style violations to the rule’s expectations.
-
isEqualTo(_:Default implementation) Whether or not the specified rule is equivalent to the current rule.
Default Implementation
Declaration
Swift
func isEqualTo(_ rule: some Rule) -> BoolParameters
ruleThe
rulevalue to compare against.Return Value
Whether or not the specified rule is equivalent to the current rule.
-
collectInfo(for:Default implementationinto: compilerArguments: ) Collects information for the specified file in a storage object, to be analyzed by a
CollectedLinter.Note
This function is called by the linter and is always implemented in extensions.
Default Implementation
Declaration
Swift
func collectInfo(for file: SwiftLintFile, into storage: RuleStorage, compilerArguments: [String])Parameters
fileThe file for which to collect info.
storageThe storage object where collected info should be saved.
compilerArgumentsThe compiler arguments needed to compile this file.
-
validate(file:Default implementationusing: compilerArguments: ) Executes the rule on a file after collecting file info for all files and returns any violations to the rule’s expectations.
Note
This function is called by the linter and is always implemented in extensions.
Default Implementation
Declaration
Swift
func validate(file: SwiftLintFile, using storage: RuleStorage, compilerArguments: [String]) -> [StyleViolation]Parameters
fileThe file for which to execute the rule.
storageThe storage object containing all collected info.
compilerArgumentsThe compiler arguments needed to compile this file.
Return Value
All style violations to the rule’s expectations.
-
canBeDisabled(violation:Default implementationby: ) Checks if a style violation can be disabled by a command specifying a rule ID. Only the rule can claim that for sure since it knows all the possible identifiers.
Default Implementation
Declaration
Swift
func canBeDisabled(violation: StyleViolation, by ruleID: RuleIdentifier) -> BoolParameters
violationA style violation.
ruleIDThe name of a rule as used in a disable command.
Return Value
A boolean value indicating whether the violation can be disabled by the given ID.
-
isEnabled(in:Default implementationfor: ) Checks if a the rule is enabled in a given region. A specific rule ID can be provided in case a rule supports more than one identifier.
Default Implementation
Declaration
Swift
func isEnabled(in region: Region, for ruleID: String) -> BoolParameters
regionThe region to check.
ruleIDRule identifier deviating from the default rule’s name.
Return Value
A boolean value indicating whether the rule is enabled in the given region.
-
notifyRuleDisabledOnce()Default implementationPrints a warning to the console once about the rule being disabled due to SourceKit being unavailable. The default implementation does nothing. Rules that depend on SourceKit should override this appropriately.
Default Implementation
Declaration
Swift
func notifyRuleDisabledOnce() -
cacheDescriptionExtension methodThe cache description which will be used to determine if a previous cached value is still valid given the new cache value.
Declaration
Swift
var cacheDescription: String { get } -
identifierExtension methodThe rule’s unique identifier which is the same as
Rule.description.identifier.Declaration
Swift
static var identifier: String { get } -
requiresSourceKitExtension methodWhether this rule requires SourceKit to operate. Returns false if the rule conforms to SourceKitFreeRule or if it conforms to ConditionallySourceKitFree and is currently configured to not require SourceKit.
Declaration
Swift
var requiresSourceKit: Bool { get }
View on GitHub
Install in Dash