Rule
public protocol Rule
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 }
-
shouldLintEmptyFiles
Default 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) throws
Parameters
configuration
The 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>) -> RuleConfigurationDescription
-
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
file
The file for which to execute the rule.
compilerArguments
The 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
file
The 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: any Rule) -> Bool
Parameters
rule
The
rule
value 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
file
The file for which to collect info.
storage
The storage object where collected info should be saved.
compilerArguments
The 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
file
The file for which to execute the rule.
storage
The storage object containing all collected info.
compilerArguments
The 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) -> Bool
Parameters
violation
A style violation.
ruleID
The 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) -> Bool
Parameters
region
The region to check.
ruleID
Rule identifier deviating from the default rule’s name.
Return Value
A boolean value indicating whether the rule is enabled in the given region.
-
cacheDescription
Extension 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 }
-
identifier
Extension methodThe rule’s unique identifier which is the same as
Rule.description.identifier
.Declaration
Swift
static var identifier: String { get }