-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
variable_name rule should extend to all identifiers #954
Merged
Merged
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e1eae5e
Function kinds refactor
marcelofabri 11364ed
Renaming enums
marcelofabri 44c55f4
VariableNameRule is now IdentifierNameRule
marcelofabri 61c783f
add missing trailing spaces to changelog entry
jpsim dd384f5
Fixing changelog
marcelofabri a150e26
Updating test
marcelofabri 43a91ef
Add workaround for CharacterSet crash on Linux
marcelofabri 8715b91
PR feedback
marcelofabri 8ef0703
Applying fix on Linux
marcelofabri 5c42f59
Validate enum element only on Swift 3
marcelofabri 967c435
Add deprecated alias to identifier_name rule
marcelofabri ec016a4
Swift 3 naming conventions
marcelofabri 7d94f5c
Updating rule identifier
marcelofabri 0f63224
Fix changelog entry position
marcelofabri 014531e
Refactor to use new Dictionary methods
marcelofabri c8e0818
Merge branch 'master' into identifier-rule
marcelofabri 8c4bfcc
Merge branch 'master' into identifier-rule
jpsim 337ec41
move changelog entry to appropriate section
jpsim fa3ab00
fix merge
jpsim a2ec02c
replace uses of 'variable_name' with 'identifier_name' in markdown files
jpsim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
139 changes: 139 additions & 0 deletions
139
Source/SwiftLintFramework/Rules/IdentifierNameRule.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,139 @@ | ||
// | ||
// IdentifierNameRule.swift | ||
// SwiftLint | ||
// | ||
// Created by JP Simard on 5/16/15. | ||
// Copyright © 2015 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct IdentifierNameRule: ASTRule, ConfigurationProviderRule { | ||
|
||
public var configuration = NameConfiguration(minLengthWarning: 3, | ||
minLengthError: 2, | ||
maxLengthWarning: 40, | ||
maxLengthError: 60) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "identifier_name", | ||
name: "Identifier Name", | ||
description: "Identifier names should only contain alphanumeric characters and " + | ||
"start with a lowercase character or should only contain capital letters. " + | ||
"In an exception to the above, variable names may start with a capital letter " + | ||
"when they are declared static and immutable. Variable names should not be too " + | ||
"long or too short.", | ||
nonTriggeringExamples: IdentifierNameRuleExamples.swift3NonTriggeringExamples, | ||
triggeringExamples: IdentifierNameRuleExamples.swift3TriggeringExamples, | ||
deprecatedAliases: ["variable_name"] | ||
) | ||
|
||
public func validate(file: File, kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard !dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override") else { | ||
return [] | ||
} | ||
|
||
return validateName(dictionary: dictionary, kind: kind).map { name, offset in | ||
guard !configuration.excluded.contains(name) else { | ||
return [] | ||
} | ||
|
||
let isFunction = SwiftDeclarationKind.functionKinds().contains(kind) | ||
let description = type(of: self).description | ||
|
||
let type = self.type(for: kind) | ||
if !isFunction { | ||
if !CharacterSet.alphanumerics.isSuperset(ofCharactersIn: name) { | ||
return [ | ||
StyleViolation(ruleDescription: description, | ||
severity: .error, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: "\(type) name should only contain alphanumeric " + | ||
"characters: '\(name)'") | ||
] | ||
} | ||
|
||
if let severity = severity(forLength: name.characters.count) { | ||
let reason = "\(type) name should be between " + | ||
"\(configuration.minLengthThreshold) and " + | ||
"\(configuration.maxLengthThreshold) characters long: '\(name)'" | ||
return [ | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: severity, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: reason) | ||
] | ||
} | ||
} | ||
|
||
if kind != .varStatic && name.isViolatingCase && !name.isOperator { | ||
let reason = "\(type) name should start with a lowercase character: '\(name)'" | ||
return [ | ||
StyleViolation(ruleDescription: description, | ||
severity: .error, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: reason) | ||
] | ||
} | ||
|
||
return [] | ||
} ?? [] | ||
} | ||
|
||
private func validateName(dictionary: [String: SourceKitRepresentable], | ||
kind: SwiftDeclarationKind) -> (name: String, offset: Int)? { | ||
guard let name = dictionary.name, | ||
let offset = dictionary.offset, | ||
kinds(for: .current).contains(kind), | ||
!name.hasPrefix("$") else { | ||
return nil | ||
} | ||
|
||
return (name.nameStrippingLeadingUnderscoreIfPrivate(dictionary), offset) | ||
} | ||
|
||
private func kinds(for version: SwiftVersion) -> [SwiftDeclarationKind] { | ||
let common = SwiftDeclarationKind.variableKinds() + SwiftDeclarationKind.functionKinds() | ||
switch version { | ||
case .two: | ||
return common | ||
case .three: | ||
return common + [.enumelement] | ||
} | ||
} | ||
|
||
private func type(for kind: SwiftDeclarationKind) -> String { | ||
if SwiftDeclarationKind.functionKinds().contains(kind) { | ||
return "Function" | ||
} else if kind == .enumelement { | ||
return "Enum element" | ||
} else { | ||
return "Variable" | ||
} | ||
} | ||
} | ||
|
||
fileprivate extension String { | ||
var isViolatingCase: Bool { | ||
let secondIndex = characters.index(after: startIndex) | ||
let firstCharacter = substring(to: secondIndex) | ||
guard firstCharacter.isUppercase() else { | ||
return false | ||
} | ||
guard characters.count > 1 else { | ||
return true | ||
} | ||
let range = secondIndex..<characters.index(after: secondIndex) | ||
let secondCharacter = substring(with: range) | ||
return secondCharacter.isLowercase() | ||
} | ||
|
||
var isOperator: Bool { | ||
let operators = ["/", "=", "-", "+", "!", "*", "|", "^", "~", "?", ".", "%", "<", ">", "&"] | ||
return !operators.filter(hasPrefix).isEmpty | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
Source/SwiftLintFramework/Rules/IdentifierNameRuleExamples.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,51 @@ | ||
// | ||
// IdentifierNameRuleExamples.swift | ||
// SwiftLint | ||
// | ||
// Created by Marcelo Fabri on 12/30/16. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
internal struct IdentifierNameRuleExamples { | ||
private static let commonNonTriggeringExamples = [ | ||
"let myLet = 0", | ||
"var myVar = 0", | ||
"private let _myLet = 0", | ||
"class Abc { static let MyLet = 0 }", | ||
"let URL: NSURL? = nil", | ||
"let XMLString: String? = nil", | ||
"override var i = 0", | ||
"enum Foo { case myEnum }", | ||
"func isOperator(name: String) -> Bool", | ||
"func typeForKind(_ kind: SwiftDeclarationKind) -> String", | ||
"func == (lhs: SyntaxToken, rhs: SyntaxToken) -> Bool", | ||
"override func IsOperator(name: String) -> Bool" | ||
] | ||
|
||
static let swift2NonTriggeringExamples = commonNonTriggeringExamples + [ | ||
"enum Foo { case MyEnum }" | ||
] | ||
|
||
static let swift3NonTriggeringExamples = commonNonTriggeringExamples | ||
|
||
private static let commonTriggeringExamples = [ | ||
"↓let MyLet = 0", | ||
"↓let _myLet = 0", | ||
"private ↓let myLet_ = 0", | ||
"↓let myExtremelyVeryVeryVeryVeryVeryVeryLongLet = 0", | ||
"↓var myExtremelyVeryVeryVeryVeryVeryVeryLongVar = 0", | ||
"private ↓let _myExtremelyVeryVeryVeryVeryVeryVeryLongLet = 0", | ||
"↓let i = 0", | ||
"↓var id = 0", | ||
"private ↓let _i = 0", | ||
"↓func IsOperator(name: String) -> Bool" | ||
] | ||
|
||
static let swift2TriggeringExamples = commonTriggeringExamples | ||
|
||
static let swift3TriggeringExamples = commonTriggeringExamples + [ | ||
"enum Foo { case ↓MyEnum }" | ||
] | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now in the wrong section