Skip to content

Commit

Permalink
Ignore shebang in all rules
Browse files Browse the repository at this point in the history
By making a creative use of regions.
Fixes #1294.
  • Loading branch information
marcelofabri committed Jul 25, 2017
1 parent d6e7df2 commit 70fc1a1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -83,6 +83,10 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#54](https://github.com/realm/SwiftLint/issues/54)

* Shebang (`#!`) in the beginning of a file is now ignored by all rules.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1294](https://github.com/realm/SwiftLint/issues/1294)

##### Bug Fixes

* Fix false positive on `redundant_discardable_let` rule when using
Expand Down
11 changes: 10 additions & 1 deletion Source/SwiftLintFramework/Extensions/File+SwiftLint.swift
Expand Up @@ -52,13 +52,22 @@ extension File {
}
let contents = self.contents.bridge()
let pattern = "swiftlint:(enable|disable)(:previous|:this|:next)?\\ [^\\n]+"
return match(pattern: pattern, with: [.comment]).flatMap { range in
return skipShebangCommands() + match(pattern: pattern, with: [.comment]).flatMap { range in
return Command(string: contents, range: range)
}.flatMap { command in
return command.expand()
}
}

private func skipShebangCommands() -> [Command] {
guard contents.hasPrefix("#!") else {
return []
}

return Command(action: .disable, ruleIdentifiers: masterRuleList.allValidIdentifiers(),
line: 0, modifier: .next).expand()
}

fileprivate func endOf(next command: Command?) -> Location {
guard let nextCommand = command else {
return Location(file: path, line: .max, character: .max)
Expand Down
5 changes: 5 additions & 0 deletions Tests/SwiftLintFrameworkTests/RulesTests.swift
Expand Up @@ -198,6 +198,11 @@ class RulesTests: XCTestCase {

func testOperatorUsageWhitespace() {
verifyRule(OperatorUsageWhitespaceRule.description)

let description = OperatorUsageWhitespaceRule.description
.with(nonTriggeringExamples: ["#!/usr/bin/env swift\n"])
.with(triggeringExamples: []).with(corrections: [:])
verifyRule(description, skipCommentTests: true, skipStringTests: true, testMultiByteOffsets: false)
}

func testPrivateOverFilePrivate() {
Expand Down

0 comments on commit 70fc1a1

Please sign in to comment.