Skip to content

Commit

Permalink
Merge branch 'master' into cache
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelofabri committed Jan 10, 2017
2 parents 94f48f4 + 480acd7 commit b7b905c
Show file tree
Hide file tree
Showing 57 changed files with 427 additions and 540 deletions.
11 changes: 11 additions & 0 deletions Source/SwiftLintFramework/Extensions/Dictionary+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ extension Dictionary where Key: ExpressibleByStringLiteral {
let array = self["key.inheritedtypes"] as? [SourceKitRepresentable] ?? []
return array.flatMap { ($0 as? [String: String])?["key.name"] }
}

internal func extractCallsToSuper(methodName: String) -> [String] {
let superCall = "super.\(methodName)"
return substructure.flatMap { elems in
guard let type = (elems["key.kind"] as? String).flatMap({ SwiftExpressionKind(rawValue: $0) }),
let name = elems["key.name"] as? String,
type == .call && superCall.contains(name)
else { return nil }
return name
}
}
}
8 changes: 4 additions & 4 deletions Source/SwiftLintFramework/Rules/AttributesRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public struct AttributesRule: ASTRule, OptInRule, ConfigurationProviderRule {
let attributesTokens = Set(attributesTokensWithRanges.map { $0.0 })

do {
let previousAttributesWithParameters = try attributesFromPreviousLines(lineNumber - 1,
let previousAttributesWithParameters = try attributesFromPreviousLines(lineNumber: lineNumber - 1,
file: file)
let previousAttributes = Set(previousAttributesWithParameters.map { $0.0 })

Expand All @@ -118,7 +118,7 @@ public struct AttributesRule: ASTRule, OptInRule, ConfigurationProviderRule {

let alwaysOnSameLineAttributes = configuration.alwaysOnSameLine
let alwaysOnNewLineAttributes =
createAlwaysOnNewLineAttributes(previousAttributesWithParameters,
createAlwaysOnNewLineAttributes(previousAttributes: previousAttributesWithParameters,
attributesTokens: attributesTokensWithRanges,
line: line, file: file)

Expand Down Expand Up @@ -146,7 +146,7 @@ public struct AttributesRule: ASTRule, OptInRule, ConfigurationProviderRule {
}
}

private func createAlwaysOnNewLineAttributes(_ previousAttributes: [(String, Bool)],
private func createAlwaysOnNewLineAttributes(previousAttributes: [(String, Bool)],
attributesTokens: [(String, NSRange)],
line: Line, file: File) -> Set<String> {
let attributesTokensWithParameters: [(String, Bool)] = attributesTokens.map {
Expand Down Expand Up @@ -189,7 +189,7 @@ public struct AttributesRule: ASTRule, OptInRule, ConfigurationProviderRule {

// returns an array with the token itself (i.e. "@objc") and whether it's parameterized
// note: the parameter is not contained in the token
private func attributesFromPreviousLines(_ lineNumber: Int,
private func attributesFromPreviousLines(lineNumber: Int,
file: File) throws -> [(String, Bool)] {
var currentLine = lineNumber - 1
var allTokens = [(String, Bool)]()
Expand Down
4 changes: 0 additions & 4 deletions Source/SwiftLintFramework/Rules/ClosingBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public struct ClosingBraceRule: CorrectableRule, ConfigurationProviderRule {

public func correct(file: File) -> [Correction] {
let violatingRanges = file.ruleEnabled(violatingRanges: file.violatingClosingBraceRanges(), for: self)
return writeToFile(file, violatingRanges: violatingRanges)
}

fileprivate func writeToFile(_ file: File, violatingRanges: [NSRange]) -> [Correction] {
var correctedContents = file.contents
var adjustedLocations = [Int]()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public struct ClosureEndIndentationRule: ASTRule, OptInRule, ConfigurationProvid
bodyLength > 0,
case let endOffset = offset + length - 1,
contents.substringWithByteRange(start: endOffset, length: 1) == "}",
let startOffset = startOffsetFor(dictionary: dictionary, file: file),
let startOffset = startOffset(forDictionary: dictionary, file: file),
let (startLine, _) = contents.lineAndCharacter(forByteOffset: startOffset),
let (endLine, endPosition) = contents.lineAndCharacter(forByteOffset: endOffset),
case let nameEndPosition = nameOffset + nameLength,
Expand Down Expand Up @@ -88,8 +88,7 @@ public struct ClosureEndIndentationRule: ASTRule, OptInRule, ConfigurationProvid
]
}

private func startOffsetFor(dictionary: [String: SourceKitRepresentable],
file: File) -> Int? {
private func startOffset(forDictionary dictionary: [String: SourceKitRepresentable], file: File) -> Int? {
guard let nameOffset = (dictionary["key.nameoffset"] as? Int64).flatMap({ Int($0) }),
let nameLength = (dictionary["key.namelength"] as? Int64).flatMap({ Int($0) }) else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,10 @@ public struct ClosureParameterPositionRule: ASTRule, ConfigurationProviderRule {
let rangeLength = paramOffset - rangeStart
let contents = file.contents.bridge()

guard let range = contents.byteRangeToNSRange(start: rangeStart,
length: rangeLength),
guard let range = contents.byteRangeToNSRange(start: rangeStart, length: rangeLength),
let match = regex.matches(in: file.contents, options: [], range: range).last?.range,
match.location != NSNotFound,
let braceOffset = contents.NSRangeToByteRange(start: match.location,
length: match.length)?.location,
let braceOffset = contents.NSRangeToByteRange(start: match.location, length: match.length)?.location,
let (braceLine, _) = contents.lineAndCharacter(forByteOffset: braceOffset),
let (paramLine, _) = contents.lineAndCharacter(forByteOffset: paramOffset),
braceLine != paramLine else {
Expand Down
10 changes: 5 additions & 5 deletions Source/SwiftLintFramework/Rules/ClosureSpacingRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public struct ClosureSpacingRule: Rule, ConfigurationProviderRule, OptInRule {

// this helps cut down the time to search through a file by
// skipping lines that do not have at least one { and one } brace
private func lineContainsBracesIn(_ range: NSRange, content: NSString) -> NSRange? {
private func lineContainsBraces(in range: NSRange, content: NSString) -> NSRange? {
let start = content.range(of: "{", options: [.literal], range: range)
guard start.length != 0 else { return nil }
let end = content.range(of: "}", options: [.literal, .backwards], range: range)
Expand All @@ -43,7 +43,7 @@ public struct ClosureSpacingRule: Rule, ConfigurationProviderRule, OptInRule {
}

// returns ranges of braces { or } in the same line
private func validBraces(_ file: File) -> [NSRange] {
private func validBraces(in file: File) -> [NSRange] {
let nsstring = file.contents.bridge()
let bracePattern = regex("\\{|\\}")
let linesTokens = file.syntaxTokensByLines
Expand All @@ -52,7 +52,7 @@ public struct ClosureSpacingRule: Rule, ConfigurationProviderRule, OptInRule {
// find all lines and accurences of open { and closed } braces
var linesWithBraces = [[NSRange]]()
for eachLine in file.lines {
guard let nsrange = lineContainsBracesIn(eachLine.range, content: nsstring) else {
guard let nsrange = lineContainsBraces(in: eachLine.range, content: nsstring) else {
continue
}

Expand All @@ -70,7 +70,7 @@ public struct ClosureSpacingRule: Rule, ConfigurationProviderRule, OptInRule {

public func validate(file: File) -> [StyleViolation] {
// match open braces to corresponding closing braces
func matchBraces(_ validBraceLocations: [NSRange]) -> [NSRange] {
func matchBraces(validBraceLocations: [NSRange]) -> [NSRange] {
if validBraceLocations.isEmpty { return [] }
var validBraces = validBraceLocations
var ranges = [NSRange]()
Expand All @@ -90,7 +90,7 @@ public struct ClosureSpacingRule: Rule, ConfigurationProviderRule, OptInRule {
}

// matching ranges of {}
let matchedUpBraces = matchBraces(validBraces(file))
let matchedUpBraces = matchBraces(validBraceLocations: validBraces(in: file))

var violationRanges = matchedUpBraces.filter {
// removes enclosing brances to just content
Expand Down
58 changes: 28 additions & 30 deletions Source/SwiftLintFramework/Rules/ColonRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public struct ColonRule: ASTRule, CorrectableRule, ConfigurationProviderRule {
)

public func validate(file: File) -> [StyleViolation] {
let violations = typeColonViolationRangesInFile(file, withPattern: pattern).flatMap { range in
let violations = typeColonViolationRanges(in: file, matching: pattern).flatMap { range in
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, characterOffset: range.location))
Expand All @@ -122,7 +122,7 @@ public struct ColonRule: ASTRule, CorrectableRule, ConfigurationProviderRule {
}

public func correct(file: File) -> [Correction] {
let violations = correctionRangesInFile(file)
let violations = correctionRanges(in: file)
let matches = violations.filter {
!file.ruleEnabled(violatingRanges: [$0.range], for: self).isEmpty
}
Expand Down Expand Up @@ -152,16 +152,14 @@ public struct ColonRule: ASTRule, CorrectableRule, ConfigurationProviderRule {

private typealias RangeWithKind = (range: NSRange, kind: ColonKind)

private func correctionRangesInFile(_ file: File) -> [RangeWithKind] {
let violations = typeColonViolationRangesInFile(file, withPattern: pattern).map {
private func correctionRanges(in file: File) -> [RangeWithKind] {
let violations = typeColonViolationRanges(in: file, matching: pattern).map {
(range: $0, kind: ColonKind.type)
}
let dictionary = file.structure.dictionary
let contents = file.contents.bridge()
let dictViolations: [RangeWithKind] = dictionaryColonViolationRangesInFile(file,
dictionary: dictionary).flatMap {
guard let range = contents.byteRangeToNSRange(start: $0.location,
length: $0.length) else {
let dictViolations: [RangeWithKind] = dictionaryColonViolationRanges(in: file, dictionary: dictionary).flatMap {
guard let range = contents.byteRangeToNSRange(start: $0.location, length: $0.length) else {
return nil
}
return (range: range, kind: ColonKind.dictionary)
Expand All @@ -180,21 +178,21 @@ extension ColonRule {
// If flexible_right_spacing is false or omitted, match 0 or 2+ whitespaces.
let spacingRegex = configuration.flexibleRightSpacing ? "(?:\\s{0})" : "(?:\\s{0}|\\s{2,})"

return "(\\w)" + // Capture an identifier
"(?:" + // start group
"\\s+" + // followed by whitespace
":" + // to the left of a colon
"\\s*" + // followed by any amount of whitespace.
"|" + // or
":" + // immediately followed by a colon
spacingRegex + // followed by right spacing regex
")" + // end group
"(" + // Capture a type identifier
"[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets
"\\S)" // lazily to the first non-whitespace character.
return "(\\w)" + // Capture an identifier
"(?:" + // start group
"\\s+" + // followed by whitespace
":" + // to the left of a colon
"\\s*" + // followed by any amount of whitespace.
"|" + // or
":" + // immediately followed by a colon
spacingRegex + // followed by right spacing regex
")" + // end group
"(" + // Capture a type identifier
"[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets
"\\S)" // lazily to the first non-whitespace character.
}

fileprivate func typeColonViolationRangesInFile(_ file: File, withPattern pattern: String) -> [NSRange] {
fileprivate func typeColonViolationRanges(in file: File, matching pattern: String) -> [NSRange] {
let nsstring = file.contents.bridge()
let commentAndStringKindsSet = Set(SyntaxKind.commentAndStringKinds())
return file.rangesAndTokens(matching: pattern).filter { _, syntaxTokens in
Expand All @@ -219,16 +217,16 @@ extension ColonRule {
public func validate(file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {

let ranges = dictionaryColonViolationRangesInFile(file, kind: kind, dictionary: dictionary)
let ranges = dictionaryColonViolationRanges(in: file, kind: kind, dictionary: dictionary)
return ranges.map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, byteOffset: $0.location))
}
}

fileprivate func dictionaryColonViolationRangesInFile(_ file: File,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
fileprivate func dictionaryColonViolationRanges(in file: File,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
guard configuration.applyToDictionaries else {
return []
}
Expand All @@ -238,15 +236,15 @@ extension ColonRule {
let kind = KindType(rawValue: kindString) else {
return []
}
return dictionaryColonViolationRangesInFile(file, dictionary: subDict) +
dictionaryColonViolationRangesInFile(file, kind: kind, dictionary: subDict)
return dictionaryColonViolationRanges(in: file, dictionary: subDict) +
dictionaryColonViolationRanges(in: file, kind: kind, dictionary: subDict)
}
}

private func dictionaryColonViolationRangesInFile(_ file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
private func dictionaryColonViolationRanges(in file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
guard kind == .dictionary,
let ranges = colonRanges(dictionary) else {
let ranges = colonRanges(dictionary: dictionary) else {
return []
}

Expand All @@ -266,7 +264,7 @@ extension ColonRule {
}
}

private func colonRanges(_ dictionary: [String: SourceKitRepresentable]) -> [NSRange]? {
private func colonRanges(dictionary: [String: SourceKitRepresentable]) -> [NSRange]? {
guard let elements = dictionary["key.elements"] as? [SourceKitRepresentable],
elements.count % 2 == 0 else {
return nil
Expand Down
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Rules/CommaRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
)

public func validate(file: File) -> [StyleViolation] {
return violationRangesInFile(file).map {
return violationRanges(in: file).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}

public func correct(file: File) -> [Correction] {
let violations = violationRangesInFile(file)
let violations = violationRanges(in: file)
let matches = file.ruleEnabled(violatingRanges: violations, for: self)
if matches.isEmpty { return [] }

Expand Down Expand Up @@ -94,7 +94,7 @@ public struct CommaRule: CorrectableRule, ConfigurationProviderRule {
private static let excludingSyntaxKindsForSecondCapture = SyntaxKind.commentKinds()
.map { $0.rawValue }

private func violationRangesInFile(_ file: File) -> [NSRange] {
private func violationRanges(in file: File) -> [NSRange] {
let contents = file.contents
let range = NSRange(location: 0, length: contents.utf16.count)
let syntaxMap = file.syntaxMap
Expand Down
11 changes: 5 additions & 6 deletions Source/SwiftLintFramework/Rules/CompilerProtocolInitRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule {

public func validate(file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
return violationRangesInFile(file, kind: kind, dictionary: dictionary).map {
return violationRanges(in: file, kind: kind, dictionary: dictionary).map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location))
}
}

private func violationRangesInFile(_ file: File,
kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
private func violationRanges(in file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [NSRange] {
guard kind == .call,
let name = dictionary["key.name"] as? String else {
return []
Expand All @@ -49,7 +48,7 @@ public struct CompilerProtocolInitRule: ASTRule, ConfigurationProviderRule {
for compilerProtocol in ExpressibleByCompiler.allProtocols {
guard compilerProtocol.initCallNames.contains(name),
case let arguments = dictionary.enclosedArguments.flatMap({ $0["key.name"] as? String }),
compilerProtocol.matchArguments(arguments),
compilerProtocol.match(arguments: arguments),
let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }),
let length = (dictionary["key.length"] as? Int64).flatMap({ Int($0) }),
let range = file.contents.bridge().byteRangeToNSRange(start: offset, length: length) else {
Expand Down Expand Up @@ -80,7 +79,7 @@ private struct ExpressibleByCompiler {
byExtendedGraphemeClusterLiteral, byStringLiteral,
byStringInterpolation, byDictionaryLiteral]

func matchArguments(_ arguments: [String]) -> Bool {
func match(arguments: [String]) -> Bool {
for item in self.arguments {
if item == arguments {
return true
Expand Down
Loading

0 comments on commit b7b905c

Please sign in to comment.