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

Changing the default line length limit to 120 #1010

Merged
merged 2 commits into from
Dec 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
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ file_header:
\/\/ Created by .*? on \d{1,2}\/\d{1,2}\/\d{2}\.
\/\/ Copyright © \d{4} Realm\. All rights reserved\.
\/\/
line_length: 120
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've added this here to prevent the swiftlint build phase to generate warnings (since it's using the installed version, which probably is the latest stable one).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could log a message if a configuration has line length explicitly set to 120, so that more people will be aware that this is SwiftLint's new default?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't feel it'd be very useful as some people may see to not relying on the defaults as a good practice?

Copy link
Collaborator

Choose a reason for hiding this comment

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

yeah, maybe. I'm a bit torn, so happy to go with your preference on this one.

4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

##### Breaking

* None.
* `file_length` rule now has a default value of `120` for warnings.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Though not really a breaking change, I do approve of having it in this section to spread the word...

[Marcelo Fabri](https://github.com/marcelofabri)
[#1008](https://github.com/realm/SwiftLint/issues/1008)

##### Enhancements

Expand Down
3 changes: 1 addition & 2 deletions Source/SwiftLintFramework/Extensions/File+Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ extension File {
}
}

private func substructureForDict(_ dict: [String: SourceKitRepresentable]) ->
[[String: SourceKitRepresentable]]? {
private func substructureForDict(_ dict: [String: SourceKitRepresentable]) -> [[String: SourceKitRepresentable]]? {
return (dict["key.substructure"] as? [SourceKitRepresentable])?.flatMap {
$0 as? [String: SourceKitRepresentable]
}
Expand Down
3 changes: 1 addition & 2 deletions Source/SwiftLintFramework/Extensions/String+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ extension String {
return self == lowercased()
}

internal func nameStrippingLeadingUnderscoreIfPrivate(
_ dict: [String: SourceKitRepresentable]) -> String {
internal func nameStrippingLeadingUnderscoreIfPrivate(_ dict: [String: SourceKitRepresentable]) -> String {
if let aclString = dict["key.accessibility"] as? String,
let acl = AccessControlLevel(identifier: aclString),
acl.isPrivate && characters.first == "_" {
Expand Down
3 changes: 1 addition & 2 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ public struct Linter {
return getStyleViolations(true)
}

fileprivate func getStyleViolations(_ benchmark: Bool = false) ->
([StyleViolation], [(id: String, time: Double)]) {
private func getStyleViolations(_ benchmark: Bool = false) -> ([StyleViolation], [(id: String, time: Double)]) {
if file.sourcekitdFailed {
queuedPrintError("Most of rules are skipped because sourcekitd fails.")
}
Expand Down
3 changes: 1 addition & 2 deletions Source/SwiftLintFramework/Protocols/ASTRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ extension ASTRule where KindType.RawValue == String {
return validateFile(file, dictionary: file.structure.dictionary)
}

public func validateFile(_ file: File, dictionary: [String: SourceKitRepresentable]) ->
[StyleViolation] {
public func validateFile(_ file: File, dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? []
return substructure.flatMap { subItem -> [StyleViolation] in
guard let subDict = subItem as? [String: SourceKitRepresentable],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public struct CyclomaticComplexityRule: ASTRule, ConfigurationProviderRule {
}

private func measureComplexity(_ file: File,
dictionary: [String: SourceKitRepresentable]) -> Int {
dictionary: [String: SourceKitRepresentable]) -> Int {
var hasSwitchStatements = false

let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? []

let complexity = substructure.reduce(0) { complexity, subItem in
guard let subDict = subItem as? [String: SourceKitRepresentable],
let kind = subDict["key.kind"] as? String else {
let kind = subDict["key.kind"] as? String else {
return complexity
}

Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/DynamicInlineRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public struct DynamicInlineRule: ASTRule, ConfigurationProviderRule {
)

public func validateFile(_ file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
// Look for functions with both "inline" and "dynamic". For each of these, we can get offset
// of the "func" keyword. We can assume that the nearest "@inline" before this offset is
// the attribute we are interested in.
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/ImplicitGetterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public struct ImplicitGetterRule: Rule, ConfigurationProviderRule {
}
}

private func variableDeclarationsFor(_ byteOffset: Int, structure: Structure) ->
[[String: SourceKitRepresentable]] {
private func variableDeclarationsFor(_ byteOffset: Int,
structure: Structure) -> [[String: SourceKitRepresentable]] {
var results = [[String: SourceKitRepresentable]]()

func parse(dictionary: [String: SourceKitRepresentable]) {
Expand Down
14 changes: 7 additions & 7 deletions Source/SwiftLintFramework/Rules/LineLengthRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import SourceKittenFramework

public struct LineLengthRule: ConfigurationProviderRule, SourceKitFreeRule {
public var configuration = SeverityLevelsConfiguration(warning: 100, error: 200)
public var configuration = SeverityLevelsConfiguration(warning: 120, error: 200)

public init() {}

Expand All @@ -18,14 +18,14 @@ public struct LineLengthRule: ConfigurationProviderRule, SourceKitFreeRule {
name: "Line Length",
description: "Lines should not span too many characters.",
nonTriggeringExamples: [
String(repeating: "/", count: 100) + "\n",
String(repeating: "#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)", count: 100) + "\n",
String(repeating: "#imageLiteral(resourceName: \"image.jpg\")", count: 100) + "\n"
String(repeating: "/", count: 120) + "\n",
String(repeating: "#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)", count: 120) + "\n",
String(repeating: "#imageLiteral(resourceName: \"image.jpg\")", count: 120) + "\n"
],
triggeringExamples: [
String(repeating: "/", count: 101) + "\n",
String(repeating: "#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)", count: 101) + "\n",
String(repeating: "#imageLiteral(resourceName: \"image.jpg\")", count: 101) + "\n"
String(repeating: "/", count: 121) + "\n",
String(repeating: "#colorLiteral(red: 0.9607843161, green: 0.7058823705, blue: 0.200000003, alpha: 1)", count: 121) + "\n",
String(repeating: "#imageLiteral(resourceName: \"image.jpg\")", count: 121) + "\n"
]
)

Expand Down
5 changes: 2 additions & 3 deletions Source/SwiftLintFramework/Rules/MissingDocsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ private func declarationOverrides(_ dictionary: [String: SourceKitRepresentable]
return dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override")
}

private func inheritedMembersForDictionary(_ dictionary: [String: SourceKitRepresentable]) ->
[String] {
private func inheritedMembersForDictionary(_ dictionary: [String: SourceKitRepresentable]) -> [String] {
return mappedDictValues(dictionary, key: "key.inheritedtypes", subKey: "key.name").flatMap {
File.allDeclarationsByType[$0] ?? []
}
}

extension File {
fileprivate func missingDocOffsets(_ dictionary: [String: SourceKitRepresentable],
acl: [AccessControlLevel], skipping: [String] = []) -> [Int] {
acl: [AccessControlLevel], skipping: [String] = []) -> [Int] {
if declarationOverrides(dictionary) {
return []
}
Expand Down
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Rules/OpeningBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public struct OpeningBraceRule: CorrectableRule, ConfigurationProviderRule {
return writeToFile(file, violatingRanges: violatingRanges)
}

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

Expand All @@ -106,8 +106,8 @@ public struct OpeningBraceRule: CorrectableRule, ConfigurationProviderRule {
}
}

fileprivate func correctContents(_ contents: String, violatingRange: NSRange)
-> (correctedContents: String, adjustedRange: NSRange?) {
private func correctContents(_ contents: String,
violatingRange: NSRange) -> (correctedContents: String, adjustedRange: NSRange?) {
guard let indexRange = contents.nsrangeToIndexRange(violatingRange) else {
return (contents, nil)
}
Expand Down
12 changes: 5 additions & 7 deletions Source/SwiftLintFramework/Rules/PrivateUnitTestRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,27 +127,25 @@ public struct PrivateUnitTestRule: ASTRule, ConfigurationProviderRule {
}
}

fileprivate func isTestClass(_ dictionary: [String: SourceKitRepresentable]) -> Bool {
private func isTestClass(_ dictionary: [String: SourceKitRepresentable]) -> Bool {
guard let regex = configuration.regex, let superclass = superclass(dictionary) else {
return false
}
let range = NSRange(location: 0, length: superclass.bridge().length)
return !regex.matches(in: superclass, options: [], range: range).isEmpty
}

fileprivate func validateFunction(_ file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable])
-> [StyleViolation] {
private func validateFunction(_ file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
assert(kind == .functionMethodInstance)
guard let name = dictionary["key.name"] as? String, name.hasPrefix("test") else {
return []
}
return validateAccessControlLevel(file, dictionary: dictionary)
}

fileprivate func validateAccessControlLevel(_ file: File,
dictionary: [String: SourceKitRepresentable])
-> [StyleViolation] {
private func validateAccessControlLevel(_ file: File,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard let acl = AccessControlLevel(dictionary), acl.isPrivate else { return [] }
let offset = Int(dictionary["key.offset"] as? Int64 ?? 0)
return [StyleViolation(ruleDescription: type(of: self).description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ public struct RedundantStringEnumValueRule: ASTRule, ConfigurationProviderRule {
}
}

private func filterEnumInits(dictionary: [String: SourceKitRepresentable]) ->
[[String: SourceKitRepresentable]] {
private func filterEnumInits(dictionary: [String: SourceKitRepresentable]) -> [[String: SourceKitRepresentable]] {
guard let elements = dictionary["key.elements"] as? [SourceKitRepresentable] else {
return []
}
Expand Down
7 changes: 3 additions & 4 deletions Source/SwiftLintFramework/Rules/StatementPositionRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ private extension StatementPositionRule {
// swiftlint:disable:next force_try
static let uncuddledRegex = try! NSRegularExpression(pattern: uncuddledPattern, options: [])

static func uncuddledMatchValidator(_ contents: String) ->
((NSTextCheckingResult) -> NSTextCheckingResult?) {
static func uncuddledMatchValidator(_ contents: String) -> ((NSTextCheckingResult) -> NSTextCheckingResult?) {
return { match in
if match.numberOfRanges != 5 {
return match
Expand All @@ -176,8 +175,8 @@ private extension StatementPositionRule {
}
}

static func uncuddledMatchFilter(contents: String, syntaxMap: SyntaxMap) ->
((NSTextCheckingResult) -> Bool) {
static func uncuddledMatchFilter(contents: String,
syntaxMap: SyntaxMap) -> ((NSTextCheckingResult) -> Bool) {
return { match in
let range = match.range
guard let matchRange = contents.bridge().NSRangeToByteRange(start: range.location,
Expand Down
10 changes: 5 additions & 5 deletions Source/SwiftLintFramework/Rules/UnusedClosureParameterRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule, Co
public func validateFile(_ file: File,
kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
return violationRangesInFile(file, kind: kind, dictionary: dictionary).map { range, name in
return violationRangesInFile(file, dictionary: dictionary, kind: kind).map { range, name in
let reason = "Unused parameter \"\(name)\" in a closure should be replaced with _."
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
Expand All @@ -65,9 +65,9 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule, Co
}
}

private func violationRangesInFile(_ file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable])
-> [(range: NSRange, name: String)] {
private func violationRangesInFile(_ file: File,
dictionary: [String: SourceKitRepresentable],
kind: SwiftExpressionKind) -> [(range: NSRange, name: String)] {
guard kind == .call else {
return []
}
Expand Down Expand Up @@ -132,7 +132,7 @@ public struct UnusedClosureParameterRule: ASTRule, ConfigurationProviderRule, Co
return []
}
return violationRangesInFile(file, dictionary: subDict) +
violationRangesInFile(file, kind: kind, dictionary: subDict).map({ $0.0 })
violationRangesInFile(file, dictionary: subDict, kind: kind).map({ $0.0 })
}
}

Expand Down