Skip to content

Commit

Permalink
Fix multibyte handling in many rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Dec 25, 2015
1 parent 0c7e729 commit 8d529f1
Show file tree
Hide file tree
Showing 27 changed files with 67 additions and 37 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
## Master

##### Breaking

* Remove `Location.init(file:offset:)` in favor of the more explicit
`Location.init(file:byteOffset:)` & `Location.init(file:characterOffset:)`.
[JP Simard](https://github.com/jpsim)

##### Enhancements

* None.

##### Bug Fixes

* Fix multibyte handling in many rules.
[JP Simard](https://github.com/jpsim)
[#279](https://github.com/realm/SwiftLint/issues/279)

## 0.5.3: Mountain Scent

##### Breaking
Expand All @@ -17,8 +35,9 @@

##### Bug Fixes

* Fix false positives in ValidDocsRule.
* Fix false positives in ValidDocsRule.
[JP Simard](https://github.com/jpsim)
[#267](https://github.com/realm/SwiftLint/issues/267)

## 0.5.2: Snuggle™

Expand Down
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "jpsim/SourceKitten"
github "jpsim/SourceKitten" "master"
2 changes: 1 addition & 1 deletion Cartfile.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ github "jpsim/SwiftXPC" "1.1.0"
github "behrang/YamlSwift" "1.2.4"
github "jspahrsummers/xcconfigs" "0.8.1"
github "Carthage/Commandant" "0.7.0"
github "jpsim/SourceKitten" "0.7.2"
github "jpsim/SourceKitten" "e6c66b4d19e688d8b57459092f7a300f3be7a2df"
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Extensions/File+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ extension File {
let fileRegions = regions()
let violatingRanges = violatingRanges.filter { range in
let region = fileRegions.filter {
$0.contains(Location(file: self, offset: range.location))
$0.contains(Location(file: self, characterOffset: range.location))
}.first
return region?.isRuleEnabled(rule) ?? true
}
Expand Down
13 changes: 12 additions & 1 deletion Source/SwiftLintFramework/Models/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ public struct Location: CustomStringConvertible, Comparable {
self.character = character
}

public init(file: File, offset: Int) {
public init(file: File, byteOffset offset: Int) {
self.file = file.path
if let lineAndCharacter = file.contents.lineAndCharacterForByteOffset(offset) {
line = lineAndCharacter.line
character = lineAndCharacter.character
} else {
line = nil
character = nil
}
}

public init(file: File, characterOffset offset: Int) {
self.file = file.path
if let lineAndCharacter = file.contents.lineAndCharacterForCharacterOffset(offset) {
line = lineAndCharacter.line
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/ClosingBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct ClosingBraceRule: CorrectableRule {
public func validateFile(file: File) -> [StyleViolation] {
return file.violatingClosingBraceRanges().map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}

Expand Down Expand Up @@ -68,7 +68,7 @@ public struct ClosingBraceRule: CorrectableRule {

return adjustedLocations.map {
Correction(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0))
location: Location(file: file, characterOffset: $0))
}
}
}
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/ColonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public struct ColonRule: CorrectableRule {

return violationRangesInFile(file, withPattern: pattern).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: range.location))
location: Location(file: file, characterOffset: range.location))
}
}

Expand Down Expand Up @@ -151,7 +151,7 @@ public struct ColonRule: CorrectableRule {
for range in matches.reverse() {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: "$1: $2")
let location = Location(file: file, offset: range.location)
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/CommaRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public struct CommaRule: Rule {

return file.matchPattern(pattern, excludingSyntaxKinds: excludingKinds).map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/ControlStatementRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct ControlStatementRule: Rule {
return nil
}
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: match.location))
location: Location(file: file, characterOffset: match.location))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/ForceCastRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public struct ForceCastRule: Rule {
public func validateFile(file: File) -> [StyleViolation] {
return file.matchPattern("as!", withSyntaxKinds: [.Keyword]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
severity: .Error, location: Location(file: file, offset: $0.location))
severity: .Error, location: Location(file: file, characterOffset: $0.location))
}
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/ForceTryRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct ForceTryRule: Rule {
public func validateFile(file: File) -> [StyleViolation] {
return file.matchPattern("try!", withSyntaxKinds: [.Keyword]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
severity: .Error, location: Location(file: file, offset: $0.location))
severity: .Error, location: Location(file: file, characterOffset: $0.location))
}
}
}
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public struct FunctionBodyLengthRule: ASTRule, ParameterizedRule {
if let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }),
let bodyOffset = (dictionary["key.bodyoffset"] as? Int64).flatMap({ Int($0) }),
let bodyLength = (dictionary["key.bodylength"] as? Int64).flatMap({ Int($0) }) {
let location = Location(file: file, offset: offset)
let startLine = file.contents.lineAndCharacterForCharacterOffset(bodyOffset)
let endLine = file.contents.lineAndCharacterForCharacterOffset(bodyOffset + bodyLength)
let location = Location(file: file, byteOffset: offset)
let startLine = file.contents.lineAndCharacterForByteOffset(bodyOffset)
let endLine = file.contents.lineAndCharacterForByteOffset(bodyOffset + bodyLength)
for parameter in parameters.reverse() {
if let startLine = startLine?.line, let endLine = endLine?.line
where endLine - startLine > parameter.value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public struct LegacyConstructorRule: Rule {

return file.matchPattern(pattern, withSyntaxKinds: [.Identifier]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/NestingRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct NestingRule: ASTRule {
var violations = [StyleViolation]()
let typeKinds: [SwiftDeclarationKind] = [.Class, .Struct, .Typealias, .Enum]
if let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }) {
let location = Location(file: file, offset: offset)
let location = Location(file: file, byteOffset: offset)
if level > 1 && typeKinds.contains(kind) {
violations.append(StyleViolation(ruleDescription: self.dynamicType.description,
location: location, reason: "Types should be nested at most 1 level deep"))
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/OpeningBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct OpeningBraceRule: CorrectableRule {
public func validateFile(file: File) -> [StyleViolation] {
return file.violatingOpeningBraceRanges().map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ public struct OpeningBraceRule: CorrectableRule {

return adjustedLocations.map {
Correction(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0))
location: Location(file: file, characterOffset: $0))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct OperatorFunctionWhitespaceRule: Rule {
return syntaxKinds.first == .Keyword
}.map { range, _ in
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: range.location))
location: Location(file: file, characterOffset: range.location))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct ReturnArrowWhitespaceRule: Rule {
let pattern = "\\)(\(spaceRegex)\\->\\s*|\\s\\->\(spaceRegex))\\S+"
return file.matchPattern(pattern, withSyntaxKinds: [.Typeidentifier]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct StatementPositionRule: Rule {

return file.matchPattern(pattern, withSyntaxKinds: [.Keyword]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/TodoRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct TodoRule: Rule {
return nil
}
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: range.location))
location: Location(file: file, characterOffset: range.location))
}
}
}
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/TrailingSemicolonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct TrailingSemicolonRule: CorrectableRule {
public func validateFile(file: File) -> [StyleViolation] {
return file.violatingTrailingSemicolonRanges().map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}

Expand All @@ -60,7 +60,7 @@ public struct TrailingSemicolonRule: CorrectableRule {
file.write(correctedContents)
return adjustedRanges.map {
Correction(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
location: Location(file: file, characterOffset: $0.location))
}
}
}
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Rules/TypeBodyLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public struct TypeBodyLengthRule: ASTRule, ParameterizedRule {
if let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }),
let bodyOffset = (dictionary["key.bodyoffset"] as? Int64).flatMap({ Int($0) }),
let bodyLength = (dictionary["key.bodylength"] as? Int64).flatMap({ Int($0) }) {
let location = Location(file: file, offset: offset)
let startLine = file.contents.lineAndCharacterForCharacterOffset(bodyOffset)
let endLine = file.contents.lineAndCharacterForCharacterOffset(bodyOffset + bodyLength)
let location = Location(file: file, byteOffset: offset)
let startLine = file.contents.lineAndCharacterForByteOffset(bodyOffset)
let endLine = file.contents.lineAndCharacterForByteOffset(bodyOffset + bodyLength)
for parameter in parameters.reverse() {
if let startLine = startLine?.line, let endLine = endLine?.line
where endLine - startLine > parameter.value {
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/TypeNameRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public struct TypeNameRule: ASTRule {
}
if let name = dictionary["key.name"] as? String,
let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }) {
let location = Location(file: file, offset: offset)
let location = Location(file: file, byteOffset: offset)
let name = name.nameStrippingLeadingUnderscoreIfPrivate(dictionary)
let nameCharacterSet = NSCharacterSet(charactersInString: name)
if !NSCharacterSet.alphanumericCharacterSet().isSupersetOfSet(nameCharacterSet) {
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/ValidDocsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public struct ValidDocsRule: Rule {
public func validateFile(file: File) -> [StyleViolation] {
return file.invalidDocOffsets(file.structure.dictionary).map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0))
location: Location(file: file, byteOffset: $0))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public struct VariableNameMaxLengthRule: ASTRule, ParameterizedRule {
dictionary: XPCDictionary) -> [StyleViolation] {
return file.validateVariableName(dictionary, kind: kind).map { name, offset in
let charCount = name.characters.count
for parameter in self.parameters.reverse() where charCount > parameter.value {
for parameter in parameters.reverse() where charCount > parameter.value {
return [StyleViolation(ruleDescription: self.dynamicType.description,
severity: parameter.severity,
location: Location(file: file, offset: offset),
location: Location(file: file, byteOffset: offset),
reason: "Variable name should be \(parameter.value) characters " +
"or less: currently \(charCount) characters")]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public struct VariableNameMinLengthRule: ASTRule, ParameterizedRule {
dictionary: XPCDictionary) -> [StyleViolation] {
return file.validateVariableName(dictionary, kind: kind).map { name, offset in
let charCount = name.characters.count
for parameter in self.parameters.reverse() where charCount < parameter.value {
for parameter in parameters.reverse() where charCount < parameter.value {
return [StyleViolation(ruleDescription: self.dynamicType.description,
severity: parameter.severity,
location: Location(file: file, offset: offset),
location: Location(file: file, byteOffset: offset),
reason: "Variable name should be \(parameter.value) characters " +
"or more: currently \(charCount) characters")]
}
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/VariableNameRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct VariableNameRule: ASTRule {
return file.validateVariableName(dictionary, kind: kind).map { name, offset in
let nameCharacterSet = NSCharacterSet(charactersInString: name)
let description = self.dynamicType.description
let location = Location(file: file, offset: offset)
let location = Location(file: file, byteOffset: offset)
if !NSCharacterSet.alphanumericCharacterSet().isSupersetOfSet(nameCharacterSet) {
return [StyleViolation(ruleDescription: description,
severity: .Error,
Expand Down

0 comments on commit 8d529f1

Please sign in to comment.