-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1035 from marcelofabri/vertical-parameter-alignme…
…nt-rule Vertical parameter alignment rule
- Loading branch information
Showing
6 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Source/SwiftLintFramework/Rules/VerticalParameterAlignmentRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// | ||
// VerticalParameterAlignmentRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Marcelo Fabri on 12/22/16. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct VerticalParameterAlignmentRule: ASTRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "vertical_parameter_alignment", | ||
name: "Vertical Parameter Alignment", | ||
description: "Function parameters should be aligned vertically if they're in multiple lines in a declaration.", | ||
nonTriggeringExamples: [ | ||
"func validateFunction(_ file: File, kind: SwiftDeclarationKind,\n" + | ||
" dictionary: [String: SourceKitRepresentable]) { }\n", | ||
"func validateFunction(_ file: File, kind: SwiftDeclarationKind,\n" + | ||
" dictionary: [String: SourceKitRepresentable]) -> [StyleViolation]\n", | ||
"func foo(bar: Int)\n", | ||
"func foo(bar: Int) -> String \n", | ||
"func validateFunction(_ file: File, kind: SwiftDeclarationKind,\n" + | ||
" dictionary: [String: SourceKitRepresentable])\n" + | ||
" -> [StyleViolation]\n", | ||
"func validateFunction(\n" + | ||
" _ file: File, kind: SwiftDeclarationKind,\n" + | ||
" dictionary: [String: SourceKitRepresentable]) -> [StyleViolation]\n" | ||
], | ||
triggeringExamples: [ | ||
"func validateFunction(_ file: File, kind: SwiftDeclarationKind,\n" + | ||
" ↓dictionary: [String: SourceKitRepresentable]) { }\n", | ||
"func validateFunction(_ file: File, kind: SwiftDeclarationKind,\n" + | ||
" ↓dictionary: [String: SourceKitRepresentable]) { }\n", | ||
"func validateFunction(_ file: File,\n" + | ||
" ↓kind: SwiftDeclarationKind,\n" + | ||
" ↓dictionary: [String: SourceKitRepresentable]) { }\n" | ||
] | ||
) | ||
|
||
public func validateFile(_ file: File, | ||
kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard SwiftDeclarationKind.functionKinds().contains(kind) else { | ||
return [] | ||
} | ||
|
||
let contents = file.contents.bridge() | ||
let pattern = "\\(\\s*(\\S)" | ||
|
||
guard let nameOffset = (dictionary["key.nameoffset"] as? Int64).flatMap({ Int($0) }), | ||
let nameLength = (dictionary["key.namelength"] as? Int64).flatMap({ Int($0) }), | ||
let nameRange = contents.byteRangeToNSRange(start: nameOffset, length: nameLength), | ||
let paramStart = regex(pattern).firstMatch(in: file.contents, | ||
options: [], range: nameRange)?.rangeAt(1).location, | ||
let (startLine, startCharacter) = contents.lineAndCharacter(forCharacterOffset: paramStart), | ||
let (endLine, _) = contents.lineAndCharacter(forByteOffset: nameOffset + nameLength - 1), | ||
endLine > startLine else { | ||
return [] | ||
} | ||
|
||
let linesRange = (startLine + 1)...endLine | ||
let violationLocations = linesRange.flatMap { lineIndex -> Int? in | ||
let line = file.lines[lineIndex - 1] | ||
guard let paramLocation = regex("\\S").firstMatch(in: file.contents, options: [], | ||
range: line.range)?.range.location, | ||
let (_, paramCharacter) = contents.lineAndCharacter(forCharacterOffset: paramLocation), | ||
paramCharacter != startCharacter else { | ||
return nil | ||
} | ||
|
||
return paramLocation | ||
} | ||
|
||
return violationLocations.map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: $0)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters