forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add joined_default_parameter opt-in rule
to discourage explicit usage of the default separator. Implements realm#1093.
- Loading branch information
1 parent
54d6199
commit da823f5
Showing
7 changed files
with
111 additions
and
0 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
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,58 @@ | ||
// | ||
// JoinedDefaultRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 8/3/17. | ||
// Copyright © 2017 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct JoinedDefaultParameterRule: ASTRule, ConfigurationProviderRule, OptInRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "joined_default_parameter", | ||
name: "Joined Default Parameter", | ||
description: "Discouraged explicit usage of the default separator.", | ||
kind: .idiomatic, | ||
nonTriggeringExamples: [ | ||
"let foo = bar.joined()", | ||
"let foo = bar.joined(separator: \",\")", | ||
"let foo = bar.joined(separator: toto)" | ||
], | ||
triggeringExamples: [ | ||
"let foo = bar.joined(separator: \"\")" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftExpressionKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard | ||
kind == .call, | ||
let offset = dictionary.offset, | ||
let name = dictionary.name, | ||
name.hasSuffix(".joined"), | ||
passesDefaultSeparator(dictionary: dictionary, file: file) | ||
else { | ||
return [] | ||
} | ||
|
||
return [StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: offset))] | ||
} | ||
|
||
private func passesDefaultSeparator(dictionary: [String: SourceKitRepresentable], file: File) -> Bool { | ||
guard | ||
let bodyOffset = dictionary.bodyOffset, | ||
let bodyLength = dictionary.bodyLength else { return false } | ||
|
||
let body = file.contents.bridge().substringWithByteRange(start: bodyOffset, length: bodyLength) | ||
return body == "separator: \"\"" | ||
} | ||
} |
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