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

Color literals count as single character towards line count #830

Merged
merged 9 commits into from
Oct 14, 2016
19 changes: 18 additions & 1 deletion Source/SwiftLintFramework/Rules/LineLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,24 @@ public struct LineLengthRule: ConfigurationProviderRule, SourceKitFreeRule {
if line.range.length < minValue {
return nil
}
let length = line.content.characters.count

var length = line.content.characters.count

// Check if a color literal exists in the line by trying to get range
if let colorLiteralRangeStart = line.content.rangeOfString("#colorLiteral("),
let colorLiteralRangeEnd = line.content.rangeOfString(")",
options: .LiteralSearch,
range: colorLiteralRangeStart.startIndex..<line.content.endIndex,
locale: nil) {
Copy link
Collaborator

@norio-nomura norio-nomura Oct 12, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add indent to lines between let rangeEnd… and locale:nil) {?


// Range was found, get substring of color literal range
let colorLiteralContent = line.content.substringWithRange(colorLiteralRangeStart.startIndex..<colorLiteralRangeEnd.endIndex)
Copy link
Collaborator

@norio-nomura norio-nomura Oct 8, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SwiftLintFrameworkTests.IntegrationTests fails that lint codes of SwiftLint itself by SwiftLint.
This line causes violation of line_length rule.
Other lines cause autocorrections of trailing_whitespace rule.
screenshot 2016-10-08 19 03 13

Could you please fix them?


// Reduce length so that literal only counts as one character
length = length - (colorLiteralContent.characters.count - 1)
}


for param in configuration.params where length > param.value {
return StyleViolation(ruleDescription: self.dynamicType.description,
severity: param.severity,
Expand Down