Skip to content

Commit

Permalink
Merge pull request #1730 from marcelofabri/bugfix-1719
Browse files Browse the repository at this point in the history
Make `file_header` rule ignore doc comments.
  • Loading branch information
marcelofabri authored Aug 1, 2017
2 parents f0f746f + d79f642 commit cca1060
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1714](https://github.com/realm/SwiftLint/issues/1714)

* Make `file_header` rule ignore doc comments.
[Marcelo Fabri](https://github.com/marcelofabri)
[#1719](https://github.com/realm/SwiftLint/issues/1719)

##### Bug Fixes

* Fix false positive on `redundant_discardable_let` rule when using
Expand Down
8 changes: 7 additions & 1 deletion Source/SwiftLintFramework/Rules/FileHeaderRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct FileHeaderRule: ConfigurationProviderRule, OptInRule {
var lastToken: SyntaxToken?

for token in file.syntaxTokensByLines.joined() {
guard let kind = SyntaxKind(rawValue: token.type), kind.isCommentLike else {
guard let kind = SyntaxKind(rawValue: token.type), kind.isFileHeaderKind else {
// found a token that is not a comment, which means it's not the top of the file
// so we can just skip the remaining tokens
break
Expand Down Expand Up @@ -111,3 +111,9 @@ public struct FileHeaderRule: ConfigurationProviderRule, OptInRule {
}
}
}

private extension SyntaxKind {
var isFileHeaderKind: Bool {
return self == .comment || self == .commentURL
}
}
4 changes: 3 additions & 1 deletion Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,10 @@ extension FileHeaderRuleTests {
("testFileHeaderWithDefaultConfiguration", testFileHeaderWithDefaultConfiguration),
("testFileHeaderWithRequiredString", testFileHeaderWithRequiredString),
("testFileHeaderWithRequiredPattern", testFileHeaderWithRequiredPattern),
("testFileHeaderWithRequiredStringAndURLComment", testFileHeaderWithRequiredStringAndURLComment),
("testFileHeaderWithForbiddenString", testFileHeaderWithForbiddenString),
("testFileHeaderWithForbiddenPattern", testFileHeaderWithForbiddenPattern)
("testFileHeaderWithForbiddenPattern", testFileHeaderWithForbiddenPattern),
("testFileHeaderWithForbiddenPatternAndDocComment", testFileHeaderWithForbiddenPatternAndDocComment)
]
}

Expand Down
45 changes: 39 additions & 6 deletions Tests/SwiftLintFrameworkTests/FileHeaderRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ class FileHeaderRuleTests: XCTestCase {
func testFileHeaderWithRequiredString() {
let nonTriggeringExamples = [
"// **Header",
"//\n // **Header"
"//\n// **Header"
]
let triggeringExamples = [
"↓// Copyright\n",
"let foo = \"**Header\"",
"let foo = 2 // **Header",
"let foo = 2\n // **Header",
"let foo = 2\n// **Header",
"let foo = 2 // **Header"
]
let description = FileHeaderRule.description
Expand All @@ -39,7 +39,7 @@ class FileHeaderRuleTests: XCTestCase {
func testFileHeaderWithRequiredPattern() {
let nonTriggeringExamples = [
"// Copyright © 2016 Realm",
"//\n // Copyright © 2016 Realm"
"//\n// Copyright © 2016 Realm"
]
let triggeringExamples = [
"↓// Copyright\n",
Expand All @@ -54,17 +54,33 @@ class FileHeaderRuleTests: XCTestCase {
stringDoesntViolate: false, skipCommentTests: true, testMultiByteOffsets: false)
}

func testFileHeaderWithRequiredStringAndURLComment() {
let nonTriggeringExamples = [
"/* Check this url: https://github.com/realm/SwiftLint */"
]
let triggeringExamples = [
"/* Check this url: https://github.com/apple/swift */"
]
let description = FileHeaderRule.description
.with(nonTriggeringExamples: nonTriggeringExamples)
.with(triggeringExamples: triggeringExamples)

let config = ["required_string": "/* Check this url: https://github.com/realm/SwiftLint */"]
verifyRule(description, ruleConfiguration: config,
stringDoesntViolate: false, skipCommentTests: true, testMultiByteOffsets: false)
}

func testFileHeaderWithForbiddenString() {
let nonTriggeringExamples = [
"// Copyright\n",
"let foo = \"**All rights reserved.\"",
"let foo = 2 // **All rights reserved.",
"let foo = 2\n // **All rights reserved.",
"let foo = 2\n// **All rights reserved.",
"let foo = 2 // **All rights reserved."
]
let triggeringExamples = [
"// ↓**All rights reserved.",
"//\n // ↓**All rights reserved."
"//\n// ↓**All rights reserved."
]
let description = FileHeaderRule.description
.with(nonTriggeringExamples: nonTriggeringExamples)
Expand All @@ -84,7 +100,7 @@ class FileHeaderRuleTests: XCTestCase {
]
let triggeringExamples = [
"//↓ FileHeaderRuleTests.swift",
"//\n //↓ FileHeaderRuleTests.swift"
"//\n//↓ FileHeaderRuleTests.swift"
]
let description = FileHeaderRule.description
.with(nonTriggeringExamples: nonTriggeringExamples)
Expand All @@ -93,4 +109,21 @@ class FileHeaderRuleTests: XCTestCase {
verifyRule(description, ruleConfiguration: ["forbidden_pattern": "\\s\\w+\\.swift"],
skipCommentTests: true)
}

func testFileHeaderWithForbiddenPatternAndDocComment() {
let nonTriggeringExamples = [
"/// This is great tool.\nclass GreatTool {}",
"class GreatTool {}"
]
let triggeringExamples = [
"↓// FileHeaderRuleTests.swift",
"↓//\n// FileHeaderRuleTests.swift"
]
let description = FileHeaderRule.description
.with(nonTriggeringExamples: nonTriggeringExamples)
.with(triggeringExamples: triggeringExamples)

verifyRule(description, ruleConfiguration: ["forbidden_pattern": ".?"],
skipCommentTests: true, skipDisableCommandTests: true, testMultiByteOffsets: false)
}
}
8 changes: 7 additions & 1 deletion Tests/SwiftLintFrameworkTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extension XCTestCase {
stringDoesntViolate: Bool = true,
skipCommentTests: Bool = false,
skipStringTests: Bool = false,
skipDisableCommandTests: Bool = false,
testMultiByteOffsets: Bool = true,
testShebang: Bool = true) {
guard let config = makeConfig(ruleConfiguration, ruleDescription.identifier) else {
Expand Down Expand Up @@ -184,7 +185,12 @@ extension XCTestCase {
)
}

let disableCommands = ruleDescription.allIdentifiers.map { "// swiftlint:disable \($0)\n" }
let disableCommands: [String]
if skipDisableCommandTests {
disableCommands = []
} else {
disableCommands = ruleDescription.allIdentifiers.map { "// swiftlint:disable \($0)\n" }
}

// "disable" commands doesn't violate
for command in disableCommands {
Expand Down

0 comments on commit cca1060

Please sign in to comment.