diff --git a/Source/SwiftLintFramework/Extensions/SourceFileSyntax+SwiftSyntax.swift b/Source/SwiftLintFramework/Extensions/SourceFileSyntax+SwiftSyntax.swift deleted file mode 100644 index d4c61279f..000000000 --- a/Source/SwiftLintFramework/Extensions/SourceFileSyntax+SwiftSyntax.swift +++ /dev/null @@ -1,10 +0,0 @@ -import SwiftOperators -import SwiftSyntax - -extension SourceFileSyntax { - func folded() -> SourceFileSyntax? { - OperatorTable.standardOperators - .foldAll(self) { _ in } - .as(SourceFileSyntax.self) - } -} diff --git a/Source/SwiftLintFramework/Extensions/SwiftLintFile+Cache.swift b/Source/SwiftLintFramework/Extensions/SwiftLintFile+Cache.swift index e409d2c96..1d6642f91 100644 --- a/Source/SwiftLintFramework/Extensions/SwiftLintFile+Cache.swift +++ b/Source/SwiftLintFramework/Extensions/SwiftLintFile+Cache.swift @@ -4,6 +4,7 @@ import Darwin import Foundation import IDEUtils import SourceKittenFramework +import SwiftOperators import SwiftParser import SwiftParserDiagnostics import SwiftSyntax @@ -25,6 +26,11 @@ private let structureDictionaryCache = Cache { file in private let syntaxTreeCache = Cache { file -> SourceFileSyntax in return Parser.parse(source: file.contents) } +private let foldedSyntaxTreeCache = Cache { file -> SourceFileSyntax? in + return OperatorTable.standardOperators + .foldAll(file.syntaxTree) { _ in } + .as(SourceFileSyntax.self) +} private let locationConverterCache = Cache { file -> SourceLocationConverter in return SourceLocationConverter(file: file.path ?? "", tree: file.syntaxTree) } @@ -155,6 +161,8 @@ extension SwiftLintFile { internal var syntaxTree: SourceFileSyntax { syntaxTreeCache.get(self) } + internal var foldedSyntaxTree: SourceFileSyntax? { foldedSyntaxTreeCache.get(self) } + internal var locationConverter: SourceLocationConverter { locationConverterCache.get(self) } internal var commands: [Command] { commandsCache.get(self) } @@ -192,6 +200,7 @@ extension SwiftLintFile { syntaxTokensByLinesCache.invalidate(self) syntaxKindsByLinesCache.invalidate(self) syntaxTreeCache.invalidate(self) + foldedSyntaxTreeCache.invalidate(self) locationConverterCache.invalidate(self) commandsCache.invalidate(self) linesWithTokensCache.invalidate(self) @@ -207,6 +216,7 @@ extension SwiftLintFile { syntaxTokensByLinesCache.clear() syntaxKindsByLinesCache.clear() syntaxTreeCache.clear() + foldedSyntaxTreeCache.clear() locationConverterCache.clear() commandsCache.clear() linesWithTokensCache.clear() diff --git a/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift b/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift index 6f18ea79f..bc2c0cc04 100644 --- a/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift +++ b/Source/SwiftLintFramework/Protocols/SwiftSyntaxRule.swift @@ -24,7 +24,7 @@ public protocol SwiftSyntaxRule: SourceKitFreeRule { /// - parameter syntaxTree: The syntax tree to run pre-processing on /// /// - returns: The tree that will be used to check for violations. If `nil`, this rule will return no violations. - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? } public extension SwiftSyntaxRule where Self: ConfigurationProviderRule, @@ -53,7 +53,7 @@ public extension SwiftSyntaxRule { } func validate(file: SwiftLintFile) -> [StyleViolation] { - guard let syntaxTree = preprocess(syntaxTree: file.syntaxTree) else { + guard let syntaxTree = preprocess(file: file) else { return [] } @@ -79,8 +79,8 @@ public extension SwiftSyntaxRule { ) } - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.syntaxTree } } diff --git a/Source/SwiftLintFramework/Rules/Idiomatic/LegacyMultipleRule.swift b/Source/SwiftLintFramework/Rules/Idiomatic/LegacyMultipleRule.swift index 430c20b19..7e25b0fd7 100644 --- a/Source/SwiftLintFramework/Rules/Idiomatic/LegacyMultipleRule.swift +++ b/Source/SwiftLintFramework/Rules/Idiomatic/LegacyMultipleRule.swift @@ -39,8 +39,8 @@ struct LegacyMultipleRule: OptInRule, ConfigurationProviderRule, SwiftSyntaxRule ] ) - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree.folded() + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.foldedSyntaxTree } func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { diff --git a/Source/SwiftLintFramework/Rules/Lint/IdenticalOperandsRule.swift b/Source/SwiftLintFramework/Rules/Lint/IdenticalOperandsRule.swift index 160b9c09b..d86afafdc 100644 --- a/Source/SwiftLintFramework/Rules/Lint/IdenticalOperandsRule.swift +++ b/Source/SwiftLintFramework/Rules/Lint/IdenticalOperandsRule.swift @@ -72,8 +72,8 @@ struct IdenticalOperandsRule: ConfigurationProviderRule, SwiftSyntaxRule, OptInR ] ) - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree.folded() + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.foldedSyntaxTree } func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { diff --git a/Source/SwiftLintFramework/Rules/Performance/ContainsOverFirstNotNilRule.swift b/Source/SwiftLintFramework/Rules/Performance/ContainsOverFirstNotNilRule.swift index be09b6beb..4ba04416b 100644 --- a/Source/SwiftLintFramework/Rules/Performance/ContainsOverFirstNotNilRule.swift +++ b/Source/SwiftLintFramework/Rules/Performance/ContainsOverFirstNotNilRule.swift @@ -34,8 +34,8 @@ struct ContainsOverFirstNotNilRule: SwiftSyntaxRule, OptInRule, ConfigurationPro Visitor(viewMode: .sourceAccurate) } - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree.folded() + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.foldedSyntaxTree } } diff --git a/Source/SwiftLintFramework/Rules/Performance/ContainsOverRangeNilComparisonRule.swift b/Source/SwiftLintFramework/Rules/Performance/ContainsOverRangeNilComparisonRule.swift index e2d1159fe..2cd637fb1 100644 --- a/Source/SwiftLintFramework/Rules/Performance/ContainsOverRangeNilComparisonRule.swift +++ b/Source/SwiftLintFramework/Rules/Performance/ContainsOverRangeNilComparisonRule.swift @@ -23,8 +23,8 @@ struct ContainsOverRangeNilComparisonRule: SwiftSyntaxRule, OptInRule, Configura } ) - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree.folded() + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.foldedSyntaxTree } func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { diff --git a/Source/SwiftLintFramework/Rules/Performance/EmptyCountRule.swift b/Source/SwiftLintFramework/Rules/Performance/EmptyCountRule.swift index 223d7b6b2..31da0eedd 100644 --- a/Source/SwiftLintFramework/Rules/Performance/EmptyCountRule.swift +++ b/Source/SwiftLintFramework/Rules/Performance/EmptyCountRule.swift @@ -35,8 +35,8 @@ struct EmptyCountRule: ConfigurationProviderRule, OptInRule, SwiftSyntaxRule { ] ) - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree.folded() + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.foldedSyntaxTree } func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor { diff --git a/Source/SwiftLintFramework/Rules/Style/ShorthandOperatorRule.swift b/Source/SwiftLintFramework/Rules/Style/ShorthandOperatorRule.swift index 53474e86e..8df47d4d2 100644 --- a/Source/SwiftLintFramework/Rules/Style/ShorthandOperatorRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/ShorthandOperatorRule.swift @@ -47,8 +47,8 @@ struct ShorthandOperatorRule: ConfigurationProviderRule, SwiftSyntaxRule { fileprivate static let allOperators = ["-", "/", "+", "*"] - func preprocess(syntaxTree: SourceFileSyntax) -> SourceFileSyntax? { - syntaxTree.folded() + func preprocess(file: SwiftLintFile) -> SourceFileSyntax? { + file.foldedSyntaxTree } func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor {