Skip to content

Commit

Permalink
update for swift 2
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Aug 27, 2015
1 parent 8835dbf commit e95625e
Show file tree
Hide file tree
Showing 44 changed files with 230 additions and 176 deletions.
31 changes: 29 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
Expand All @@ -10,11 +16,32 @@ build/
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
DerivedData
*.xcuserstate
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa
*.xcuserstate

# CocoaPods
#
# We recommend against adding the Pods directory to your .gitignore. However
# you should judge for yourself, the pros and cons are mentioned at:
# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
#
#Pods/

# Carthage
#
# Add this line if you want to avoid checking in source code from Carthage dependencies.
# Carthage/Checkouts

Carthage/Build

# SwiftLint

SwiftLint.pkg
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "Carthage/Checkouts/SourceKitten"]
path = Carthage/Checkouts/SourceKitten
url = https://github.com/jpsim/SourceKitten.git
[submodule "Carthage/Checkouts/Result"]
path = Carthage/Checkouts/Result
url = https://github.com/antitypical/Result.git
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "jpsim/SourceKitten" ~> 0.4
github "jpsim/SourceKitten" "swift-2.0"
2 changes: 1 addition & 1 deletion Cartfile.private
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github "Carthage/Commandant" ~> 0.5
github "Carthage/Commandant" "swift-2.0"
github "jspahrsummers/xcconfigs" >= 0.8
12 changes: 6 additions & 6 deletions Cartfile.resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github "LlamaKit/LlamaKit" "v0.6.0"
github "drmohundro/SWXMLHash" "1.0.0"
github "jpsim/SwiftXPC" "1.0.0"
github "jspahrsummers/xcconfigs" "0.8"
github "Carthage/Commandant" "0.5"
github "jpsim/SourceKitten" "0.4.5"
github "antitypical/Result" "0.6-beta.1"
github "drmohundro/SWXMLHash" "2203b05b1e093b1dc3e32114953f937a6e7aa5f5"
github "jpsim/SwiftXPC" "d32e70f1b35cfa833be85fd40e70401f4190f5b0"
github "jspahrsummers/xcconfigs" "0.8.1"
github "Carthage/Commandant" "cc47af4a9e7a7d56bd1202d52762c1c66184c936"
github "jpsim/SourceKitten" "4906120ed45b09d9720718bf666eab1e7ef30b31"
1 change: 1 addition & 0 deletions Carthage/Checkouts/Result
Submodule Result added at 18a10d
2 changes: 1 addition & 1 deletion Carthage/Checkouts/SourceKitten
Submodule SourceKitten updated 67 files
+3 −3 .gitmodules
+11 −0 .travis.yml
+51 −0 CHANGELOG.md
+2 −2 Cartfile
+1 −1 Cartfile.private
+5 −5 Cartfile.resolved
+1 −1 Carthage/Checkouts/Commandant
+0 −1 Carthage/Checkouts/LlamaKit
+1 −0 Carthage/Checkouts/Result
+1 −1 Carthage/Checkouts/SWXMLHash
+1 −1 Carthage/Checkouts/SwiftXPC
+1 −1 Carthage/Checkouts/xcconfigs
+5 −14 Makefile
+2 −0 README.md
+2 −2 Source/SourceKittenFramework/Array+SourceKitten.swift
+16 −46 Source/SourceKittenFramework/ClangTranslationUnit.swift
+4 −4 Source/SourceKittenFramework/Dictionary+Merge.swift
+61 −116 Source/SourceKittenFramework/File.swift
+2 −2 Source/SourceKittenFramework/Info.plist
+0 −90 Source/SourceKittenFramework/Language.swift
+21 −71 Source/SourceKittenFramework/Module.swift
+22 −21 Source/SourceKittenFramework/OffsetMap.swift
+21 −23 Source/SourceKittenFramework/Request.swift
+0 −22 Source/SourceKittenFramework/SequenceType+SourceKitten.swift
+65 −0 Source/SourceKittenFramework/SourceDeclaration.swift
+2 −0 Source/SourceKittenFramework/SourceKittenFramework.h
+64 −70 Source/SourceKittenFramework/String+SourceKitten.swift
+7 −7 Source/SourceKittenFramework/Structure.swift
+5 −1 Source/SourceKittenFramework/SwiftDeclarationKind.swift
+42 −75 Source/SourceKittenFramework/SwiftDocKey.swift
+10 −8 Source/SourceKittenFramework/SwiftDocs.swift
+15 −33 Source/SourceKittenFramework/SwiftXPC+JSON.swift
+10 −2 Source/SourceKittenFramework/SyntaxKind.swift
+21 −18 Source/SourceKittenFramework/SyntaxMap.swift
+8 −8 Source/SourceKittenFramework/SyntaxToken.swift
+166 −0 Source/SourceKittenFramework/Xcode.swift
+10 −2 Source/SourceKittenFramework/clang-c/BuildSystem.h
+87 −13 Source/SourceKittenFramework/clang-c/Index.h
+38 −0 Source/SourceKittenFramework/clang-c/Makefile
+4 −0 Source/SourceKittenFramework/clang-c/module.modulemap
+1 −1 Source/SourceKittenFramework/sourcekitd.swift
+1 −1 Source/SourceKittenFrameworkTests/ClangTranslationUnitTests.swift
+16 −0 Source/SourceKittenFrameworkTests/FileTests.swift
+275 −100 Source/SourceKittenFrameworkTests/Fixtures/Bicycle.json
+25 −25 Source/SourceKittenFrameworkTests/Fixtures/Bicycle.swift
+928 −808 Source/SourceKittenFrameworkTests/Fixtures/Commandant.json
+1 −1 Source/SourceKittenFrameworkTests/Info.plist
+19 −27 Source/SourceKittenFrameworkTests/ModuleTests.swift
+6 −3 Source/SourceKittenFrameworkTests/OffsetMapTests.swift
+110 −0 Source/SourceKittenFrameworkTests/SourceKitTests.swift
+30 −48 Source/SourceKittenFrameworkTests/StringTests.swift
+31 −21 Source/SourceKittenFrameworkTests/StructureTests.swift
+1 −1 Source/SourceKittenFrameworkTests/SwiftDocsTests.swift
+3 −3 Source/SourceKittenFrameworkTests/SyntaxTests.swift
+15 −19 Source/sourcekitten/Components.plist
+21 −22 Source/sourcekitten/DocCommand.swift
+2 −7 Source/sourcekitten/Errors.swift
+2 −2 Source/sourcekitten/Info.plist
+10 −11 Source/sourcekitten/StructureCommand.swift
+8 −12 Source/sourcekitten/SyntaxCommand.swift
+5 −6 Source/sourcekitten/VersionCommand.swift
+2 −2 SourceKitten.xcworkspace/contents.xcworkspacedata
+58 −0 SourceKitten.xcworkspace/xcshareddata/SourceKitten.xcscmblueprint
+2 −2 jazzy.sh
+68 −37 sourcekitten.xcodeproj/project.pbxproj
+8 −4 sourcekitten.xcodeproj/xcshareddata/xcschemes/SourceKittenFramework.xcscheme
+8 −5 sourcekitten.xcodeproj/xcshareddata/xcschemes/sourcekitten.xcscheme
2 changes: 1 addition & 1 deletion Carthage/Checkouts/SwiftXPC
2 changes: 1 addition & 1 deletion Carthage/Checkouts/xcconfigs
29 changes: 13 additions & 16 deletions Source/SwiftLintFramework/File+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,23 @@ extension File {
withSyntaxKinds syntaxKinds: [SyntaxKind]) -> [NSRange] {
return matchPattern(pattern).filter { _, kindsInRange in
return kindsInRange.count == syntaxKinds.count &&
filter(zip(kindsInRange, syntaxKinds), { $0.0 != $0.1 }).count == 0
zip(kindsInRange, syntaxKinds).filter({ $0.0 != $0.1 }).count == 0
}.map { $0.0 }
}

public func matchPattern(pattern: String) -> [(NSRange, [SyntaxKind])] {
return flatMap(NSRegularExpression(pattern: pattern, options: nil, error: nil)) { regex in
let range = NSRange(location: 0, length: count(contents.utf16))
let syntax = syntaxMap
let matches = regex.matchesInString(contents, options: nil, range: range)
return map(matches as? [NSTextCheckingResult]) { matches in
return matches.map { match in
let tokensInRange = syntax.tokens.filter {
NSLocationInRange($0.offset, match.range)
}
let kindsInRange = compact(map(tokensInRange) {
SyntaxKind(rawValue: $0.type)
})
return (match.range, kindsInRange)
}
let regex = try! NSRegularExpression(pattern: pattern, options: [])
let range = NSRange(location: 0, length: contents.utf16.count)
let syntax = syntaxMap
let matches = regex.matchesInString(contents, options: [], range: range)
return matches.map { match in
let tokensInRange = syntax.tokens.filter {
NSLocationInRange($0.offset, match.range)
}
} ?? []
let kindsInRange = tokensInRange.flatMap {
SyntaxKind(rawValue: $0.type)
}
return (match.range, kindsInRange)
}
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>io.realm.$(PRODUCT_NAME:rfc1034identifier)</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ public struct Linter {
]

public var styleViolations: [StyleViolation] {
return rules.flatMap { $0.validateFile(file) }
return rules.flatMap { $0.validateFile(self.file) }
}

public var ruleExamples: [RuleExample] {
return compact(rules.map { $0.example })
return rules.flatMap { $0.example }
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Location.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@

import SourceKittenFramework

public struct Location: Printable, Equatable {
public struct Location: CustomStringConvertible, Equatable {
public let file: String?
public let line: Int?
public let character: Int?
public var description: String {
// Xcode likes warnings and errors in the following format:
// {full_path_to_file}{:line}{:character}: {error,warning}: {content}
return (file ?? "<nopath>") +
(map(line, { ":\($0)" }) ?? "") +
(map(character, { ":\($0)" }) ?? "")
(line.map({ ":\($0)" }) ?? "") +
(character.map({ ":\($0)" }) ?? "")
}

public init(file: String?, line: Int? = nil, character: Int? = nil) {
Expand Down
11 changes: 4 additions & 7 deletions Source/SwiftLintFramework/NSFileManager+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
//

import Foundation
import SourceKittenFramework

extension NSFileManager {
public func allFilesRecursively(# directory: String) -> [String] {
let relativeFiles = (contentsOfDirectoryAtPath(directory, error: nil) as? [String] ?? []) +
(subpathsOfDirectoryAtPath(directory, error: nil) as? [String] ?? [])
return relativeFiles.map {
directory.stringByAppendingPathComponent($0)
}
public func allFilesRecursively(directory directory: String) -> [String] {
let relativeFiles = (try! contentsOfDirectoryAtPath(directory)) +
(try! subpathsOfDirectoryAtPath(directory))
return relativeFiles.map((directory as NSString).stringByAppendingPathComponent)
}
}
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Rules/ControlStatementRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ public struct ControlStatementRule: Rule {
public let identifier = "control_statement"

public func validateFile(file: File) -> [StyleViolation] {
return ["if", "for", "switch", "while"].flatMap { statementKind in
return ["if", "for", "switch", "while"].flatMap { statementKind -> [StyleViolation] in
let pattern = "\(statementKind)\\s*\\([^,]*\\)\\s*\\{"
return compact(file.matchPattern(pattern).map { match, syntaxKinds in
return file.matchPattern(pattern).flatMap { match, syntaxKinds in
if syntaxKinds.first != .Keyword {
return nil
}
Expand All @@ -25,7 +25,7 @@ public struct ControlStatementRule: Rule {
severity: .Low,
reason: "\(statementKind) statements shouldn't wrap their conditionals in " +
"parentheses.")
})
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/FileLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct FileLengthRule: ParameterizedRule {

public func validateFile(file: File) -> [StyleViolation] {
let lines = file.contents.lines()
for parameter in reverse(parameters) {
for parameter in parameters.reverse() {
if lines.count > parameter.value {
return [StyleViolation(type: .Length,
location: Location(file: file.path, line: lines.count),
Expand Down
19 changes: 10 additions & 9 deletions Source/SwiftLintFramework/Rules/FunctionBodyLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public struct FunctionBodyLengthRule: ASTRule, ParameterizedRule {
}

public func validateFile(file: File, dictionary: XPCDictionary) -> [StyleViolation] {
return (dictionary["key.substructure"] as? XPCArray ?? []).flatMap { subItem in
let substructure = dictionary["key.substructure"] as? XPCArray ?? []
return substructure.flatMap { subItem -> [StyleViolation] in
var violations = [StyleViolation]()
if let subDict = subItem as? XPCDictionary,
let kindString = subDict["key.kind"] as? String,
let kind = flatMap(kindString, { SwiftDeclarationKind(rawValue: $0) }) {
violations.extend(validateFile(file, dictionary: subDict))
violations.extend(validateFile(file, kind: kind, dictionary: subDict))
let kind = SwiftDeclarationKind(rawValue: kindString) {
violations.extend(self.validateFile(file, dictionary: subDict))
violations.extend(self.validateFile(file, kind: kind, dictionary: subDict))
}
return violations
}
Expand All @@ -58,16 +59,16 @@ public struct FunctionBodyLengthRule: ASTRule, ParameterizedRule {
.FunctionOperator,
.FunctionSubscript
]
if !contains(functionKinds, kind) {
if !functionKinds.contains(kind) {
return []
}
if let offset = flatMap(dictionary["key.offset"] as? Int64, { Int($0) }),
let bodyOffset = flatMap(dictionary["key.bodyoffset"] as? Int64, { Int($0) }),
let bodyLength = flatMap(dictionary["key.bodylength"] as? Int64, { Int($0) }) {
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.lineAndCharacterForByteOffset(bodyOffset)
let endLine = file.contents.lineAndCharacterForByteOffset(bodyOffset + bodyLength)
for parameter in reverse(parameters) {
for parameter in parameters.reverse() {
if let startLine = startLine?.line, let endLine = endLine?.line
where endLine - startLine > parameter.value {
return [StyleViolation(type: .Length,
Expand Down
10 changes: 5 additions & 5 deletions Source/SwiftLintFramework/Rules/LineLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ public struct LineLengthRule: ParameterizedRule {
]

public func validateFile(file: File) -> [StyleViolation] {
return compact(file.contents.lines().map { line in
for parameter in reverse(self.parameters) {
if count(line.content) > parameter.value {
return file.contents.lines().flatMap { line in
for parameter in self.parameters.reverse() {
if line.content.characters.count > parameter.value {
return StyleViolation(type: .Length,
location: Location(file: file.path, line: line.index),
severity: parameter.severity,
reason: "Line should be 100 characters or less: currently " +
"\(count(line.content)) characters")
"\(line.content.characters.count) characters")
}
}
return nil
})
}
}

public let example = RuleExample(
Expand Down
28 changes: 17 additions & 11 deletions Source/SwiftLintFramework/Rules/NestingRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public struct NestingRule: ASTRule {
}

public func validateFile(file: File, dictionary: XPCDictionary) -> [StyleViolation] {
return (dictionary["key.substructure"] as? XPCArray ?? []).flatMap { subItem in
let substructure = dictionary["key.substructure"] as? XPCArray ?? []
return substructure.flatMap { subItem -> [StyleViolation] in
var violations = [StyleViolation]()
if let subDict = subItem as? XPCDictionary,
let kindString = subDict["key.kind"] as? String,
let kind = flatMap(kindString, { SwiftDeclarationKind(rawValue: $0) }) {
violations.extend(validateFile(file, dictionary: subDict))
violations.extend(validateFile(file, kind: kind, dictionary: subDict))
let kind = SwiftDeclarationKind(rawValue: kindString) {
violations.extend(self.validateFile(file, dictionary: subDict))
violations.extend(self.validateFile(file, kind: kind, dictionary: subDict))
}
return violations
}
Expand All @@ -47,10 +48,15 @@ public struct NestingRule: ASTRule {
.Struct,
.Typealias,
.Enum,
.Enumelement
.Enumcase
]
if let offset = flatMap(dictionary["key.offset"] as? Int64, { Int($0) }) {
if level > 1 && contains(typeKinds, kind) {
if let offset = (dictionary["key.offset"] as? Int64).flatMap({ Int($0) }) {
if level > 1 && typeKinds.contains(kind) {
violations.append(StyleViolation(type: .Nesting,
location: Location(file: file, offset: offset),
reason: "Types should be nested at most 1 level deep"))
} else if level > 2 && kind == .Enumelement {
// Enum elements are implicitly wrapped in an .Enumcase
violations.append(StyleViolation(type: .Nesting,
location: Location(file: file, offset: offset),
reason: "Types should be nested at most 1 level deep"))
Expand All @@ -61,18 +67,18 @@ public struct NestingRule: ASTRule {
}
}
let substructure = dictionary["key.substructure"] as? XPCArray ?? []
violations.extend(compact(substructure.map { subItem in
violations.extend(substructure.flatMap { subItem in
let subDict = subItem as? XPCDictionary
let kindString = subDict?["key.kind"] as? String
let kind = flatMap(kindString) { kindString in
let kind = kindString.flatMap { kindString in
return SwiftDeclarationKind(rawValue: kindString)
}
if let kind = kind, subDict = subDict {
return (kind, subDict)
}
return nil
} as [(SwiftDeclarationKind, XPCDictionary)?]).flatMap { (kind, dict) in
return validateFile(file, kind: kind, dictionary: dict, level: level + 1)
}.flatMap { (kind, dict) -> [StyleViolation] in
return self.validateFile(file, kind: kind, dictionary: dict, level: level + 1)
})
return violations
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright (c) 2015 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

public struct ReturnArrowWhitespaceRule: Rule {
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/TrailingNewlineRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public struct TrailingNewlineRule: Rule {
let range = Range(start: start, end: string.endIndex)
let substring = string[range].utf16
let newLineSet = NSCharacterSet.newlineCharacterSet()
let slices = split(substring, allowEmptySlices: true) { !newLineSet.characterIsMember($0) }
let slices = substring.split(allowEmptySlices: true) { !newLineSet.characterIsMember($0) }

if let slice = slices.last where count(slice) != 1 {
if let slice = slices.last where slice.count != 1 {
return [StyleViolation(type: .TrailingNewline,
location: Location(file: file.path, line: file.contents.lines().count + 1),
severity: .Medium,
Expand Down
Loading

0 comments on commit e95625e

Please sign in to comment.