Skip to content

Commit

Permalink
Prevent empty files from triggering rule violations (realm#3885)
Browse files Browse the repository at this point in the history
There are many valid cases to have an empty Swift source files, and these
shouldn't trigger violations.
  • Loading branch information
jpsim authored and coffmark committed Apr 11, 2022
1 parent c61ee82 commit 033f283
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@

#### Enhancements

* None.
* Empty files no longer trigger any violations.
[JP Simard](https://github.com/jpsim)
[#3854](https://github.com/realm/SwiftLint/issues/3854)

#### Bug Fixes

Expand Down
14 changes: 14 additions & 0 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ private extension Rule {
configuration: Configuration,
superfluousDisableCommandRule: SuperfluousDisableCommandRule?,
compilerArguments: [String]) -> LintResult? {
// Empty files shouldn't trigger violations
if file.isEmpty { return nil }

if !(self is SourceKitFreeRule) && file.sourcekitdFailed {
return nil
}
Expand Down Expand Up @@ -199,6 +202,11 @@ public struct CollectedLinter {

private func getStyleViolations(using storage: RuleStorage,
benchmark: Bool = false) -> ([StyleViolation], [(id: String, time: Double)]) {
guard !file.isEmpty else {
// Empty files shouldn't trigger violations
return ([], [])
}

if let cached = cachedStyleViolations(benchmark: benchmark) {
return cached
}
Expand Down Expand Up @@ -329,3 +337,9 @@ public struct CollectedLinter {
}
}
}

private extension SwiftLintFile {
var isEmpty: Bool {
contents.isEmpty || contents == "\n"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public struct LeadingWhitespaceRule: CorrectableRule, ConfigurationProviderRule,
description: "Files should not contain leading whitespace.",
kind: .style,
nonTriggeringExamples: [ Example("//\n") ],
triggeringExamples: [ Example("\n"), Example(" //\n") ],
triggeringExamples: [ Example("\n//\n"), Example(" //\n") ],
corrections: [Example("\n //"): Example("//")]
)

Expand Down
4 changes: 2 additions & 2 deletions Tests/SwiftLintFrameworkTests/CollectingRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CollectingRuleTests: XCTestCase {
}
}

XCTAssertFalse(violations(Example(""), config: Spec.configuration!).isEmpty)
XCTAssertFalse(violations(Example("_ = 0"), config: Spec.configuration!).isEmpty)
}

func testCollectsAllFiles() {
Expand Down Expand Up @@ -49,7 +49,7 @@ class CollectingRuleTests: XCTestCase {
}
}

XCTAssertFalse(violations(Example(""), config: Spec.configuration!, requiresFileOnDisk: true).isEmpty)
XCTAssertFalse(violations(Example("_ = 0"), config: Spec.configuration!, requiresFileOnDisk: true).isEmpty)
}

func testCorrects() {
Expand Down

0 comments on commit 033f283

Please sign in to comment.