Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add content of the todo statement to message #765

Merged
merged 1 commit into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
instance variables to be `private`.
[Olivier Halligon](https://github.com/AliSoftware)

* Add content of the todo statement to message.
[J. Cheyo Jimenez](https://github.com/masters3d)
[#478](https://github.com/realm/SwiftLint/issues/478)

* Add `LegacyNSGeometryFunctionsRule` rule. Add `NSSize`, `NSPoint`, and
`NSRect` constants and constructors to existing rules.
[David Rönnqvist](https://github.com/d-ronnqvist)
Expand Down
36 changes: 35 additions & 1 deletion Source/SwiftLintFramework/Rules/TodoRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,48 @@ public struct TodoRule: ConfigurationProviderRule {
]
)

private func customMessage(lines: [Line], location: Location) -> String {
var reason = self.dynamicType.description.description

guard let lineIndex = location.line,
let currentLine = lines.filter({ $0.index == lineIndex }).first
else { return reason }

// customizing the reason message to be specific to fixme or todo
var message = currentLine.content
if currentLine.content.containsString("FIXME") {
reason = "FIXMEs should be avoided"
message = message.stringByReplacingOccurrencesOfString("FIXME", withString: "")
} else {
reason = "TODOs should be avoided"
message = message.stringByReplacingOccurrencesOfString("TODO", withString: "")
}
message = message.stringByReplacingOccurrencesOfString("//", withString: "")

// limiting the output length of todo message
let maxLengthOfMessage = 30
if message.utf16.count > maxLengthOfMessage {
let index = message.startIndex.advancedBy(maxLengthOfMessage)
reason += message.substringToIndex(index) + "..."
} else {
reason += message
}

return reason
}

public func validateFile(file: File) -> [StyleViolation] {
return file.matchPattern("\\b(TODO|FIXME)\\b").flatMap { range, syntaxKinds in
if !syntaxKinds.filter({ !$0.isCommentLike }).isEmpty {
return nil
}
let location = Location(file: file, characterOffset: range.location)
let reason = customMessage(file.lines, location: location)

return StyleViolation(ruleDescription: self.dynamicType.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
location: location,
reason: reason )
}
}
}