Skip to content

Commit

Permalink
Pass options to visitor (#5811)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Sep 29, 2024
1 parent 36b88c5 commit 1ec3fdc
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 75 deletions.
41 changes: 22 additions & 19 deletions Source/SwiftLintFramework/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ extension Configuration {
-> [Configuration: [SwiftLintFile]] {
if files.isEmpty && !visitor.allowZeroLintableFiles {
throw SwiftLintError.usageError(
description: "No lintable files found at paths: '\(visitor.paths.joined(separator: ", "))'"
description: "No lintable files found at paths: '\(visitor.options.paths.joined(separator: ", "))'"
)
}

Expand Down Expand Up @@ -141,13 +141,13 @@ extension Configuration {
let counter = CounterActor()
let total = linters.filter(\.isCollecting).count
let progress = ProgressBar(count: total)
if visitor.showProgressBar && total > 0 {
if visitor.options.progress && total > 0 {
await progress.initialize()
}
let collect = { (linter: Linter) -> CollectedLinter? in
let skipFile = visitor.shouldSkipFile(atPath: linter.file.path)
if !visitor.quiet && linter.isCollecting {
if visitor.showProgressBar {
if !visitor.options.quiet && linter.isCollecting {
if visitor.options.progress {
await progress.printNext()
} else if let filePath = linter.file.path {
let outputFilename = self.outputFilename(for: filePath, duplicateFileNames: duplicateFileNames)
Expand Down Expand Up @@ -185,17 +185,19 @@ extension Configuration {
duplicateFileNames: Set<String>) async -> [SwiftLintFile] {
let counter = CounterActor()
let progress = ProgressBar(count: linters.count)
if visitor.showProgressBar {
if visitor.options.progress {
await progress.initialize()
}
let visit = { (linter: CollectedLinter) -> SwiftLintFile in
if !visitor.quiet {
if visitor.showProgressBar {
if !visitor.options.quiet {
if visitor.options.progress {
await progress.printNext()
} else if let filePath = linter.file.path {
let outputFilename = self.outputFilename(for: filePath, duplicateFileNames: duplicateFileNames)
let visited = await counter.next()
queuedPrintError("\(visitor.action) '\(outputFilename)' (\(visited)/\(linters.count))")
queuedPrintError(
"\(visitor.options.capitalizedVerb) '\(outputFilename)' (\(visited)/\(linters.count))"
)
}
}

Expand All @@ -210,45 +212,46 @@ extension Configuration {
}

fileprivate func getFiles(with visitor: LintableFilesVisitor) async throws -> [SwiftLintFile] {
if visitor.useSTDIN {
let options = visitor.options
if options.useSTDIN {
let stdinData = FileHandle.standardInput.readDataToEndOfFile()
if let stdinString = String(data: stdinData, encoding: .utf8) {
return [SwiftLintFile(contents: stdinString)]
}
throw SwiftLintError.usageError(description: "stdin isn't a UTF8-encoded string")
}
if visitor.useScriptInputFiles {
if options.useScriptInputFiles {
let files = try scriptInputFiles()
guard visitor.forceExclude else {
guard options.forceExclude else {
return files
}

let scriptInputPaths = files.compactMap(\.path)

if visitor.useExcludingByPrefix {
if options.useExcludingByPrefix {
return filterExcludedPathsByPrefix(in: scriptInputPaths)
.map(SwiftLintFile.init(pathDeferringReading:))
}
return filterExcludedPaths(excludedPaths(), in: scriptInputPaths)
.map(SwiftLintFile.init(pathDeferringReading:))
}
if !visitor.quiet {
if !options.quiet {
let filesInfo: String
if visitor.paths.isEmpty || visitor.paths == [""] {
if options.paths.isEmpty || options.paths == [""] {
filesInfo = "in current working directory"
} else {
filesInfo = "at paths \(visitor.paths.joined(separator: ", "))"
filesInfo = "at paths \(options.paths.joined(separator: ", "))"
}

queuedPrintError("\(visitor.action) Swift files \(filesInfo)")
queuedPrintError("\(options.capitalizedVerb) Swift files \(filesInfo)")
}
let excludeLintableFilesBy = visitor.useExcludingByPrefix
let excludeLintableFilesBy = options.useExcludingByPrefix
? Configuration.ExcludeBy.prefix
: .paths(excludedPaths: excludedPaths())
return visitor.paths.flatMap {
return options.paths.flatMap {
self.lintableFiles(
inPath: $0,
forceExclude: visitor.forceExclude,
forceExclude: options.forceExclude,
excludeBy: excludeLintableFilesBy)
}
}
Expand Down
9 changes: 5 additions & 4 deletions Source/SwiftLintFramework/LintOrAnalyzeCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,11 @@ package struct LintOrAnalyzeOptions {
}

var verb: String {
if autocorrect {
return "correcting"
}
return mode.verb
autocorrect ? "correcting" : mode.verb
}

var capitalizedVerb: String {
verb.capitalized
}
}

Expand Down
69 changes: 17 additions & 52 deletions Source/SwiftLintFramework/LintableFilesVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,78 +68,43 @@ private func resolveParamsFiles(args: [String]) -> [String] {
}

struct LintableFilesVisitor {
let paths: [String]
let action: String
let useSTDIN: Bool
let quiet: Bool
let showProgressBar: Bool
let useScriptInputFiles: Bool
let forceExclude: Bool
let useExcludingByPrefix: Bool
let options: LintOrAnalyzeOptions
let cache: LinterCache?
let parallel: Bool
let allowZeroLintableFiles: Bool
let mode: LintOrAnalyzeModeWithCompilerArguments
let parallel: Bool
let block: (CollectedLinter) async -> Void
let allowZeroLintableFiles: Bool

private init(paths: [String],
action: String,
useSTDIN: Bool,
quiet: Bool,
showProgressBar: Bool,
useScriptInputFiles: Bool,
forceExclude: Bool,
useExcludingByPrefix: Bool,
private init(options: LintOrAnalyzeOptions,
cache: LinterCache?,
compilerInvocations: CompilerInvocations?,
allowZeroLintableFiles: Bool,
block: @escaping (CollectedLinter) async -> Void) {
self.paths = resolveParamsFiles(args: paths)
self.action = action
self.useSTDIN = useSTDIN
self.quiet = quiet
self.showProgressBar = showProgressBar
self.useScriptInputFiles = useScriptInputFiles
self.forceExclude = forceExclude
self.useExcludingByPrefix = useExcludingByPrefix
block: @escaping (CollectedLinter) async -> Void) throws {
self.options = options
self.cache = cache
if let compilerInvocations {
self.mode = .analyze(allCompilerInvocations: compilerInvocations)
if options.mode == .lint {
self.mode = .lint
self.parallel = true
} else {
self.mode = .analyze(allCompilerInvocations: try Self.loadCompilerInvocations(options))
// SourceKit had some changes in 5.6 that makes it ~100x more expensive
// to process files concurrently. By processing files serially, it's
// only 2x slower than before.
self.parallel = SwiftVersion.current < .fiveDotSix
} else {
self.mode = .lint
self.parallel = true
}
self.block = block
self.allowZeroLintableFiles = allowZeroLintableFiles
self.block = block
}

static func create(_ options: LintOrAnalyzeOptions,
cache: LinterCache?,
allowZeroLintableFiles: Bool,
block: @escaping (CollectedLinter) async -> Void)
throws -> Self {
block: @escaping (CollectedLinter) async -> Void) throws -> Self {
try Signposts.record(name: "LintableFilesVisitor.Create") {
let compilerInvocations: CompilerInvocations?
if options.mode == .lint {
compilerInvocations = nil
} else {
compilerInvocations = try loadCompilerInvocations(options)
}

return Self(
paths: options.paths, action: options.verb.bridge().capitalized,
useSTDIN: options.useSTDIN, quiet: options.quiet,
showProgressBar: options.progress,
useScriptInputFiles: options.useScriptInputFiles,
forceExclude: options.forceExclude,
useExcludingByPrefix: options.useExcludingByPrefix,
try Self(
options: options,
cache: cache,
compilerInvocations: compilerInvocations,
allowZeroLintableFiles: allowZeroLintableFiles, block: block
allowZeroLintableFiles: allowZeroLintableFiles,
block: block
)
}
}
Expand Down

0 comments on commit 1ec3fdc

Please sign in to comment.