Skip to content

Commit

Permalink
Fix bug where nested configuration excluded files are ignored (#2648)
Browse files Browse the repository at this point in the history
Fixes #2447

When SwiftLint is linting files in `visitLintableFiles` in `Configuration+CommandLine`, it:

1. Gathers all lintable files in `getFiles`. This is where the exclusion of files occurs based on the parent configuration's exclusion list.
2. These files are grouped by their specific configuration in `groupFiles`. This is where configurations for each available file are determined (and if nested configurations exist, merged). After these configurations are determined and the files are grouped accordingly, no more files are excluded from the lintable files list. Even though a file's configuration thinks it should be excluded, these files are not removed from the list of lintable files, generating the bug.
3. Finally, each file is visited by the linter.

My solution is to skip files whose merged configurations specify they should be excluded in step 2 or `groupFiles`. Therefore, they will not be visited in step 3.
  • Loading branch information
Bruschidy54 authored and jpsim committed Apr 7, 2019
1 parent 834df26 commit 9396a0a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@

#### Bug Fixes

* Fix bug where SwiftLint ignores excluded files list in a nested configuration file.
[Dylan Bruschi](https://github.com/Bruschidy54)
[#2447](https://github.com/realm/SwiftLint/issues/2447)

* Fix false positives on `no_grouping_extension` rule when using `where`
clause.
[Almaz Ibragimov](https://github.com/almazrafi)
Expand Down
24 changes: 23 additions & 1 deletion Source/swiftlint/Extensions/Configuration+CommandLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,29 @@ extension Configuration {
let errorMessage = "No lintable files found at paths: '\(visitor.paths.joined(separator: ", "))'"
return .failure(.usageError(description: errorMessage))
}
return .success(Dictionary(grouping: files, by: configuration(for:)))

var groupedFiles = [Configuration: [File]]()
for file in files {
// Files whose configuration specifies they should be excluded will be skipped
let fileConfiguration = configuration(for: file)
let excludedPaths = fileConfiguration.excluded
.map { (fileConfiguration.rootPath ?? "").bridge().appendingPathComponent($0) }

let shouldSkip: Bool = excludedPaths.contains {
file.path?.bridge().pathComponents.starts(with: $0.bridge().pathComponents) ?? false
}

if !shouldSkip {
if var configuredFiles = groupedFiles[fileConfiguration] {
configuredFiles.append(file)
groupedFiles[fileConfiguration] = configuredFiles
} else {
groupedFiles[fileConfiguration] = [file]
}
}
}

return .success(groupedFiles)
}

private func visit(filesPerConfiguration: [Configuration: [File]],
Expand Down

0 comments on commit 9396a0a

Please sign in to comment.