Skip to content

Commit

Permalink
Merge pull request #739 from mohpor/InitReturnDoc
Browse files Browse the repository at this point in the history
Fixed returns doc for init methods issue-557
  • Loading branch information
jpsim committed Aug 23, 2016
2 parents 8389aa2 + c7d0cf0 commit e8dc451
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@

* Adds 'ConditionalReturnsOnNewLineRule' rule.
[Rohan Dhaimade](https://github.com/HaloZero)

* Made `- returns:` doc optional for initializers.
[Mohpor](https://github.com/mohpor)
[#557](https://github.com/realm/SwiftLint/issues/557)

##### Bug Fixes

Expand Down
13 changes: 12 additions & 1 deletion Source/SwiftLintFramework/Rules/ValidDocsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func declarationReturns(declaration: String, kind: SwiftDeclarationKind? = nil)
guard let outsideBracesMatch = matchOutsideBraces(declaration) else {
return false
}

return outsideBracesMatch.containsString("->")
}

Expand All @@ -66,6 +65,12 @@ func matchOutsideBraces(declaration: String) -> NSString? {
return NSString(string: declaration).substringWithRange(outsideBracesMatch.range)
}

func declarationIsInitializer(declaration: String) -> Bool {
return !regex("^((.+)?\\s+)?init\\?*\\(.*\\)")
.matchesInString(declaration, options: [],
range: NSRange(location: 0, length: declaration.characters.count)).isEmpty
}

func commentHasBatchedParameters(comment: String) -> Bool {
return comment.lowercaseString.containsString("- parameters:")
}
Expand All @@ -76,11 +81,17 @@ func commentReturns(comment: String) -> Bool {
}

func missingReturnDocumentation(declaration: String, comment: String) -> Bool {
guard !declarationIsInitializer(declaration) else {
return false
}
return declarationReturns(declaration) && !commentReturns(comment)
}

func superfluousReturnDocumentation(declaration: String, comment: String,
kind: SwiftDeclarationKind) -> Bool {
guard !declarationIsInitializer(declaration) else {
return false
}
return !declarationReturns(declaration, kind: kind) && commentReturns(comment)
}

Expand Down

0 comments on commit e8dc451

Please sign in to comment.