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

avoid pre-reading files #5973

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
53 changes: 31 additions & 22 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,29 +66,33 @@ private extension Rule {
return superfluousDisableCommandViolations
}

// As we need the configuration to get custom identifiers.
// swiftlint:disable:next function_parameter_count function_body_length
func lint(file: SwiftLintFile,
regions: [Region],
benchmark: Bool,
storage: RuleStorage,
superfluousDisableCommandRule: SuperfluousDisableCommandRule?,
compilerArguments: [String]) -> LintResult? {
func shouldRun(onFile file: SwiftLintFile) -> Bool {
// We shouldn't lint if the current Swift version is not supported by the rule
guard SwiftVersion.current >= Self.description.minSwiftVersion else {
return nil
return false
}

// Empty files shouldn't trigger violations if `shouldLintEmptyFiles` is `false`
if file.isEmpty, !shouldLintEmptyFiles {
return nil
return false
}

if !(self is any SourceKitFreeRule) && file.sourcekitdFailed {
warnSourceKitFailedOnce()
return nil
return false
}

return true
}

// As we need the configuration to get custom identifiers.
// swiftlint:disable:next function_parameter_count
func lint(file: SwiftLintFile,
regions: [Region],
benchmark: Bool,
storage: RuleStorage,
superfluousDisableCommandRule: SuperfluousDisableCommandRule?,
compilerArguments: [String]) -> LintResult {
let ruleID = Self.identifier

let violations: [StyleViolation]
Expand Down Expand Up @@ -226,12 +230,7 @@ public struct Linter {
self.configuration = configuration
self.compilerArguments = compilerArguments

let fileIsEmpty = file.isEmpty
let rules = configuration.rules.filter { rule in
if fileIsEmpty, !rule.shouldLintEmptyFiles {
return false
}

if compilerArguments.isEmpty {
return !(rule is any AnalyzerRule)
}
Expand Down Expand Up @@ -307,11 +306,15 @@ public struct CollectedLinter {
let superfluousDisableCommandRule = rules.first(where: {
$0 is SuperfluousDisableCommandRule
}) as? SuperfluousDisableCommandRule
let validationResults = rules.parallelCompactMap {
$0.lint(file: file, regions: regions, benchmark: benchmark,
storage: storage,
superfluousDisableCommandRule: superfluousDisableCommandRule,
compilerArguments: compilerArguments)
let validationResults: [LintResult] = rules.parallelCompactMap {
guard $0.shouldRun(onFile: file) else {
return nil
}

return $0.lint(file: file, regions: regions, benchmark: benchmark,
storage: storage,
superfluousDisableCommandRule: superfluousDisableCommandRule,
compilerArguments: compilerArguments)
}
let undefinedSuperfluousCommandViolations = self.undefinedSuperfluousCommandViolations(
regions: regions, configuration: configuration,
Expand Down Expand Up @@ -378,7 +381,13 @@ public struct CollectedLinter {
}

var corrections = [Correction]()
for rule in rules.compactMap({ $0 as? any CorrectableRule }) {
for rule in rules.compactMap({ (rule: any Rule) -> (any CorrectableRule)? in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stylistically, I would prefer a separate if statement in the for block to check for shouldRun. Or you can make use of a where clause.

We could even check for CorrectableRule in the for block to avoid creating a new list and iterating twice.

guard rule.shouldRun(onFile: file) else {
return nil
}

return rule as? any CorrectableRule
}) {
let newCorrections = rule.correct(file: file, using: storage, compilerArguments: compilerArguments)
corrections += newCorrections
if newCorrections.isNotEmpty, !file.isVirtual {
Expand Down