Skip to content

Commit

Permalink
Support linting only provided file paths with command plugins (#5879)
Browse files Browse the repository at this point in the history
This enables anyone to run SwiftLint on file paths provided via arguments without
installing it via Mint or Homebrew. This can be useful for custom Git hooks that run
only for modified files.
  • Loading branch information
dshikulin-mwb authored Dec 7, 2024
1 parent 40f9a2a commit 75c1322
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

* Support replacing identity expressions with `\.self` in `prefer_key_path` rule from Swift 6 on.
[SimplyDanny](https://github.com/SimplyDanny)

* Support linting only provided file paths with command plugins.
[DanSkeel](https://github.com/DanSkeel)

#### Bug Fixes

Expand Down
12 changes: 11 additions & 1 deletion Plugins/SwiftLintCommandPlugin/SwiftLintCommandPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,20 @@ extension SwiftLintCommandPlugin {
var argExtractor = ArgumentExtractor(arguments)
let targetNames = argExtractor.extractOption(named: "target")
let remainingArguments = argExtractor.remainingArguments
guard !targetNames.isEmpty, commandsNotExpectingPaths.isDisjoint(with: remainingArguments) else {

if !commandsNotExpectingPaths.isDisjoint(with: remainingArguments) {
try lintFiles(with: context, arguments: remainingArguments)
return
}
guard !targetNames.isEmpty else {
if let pathArgument = remainingArguments.last, FileManager.default.fileExists(atPath: pathArgument) {
Diagnostics.remark("No targets provided. Files provided in path arguments will be linted.")
try lintFiles(in: [], with: context, arguments: remainingArguments)
} else {
try lintFiles(with: context, arguments: remainingArguments)
}
return
}
for target in try context.targets(named: targetNames) {
try lintFiles(in: target.paths, for: target.name, with: context, arguments: remainingArguments)
}
Expand Down

0 comments on commit 75c1322

Please sign in to comment.