Files
SwiftLint/Source/SwiftLintBuiltInRules/Rules/Performance/EmptyCollectionLiteralRule.swift
Danny Mösch 40bd97038a Support arbitrary configurations in @SwiftSyntaxRule (#5275)
Almost all rules based on SwiftSyntax can be set up now by just adding
`@SwiftSyntaxRule` to the rule struct.
2023-10-16 19:34:43 +02:00

49 lines
1.8 KiB
Swift

import SwiftSyntax
@SwiftSyntaxRule
struct EmptyCollectionLiteralRule: OptInRule {
var configuration = SeverityConfiguration<Self>(.warning)
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: [
Example("myArray = []"),
Example("myArray.isEmpty"),
Example("!myArray.isEmpty"),
Example("myDict = [:]")
],
triggeringExamples: [
Example("myArray↓ == []"),
Example("myArray↓ != []"),
Example("myArray↓ == [ ]"),
Example("myDict↓ == [:]"),
Example("myDict↓ != [:]"),
Example("myDict↓ == [: ]"),
Example("myDict↓ == [ :]"),
Example("myDict↓ == [ : ]")
]
)
}
private extension EmptyCollectionLiteralRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: TokenSyntax) {
guard
node.tokenKind.isEqualityComparison,
let violationPosition = node.previousToken(viewMode: .sourceAccurate)?.endPositionBeforeTrailingTrivia,
let expectedLeftSquareBracketToken = node.nextToken(viewMode: .sourceAccurate),
expectedLeftSquareBracketToken.tokenKind == .leftSquare,
let expectedColonToken = expectedLeftSquareBracketToken.nextToken(viewMode: .sourceAccurate),
expectedColonToken.tokenKind == .colon || expectedColonToken.tokenKind == .rightSquare
else {
return
}
violations.append(violationPosition)
}
}
}