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.
- Loading branch information
1 parent
0d18758
commit 130371b
Showing
7 changed files
with
133 additions
and
1 deletion.
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
40 changes: 40 additions & 0 deletions
40
Source/SwiftLintFramework/Rules/Performance/EmptyCollectionLiteralRule.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,40 @@ | ||
import SourceKittenFramework | ||
|
||
public struct EmptyCollectionLiteralRule: ConfigurationProviderRule, OptInRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "empty_collection_literal", | ||
name: "Empty Collection Literal", | ||
description: "Prefer checking `isEmpty` over comparing collection to an empty array or dictionary literal.", | ||
kind: .performance, | ||
nonTriggeringExamples: [ | ||
"myArray = []", | ||
"myArray.isEmpty", | ||
"!myArray.isEmpy", | ||
"myDict = [:]" | ||
], | ||
triggeringExamples: [ | ||
"myArray↓ == []", | ||
"myArray↓ != []", | ||
"myArray↓ == [ ]", | ||
"myDict↓ == [:]", | ||
"myDict↓ != [:]", | ||
"myDict↓ == [: ]", | ||
"myDict↓ == [ :]", | ||
"myDict↓ == [ : ]" | ||
] | ||
) | ||
|
||
public func validate(file: File) -> [StyleViolation] { | ||
let pattern = "\\b\\s*(==|!=)\\s*\\[\\s*:?\\s*\\]" | ||
let excludingKinds = SyntaxKind.commentAndStringKinds | ||
return file.match(pattern: pattern, excludingSyntaxKinds: excludingKinds).map { | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: $0.location)) | ||
} | ||
} | ||
} |
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