Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add Isnot Empty extension in the Swift Framework (realm#3387)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulTaykalo authored Nov 7, 2020
1 parent 580314d commit 693ee2c
Show file tree
Hide file tree
Showing 67 changed files with 111 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ struct RuleDocumentation {
var fileContents: String {
let description = ruleType.description
var content = [h1(description.name), description.description, detailsSummary(ruleType.init())]
if !description.nonTriggeringExamples.isEmpty {
if description.nonTriggeringExamples.isNotEmpty {
content += [h2("Non Triggering Examples")]
content += description.nonTriggeringExamples.map(formattedCode)
}
if !description.triggeringExamples.isEmpty {
if description.triggeringExamples.isNotEmpty {
content += [h2("Triggering Examples")]
content += description.triggeringExamples.map(formattedCode)
}
Expand Down
6 changes: 6 additions & 0 deletions Source/SwiftLintFramework/Extensions/Array+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,9 @@ extension Array {
}
}
}

extension Collection {
var isNotEmpty: Bool {
return !isEmpty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ extension Configuration {
let rulesMode: RulesMode
if enableAllRules {
rulesMode = .allEnabled
} else if !whitelistRules.isEmpty {
if !disabledRules.isEmpty || !optInRules.isEmpty {
} else if whitelistRules.isNotEmpty {
if disabledRules.isNotEmpty || optInRules.isNotEmpty {
queuedPrintError("'\(Key.disabledRules.rawValue)' or " +
"'\(Key.optInRules.rawValue)' cannot be used in combination " +
"with '\(Key.whitelistRules.rawValue)'")
Expand Down Expand Up @@ -200,7 +200,7 @@ extension Configuration {
private static func warnAboutInvalidKeys(configurationDictionary dict: [String: Any], ruleList: RuleList) {
// Log an error when supplying invalid keys in the configuration dictionary
let invalidKeys = Set(dict.keys).subtracting(self.validKeys(ruleList: ruleList))
if !invalidKeys.isEmpty {
if invalidKeys.isNotEmpty {
queuedPrintError("Configuration contains invalid keys:\n\(invalidKeys)")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ extension SwiftLintFile {
}.map { ($0.0, pattern, template) }
}).sorted { $0.0.location > $1.0.location } // reversed

guard !matches.isEmpty else { return [] }
guard matches.isNotEmpty else { return [] }

let description = type(of: legacyRule).description
var corrections = [Correction]()
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public struct Command: Equatable {
}
let ruleTexts = rawRuleTexts.components(separatedBy: .whitespacesAndNewlines).filter {
let component = $0.trimmingCharacters(in: .whitespaces)
return !component.isEmpty && component != "*/"
return component.isNotEmpty && component != "*/"
}

ruleIdentifiers = Set(ruleTexts.map(RuleIdentifier.init(_:)))
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ public struct Configuration: Hashable {
private func validateRuleIdentifiers(ruleIdentifiers: [String], validRuleIdentifiers: [String]) -> [String] {
// Validate that all rule identifiers map to a defined rule
let invalidRuleIdentifiers = ruleIdentifiers.filter { !validRuleIdentifiers.contains($0) }
if !invalidRuleIdentifiers.isEmpty {
if invalidRuleIdentifiers.isNotEmpty {
for invalidRuleIdentifier in invalidRuleIdentifiers {
queuedPrintError("configuration error: '\(invalidRuleIdentifier)' is not a valid rule identifier")
}
Expand Down
6 changes: 3 additions & 3 deletions Source/SwiftLintFramework/Models/Linter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ private extension Rule {
static func superfluousDisableCommandViolations(regions: [Region],
superfluousDisableCommandRule: SuperfluousDisableCommandRule?,
allViolations: [StyleViolation]) -> [StyleViolation] {
guard !regions.isEmpty, let superfluousDisableCommandRule = superfluousDisableCommandRule else {
guard regions.isNotEmpty, let superfluousDisableCommandRule = superfluousDisableCommandRule else {
return []
}

Expand Down Expand Up @@ -279,7 +279,7 @@ public struct CollectedLinter {
for rule in rules.compactMap({ $0 as? CorrectableRule }) {
let newCorrections = rule.correct(file: file, using: storage, compilerArguments: compilerArguments)
corrections += newCorrections
if !newCorrections.isEmpty {
if newCorrections.isNotEmpty {
file.invalidateCache()
}
}
Expand All @@ -303,7 +303,7 @@ public struct CollectedLinter {
configuration: Configuration,
superfluousDisableCommandRule: SuperfluousDisableCommandRule?
) -> [StyleViolation] {
guard !regions.isEmpty, let superfluousDisableCommandRule = superfluousDisableCommandRule else {
guard regions.isNotEmpty, let superfluousDisableCommandRule = superfluousDisableCommandRule else {
return []
}
let allCustomIdentifiers = configuration.customRuleIdentifiers.map { RuleIdentifier($0) }
Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Models/LinterCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public final class LinterCache {
defer {
writeCacheLock.unlock()
}
guard !writeCache.isEmpty else {
guard writeCache.isNotEmpty else {
return
}

Expand All @@ -105,7 +105,7 @@ public final class LinterCache {
readCacheLock.unlock()

let encoder = Encoder()
for (description, writeFileCache) in writeCache where !writeFileCache.entries.isEmpty {
for (description, writeFileCache) in writeCache where writeFileCache.entries.isNotEmpty {
let fileCacheEntries = readCache[description]?.entries.merging(writeFileCache.entries) { _, write in write }
let fileCache = fileCacheEntries.map(FileCache.init) ?? writeFileCache
let data = try encoder.encode(fileCache)
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Protocols/Rule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public protocol SubstitutionCorrectableRule: CorrectableRule {
public extension SubstitutionCorrectableRule {
func correct(file: SwiftLintFile) -> [Correction] {
let violatingRanges = file.ruleEnabled(violatingRanges: violationRanges(in: file), for: self)
guard !violatingRanges.isEmpty else { return [] }
guard violatingRanges.isNotEmpty else { return [] }

let description = Self.description
var corrections = [Correction]()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct ConvenienceTypeRule: ASTRule, OptInRule, ConfigurationProviderRule
guard let offset = dictionary.offset,
[.class, .struct].contains(kind),
dictionary.inheritedTypes.isEmpty,
!dictionary.substructure.isEmpty else {
dictionary.substructure.isNotEmpty else {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public struct ExplicitACLRule: OptInRule, ConfigurationProviderRule, AutomaticTe
public func validate(file: SwiftLintFile) -> [StyleViolation] {
let implicitAndExplicitInternalElements = internalTypeElements(in: file.structureDictionary)

guard !implicitAndExplicitInternalElements.isEmpty else {
guard implicitAndExplicitInternalElements.isNotEmpty else {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct ExplicitTopLevelACLRule: OptInRule, ConfigurationProviderRule, Aut
return nil
}

guard !internalTypesOffsets.isEmpty else {
guard internalTypesOffsets.isNotEmpty else {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public struct FileNameRule: ConfigurationProviderRule, OptInRule {
$0.replacingOccurrences(of: ".", with: configuration.nestedTypeSeparator)
}

guard !allDeclaredTypeNames.isEmpty, !allDeclaredTypeNames.contains(typeInFileName) else {
guard allDeclaredTypeNames.isNotEmpty, !allDeclaredTypeNames.contains(typeInFileName) else {
return []
}

Expand Down
4 changes: 2 additions & 2 deletions Source/SwiftLintFramework/Rules/Idiomatic/ForWhereRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ public struct ForWhereRule: ASTRule, ConfigurationProviderRule, AutomaticTestabl
return false
}

let containsKeyword = !file.match(pattern: "\\blet|var|case\\b", with: [.keyword], range: range).isEmpty
let containsKeyword = file.match(pattern: "\\blet|var|case\\b", with: [.keyword], range: range).isNotEmpty
if containsKeyword {
return true
}

return !file.match(pattern: "\\|\\||&&", with: [], range: range).isEmpty
return file.match(pattern: "\\|\\||&&", with: [], range: range).isNotEmpty
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ public struct FunctionDefaultParameterAtEndRule: ASTRule, ConfigurationProviderR
return paramOffset < bodyOffset
}

guard !params.isEmpty else {
guard params.isNotEmpty else {
return []
}

let containsDefaultValue = { self.isDefaultParameter(file: file, dictionary: $0) }
let defaultParams = params.filter(containsDefaultValue)
guard !defaultParams.isEmpty else {
guard defaultParams.isNotEmpty else {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ public struct NimbleOperatorRule: ConfigurationProviderRule, OptInRule, Correcta

public func correct(file: SwiftLintFile) -> [Correction] {
let matches = violationMatchesRanges(in: file)
.filter { !file.ruleEnabled(violatingRanges: [$0], for: self).isEmpty }
guard !matches.isEmpty else { return [] }
.filter { file.ruleEnabled(violatingRanges: [$0], for: self).isNotEmpty }
guard matches.isNotEmpty else { return [] }

let description = Self.description
var corrections: [Correction] = []
Expand Down Expand Up @@ -186,7 +186,7 @@ private extension String {

for case let (pattern, operatorString?) in [toPattern, toNotPattern] {
let expression = regex(pattern)
guard !expression.matches(in: self, options: [], range: range).isEmpty else {
guard expression.matches(in: self, options: [], range: range).isNotEmpty else {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public struct NoGroupingExtensionRule: OptInRule, ConfigurationProviderRule, Aut
return false
}

return !file.match(pattern: "\\bwhere\\b", with: [.keyword], range: range).isEmpty
return file.match(pattern: "\\bwhere\\b", with: [.keyword], range: range).isNotEmpty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public struct PatternMatchingKeywordsRule: ASTRule, ConfigurationProviderRule, O
let letMatches = file.match(pattern: "\\blet\\b", with: [.keyword], range: caseRange)
let varMatches = file.match(pattern: "\\bvar\\b", with: [.keyword], range: caseRange)

if !letMatches.isEmpty && !varMatches.isEmpty {
if letMatches.isNotEmpty && varMatches.isNotEmpty {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ extension SourceKittenDictionary {
guard let byteRange = byteRange,
case let contents = file.stringView,
let range = contents.byteRangeToNSRange(byteRange),
!file.match(pattern: "\\Avar\\b", with: [.keyword], range: range).isEmpty else {
file.match(pattern: "\\Avar\\b", with: [.keyword], range: range).isNotEmpty else {
return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public struct RedundantStringEnumValueRule: ASTRule, ConfigurationProviderRule,

private func enumElementsCount(dictionary: SourceKittenDictionary) -> Int {
return children(of: dictionary, matching: .enumelement).filter({ element in
return !filterEnumInits(dictionary: element).isEmpty
return filterEnumInits(dictionary: element).isNotEmpty
}).count
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public struct ToggleBoolRule: SubstitutionCorrectableRule, ConfigurationProvider

public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? {
let violationString = file.stringView.substring(with: violationRange)
let identifier = violationString.components(separatedBy: .whitespaces).first { !$0.isEmpty }
let identifier = violationString.components(separatedBy: .whitespaces).first { $0.isNotEmpty }
return (violationRange, identifier! + ".toggle()")
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/Lint/ArrayInitRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public struct ArrayInitRule: ASTRule, ConfigurationProviderRule, OptInRule, Auto
.trimmingCharacters(in: CharacterSet(charactersIn: "{}"))
.trimmingCharacters(in: .whitespacesAndNewlines)

if !processedSubstring.isEmpty {
if processedSubstring.isNotEmpty {
stop.pointee = true
containsContent = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public struct ClassDelegateProtocolRule: ASTRule, ConfigurationProviderRule, Aut
}

private func isClassProtocol(file: SwiftLintFile, range: NSRange) -> Bool {
return !file.match(pattern: "\\bclass\\b", with: [.keyword], range: range).isEmpty
return file.match(pattern: "\\bclass\\b", with: [.keyword], range: range).isNotEmpty
}

private func isDelegateProtocol(_ name: String) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public struct DeploymentTargetRule: ConfigurationProviderRule {
let attributes = dictionary.swiftAttributes.filter {
$0.attribute.flatMap(SwiftDeclarationAttributeKind.init) == .available
}
guard !attributes.isEmpty else {
guard attributes.isNotEmpty else {
return []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ private extension StringView {
_ endToken: SwiftLintSyntaxToken) -> Bool {
guard let betweenTokens = subStringBetweenTokens(startToken, endToken) else { return false }
let range = betweenTokens.fullNSRange
return !regex(#"^[\s\(,]*$"#).matches(in: betweenTokens, options: [], range: range).isEmpty
return regex(#"^[\s\(,]*$"#).matches(in: betweenTokens, options: [], range: range).isNotEmpty
}

func isRegexBetweenTokens(_ startToken: SwiftLintSyntaxToken, _ regexString: String,
_ endToken: SwiftLintSyntaxToken) -> Bool {
guard let betweenTokens = subStringBetweenTokens(startToken, endToken) else { return false }

let range = betweenTokens.fullNSRange
return !regex("^\\s*\(regexString)\\s*$").matches(in: betweenTokens, options: [], range: range).isEmpty
return regex("^\\s*\(regexString)\\s*$").matches(in: betweenTokens, options: [], range: range).isNotEmpty
}
}
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/Lint/MarkRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public struct MarkRule: CorrectableRule, ConfigurationProviderRule {
guard let syntaxKind = syntaxTokens.first?.kind else {
return false
}
return !syntaxTokens.isEmpty && SyntaxKind.commentKinds.contains(syntaxKind)
return syntaxTokens.isNotEmpty && SyntaxKind.commentKinds.contains(syntaxKind)
}.compactMap { range, syntaxTokens in
let byteRange = ByteRange(location: syntaxTokens[0].offset, length: 0)
let identifierRange = file.stringView.byteRangeToNSRange(byteRange)
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Rules/Lint/MissingDocsRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ private extension SwiftLintFile {
func missingDocOffsets(in dictionary: SourceKittenDictionary,
acls: [AccessControlLevel]) -> [(ByteCount, AccessControlLevel)] {
if dictionary.enclosedSwiftAttributes.contains(.override) ||
!dictionary.inheritedTypes.isEmpty {
dictionary.inheritedTypes.isNotEmpty {
return []
}
let substructureOffsets = dictionary.substructure.flatMap {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public struct OrphanedDocCommentRule: ConfigurationProviderRule {
return false
}

return !contents.trimmingCharacters(in: Self.characterSet).isEmpty
return contents.trimmingCharacters(in: Self.characterSet).isNotEmpty
}.map { token in
return StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public struct PrivateUnitTestRule: ASTRule, ConfigurationProviderRule, CacheDesc
return false
}
let range = superclass.fullNSRange
return !regex.matches(in: superclass, options: [], range: range).isEmpty
return regex.matches(in: superclass, options: [], range: range).isNotEmpty
}

private func validateFunction(file: SwiftLintFile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct ProhibitedSuperRule: ConfigurationProviderRule, ASTRule, OptInRule
kind == .functionMethodInstance,
configuration.resolvedMethodNames.contains(name),
dictionary.enclosedSwiftAttributes.contains(.override),
!dictionary.extractCallsToSuper(methodName: name).isEmpty
dictionary.extractCallsToSuper(methodName: name).isNotEmpty
else { return [] }

return [StyleViolation(ruleDescription: Self.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public struct UnusedDeclarationRule: AutomaticTestableRule, ConfigurationProvide
)

public func collectInfo(for file: SwiftLintFile, compilerArguments: [String]) -> UnusedDeclarationRule.FileUSRs {
guard !compilerArguments.isEmpty else {
guard compilerArguments.isNotEmpty else {
queuedPrintError("""
Attempted to lint file at path '\(file.path ?? "...")' with the \
\(Self.description.identifier) rule without any compiler arguments.
""")
return .empty
}

guard let index = file.index(compilerArguments: compilerArguments), !index.value.isEmpty else {
guard let index = file.index(compilerArguments: compilerArguments), index.value.isNotEmpty else {
queuedPrintError("""
Could not index file at path '\(file.path ?? "...")' with the \
\(Self.description.identifier) rule.
Expand Down
8 changes: 4 additions & 4 deletions Source/SwiftLintFramework/Rules/Lint/UnusedImportRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public struct UnusedImportRule: CorrectableRule, ConfigurationProviderRule, Anal
corrections.append(Correction(ruleDescription: description, location: location))
}

if !corrections.isEmpty {
if corrections.isNotEmpty {
file.write(contents.bridge())
}

Expand All @@ -57,7 +57,7 @@ public struct UnusedImportRule: CorrectableRule, ConfigurationProviderRule, Anal
}
}

guard !missingImports.isEmpty else {
guard missingImports.isNotEmpty else {
return corrections
}

Expand All @@ -84,7 +84,7 @@ public struct UnusedImportRule: CorrectableRule, ConfigurationProviderRule, Anal
}

private func importUsage(in file: SwiftLintFile, compilerArguments: [String]) -> [ImportUsage] {
guard !compilerArguments.isEmpty else {
guard compilerArguments.isNotEmpty else {
queuedPrintError("""
Attempted to lint file at path '\(file.path ?? "...")' with the \
\(Self.description.identifier) rule without any compiler arguments.
Expand All @@ -108,7 +108,7 @@ private extension SwiftLintFile {
unusedImports.remove("Foundation")
}

if !unusedImports.isEmpty {
if unusedImports.isNotEmpty {
unusedImports.subtract(
operatorImports(
arguments: compilerArguments,
Expand Down
Loading

0 comments on commit 693ee2c

Please sign in to comment.