Files
SwiftLint/Source/SwiftLintFramework/Rules/Style/NoSpaceInMethodCallRule.swift
T
Zev Eisenberg fcf848608e Add Inline test failure messages (#3040)
* Add Example wrapper in order to display test failures inline when running in Xcode.
* Stop using Swift 5.1-only features so we can compile on Xcode 10.2.
* Wrap strings in Example.
* Add Changelog entry.
* Wrap all examples in Example struct.
* Better and more complete capturing of line numbers.
* Fix broken test.
* Better test traceability.
* Address or disable linting warnings.
* Add documentation comments.
* Disable linter for a few cases.
* Limit mutability and add copy-and-mutate utility functions.
* Limit scope of mutability.
2020-02-02 10:35:37 +02:00

94 lines
3.6 KiB
Swift

import Foundation
import SourceKittenFramework
public struct NoSpaceInMethodCallRule: SubstitutionCorrectableASTRule, ConfigurationProviderRule,
AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "no_space_in_method_call",
name: "No Space in Method Call",
description: "Don't add a space between the method name and the parentheses.",
kind: .style,
minSwiftVersion: .fourDotTwo,
nonTriggeringExamples: [
Example("foo()"),
Example("object.foo()"),
Example("object.foo(1)"),
Example("object.foo(value: 1)"),
Example("object.foo { print($0 }"),
Example("list.sorted { $0.0 < $1.0 }.map { $0.value }"),
Example("self.init(rgb: (Int) (colorInt))")
],
triggeringExamples: [
Example("foo↓ ()"),
Example("object.foo↓ ()"),
Example("object.foo↓ (1)"),
Example("object.foo↓ (value: 1)"),
Example("object.foo↓ () {}"),
Example("object.foo↓ ()")
],
corrections: [
Example("foo↓ ()"): Example("foo()"),
Example("object.foo↓ ()"): Example("object.foo()"),
Example("object.foo↓ (1)"): Example("object.foo(1)"),
Example("object.foo↓ (value: 1)"): Example("object.foo(value: 1)"),
Example("object.foo↓ () {}"): Example("object.foo() {}"),
Example("object.foo↓ ()"): Example("object.foo()")
]
)
// MARK: - ASTRule
public func validate(file: SwiftLintFile,
kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
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))
}
}
// MARK: - SubstitutionCorrectableASTRule
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? {
return (violationRange, "")
}
public func violationRanges(in file: SwiftLintFile,
kind: SwiftExpressionKind,
dictionary: SourceKittenDictionary) -> [NSRange] {
guard kind == .call,
let bodyOffset = dictionary.bodyOffset,
let nameOffset = dictionary.nameOffset,
let nameLength = dictionary.nameLength,
nameLength > 0,
case let nameEndPosition = nameOffset + nameLength,
bodyOffset != nameEndPosition + 1,
case let contents = file.stringView,
case let byteRange = ByteRange(location: nameEndPosition, length: bodyOffset - nameEndPosition - 1),
let range = contents.byteRangeToNSRange(byteRange)
else {
return []
}
// Don't trigger if it's a single parameter trailing closure without parens
if let subDict = dictionary.substructure.last,
subDict.expressionKind == .closure,
let closureBodyOffset = subDict.bodyOffset,
closureBodyOffset == bodyOffset {
return []
}
// Don't trigger if it's a typecast
if let name = dictionary.name, name.hasPrefix("("), name.hasSuffix(")") {
return []
}
return [range]
}
}