From ffa4e8a3fb5d23692e03e7a0641b45033dc3a2dc Mon Sep 17 00:00:00 2001 From: Marcelo Fabri Date: Sun, 1 Oct 2017 00:45:52 -0300 Subject: [PATCH] Workaround `union` not working on Linux on Swift 3.1 --- .../Extensions/CharacterSet+LinuxHack.swift | 11 +++++++---- .../Rules/GenericTypeNameRule.swift | 4 ++-- .../SwiftLintFramework/Rules/IdentifierNameRule.swift | 4 ++-- Source/SwiftLintFramework/Rules/TypeNameRule.swift | 4 ++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Source/SwiftLintFramework/Extensions/CharacterSet+LinuxHack.swift b/Source/SwiftLintFramework/Extensions/CharacterSet+LinuxHack.swift index 4fc123abd..582529496 100644 --- a/Source/SwiftLintFramework/Extensions/CharacterSet+LinuxHack.swift +++ b/Source/SwiftLintFramework/Extensions/CharacterSet+LinuxHack.swift @@ -9,13 +9,16 @@ import Foundation extension CharacterSet { - func isSuperset(ofCharactersIn string: String) -> Bool { -#if swift(>=4.0) - return isSuperset(of: CharacterSet(charactersIn: string)) + func isSuperset(ofCharactersIn string: String, + union other: CharacterSet = CharacterSet()) -> Bool { +#if swift(>=3.2) + let set = union(other) + return set.isSuperset(of: CharacterSet(charactersIn: string)) #else // workaround for https://bugs.swift.org/browse/SR-3485 return !Set(string.characters).contains { character in - !contains(String(character).unicodeScalars.first!) + let scalar = String(character).unicodeScalars.first! + return !contains(scalar) && !other.contains(scalar) } #endif } diff --git a/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift b/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift index 18eca52f6..9fe1003ee 100644 --- a/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift +++ b/Source/SwiftLintFramework/Rules/GenericTypeNameRule.swift @@ -165,8 +165,8 @@ public struct GenericTypeNameRule: ASTRule, ConfigurationProviderRule { return [] } - let allowedSymbols = configuration.allowedSymbols.union(.alphanumerics) - if !allowedSymbols.isSuperset(ofCharactersIn: name) { + let allowedSymbols = configuration.allowedSymbols + if !allowedSymbols.isSuperset(ofCharactersIn: name, union: .alphanumerics) { return [ StyleViolation(ruleDescription: type(of: self).description, severity: .error, diff --git a/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift b/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift index ccb8e3b74..162d54748 100644 --- a/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift +++ b/Source/SwiftLintFramework/Rules/IdentifierNameRule.swift @@ -48,8 +48,8 @@ public struct IdentifierNameRule: ASTRule, ConfigurationProviderRule { let type = self.type(for: kind) if !isFunction { - let allowedSymbols = configuration.allowedSymbols.union(.alphanumerics) - if !allowedSymbols.isSuperset(ofCharactersIn: name) { + let allowedSymbols = configuration.allowedSymbols + if !allowedSymbols.isSuperset(ofCharactersIn: name, union: .alphanumerics) { return [ StyleViolation(ruleDescription: description, severity: .error, diff --git a/Source/SwiftLintFramework/Rules/TypeNameRule.swift b/Source/SwiftLintFramework/Rules/TypeNameRule.swift index fee32a2e3..80ea8bdd9 100644 --- a/Source/SwiftLintFramework/Rules/TypeNameRule.swift +++ b/Source/SwiftLintFramework/Rules/TypeNameRule.swift @@ -75,8 +75,8 @@ public struct TypeNameRule: ASTRule, ConfigurationProviderRule { } let name = name.nameStrippingLeadingUnderscoreIfPrivate(dictionary) - let allowedSymbols = configuration.allowedSymbols.union(.alphanumerics) - if !allowedSymbols.isSuperset(ofCharactersIn: name) { + let allowedSymbols = configuration.allowedSymbols + if !allowedSymbols.isSuperset(ofCharactersIn: name, union: .alphanumerics) { return [StyleViolation(ruleDescription: type(of: self).description, severity: .error, location: Location(file: file, byteOffset: offset),