Skip to content

Commit

Permalink
[WIP] Use SwiftSyntax's new SwiftParser
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim committed Sep 27, 2022
1 parent fd7afed commit b89dc15
Show file tree
Hide file tree
Showing 52 changed files with 188 additions and 106 deletions.
6 changes: 3 additions & 3 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ swift_library(
visibility = ["//visibility:public"],
deps = [
"@com_github_jpsim_sourcekitten//:SourceKittenFramework",
"@com_github_keith_swift_syntax//:SwiftSyntax",
"@com_github_keith_swift_syntax//:SwiftSyntaxBuilder",
"@com_github_keith_swift_syntax//:SwiftSyntaxParser",
"@com_github_apple_swift_syntax//:SwiftSyntax",
"@com_github_apple_swift_syntax//:SwiftSyntaxBuilder",
"@com_github_apple_swift_syntax//:SwiftParser",
"@sourcekitten_com_github_jpsim_yams//:Yams",
] + select({
"@platforms//os:linux": ["@com_github_krzyzanowskim_cryptoswift//:CryptoSwift"],
Expand Down
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax.git",
"repositoryURL": "https://github.com/rintaro/swift-syntax.git",
"state": {
"branch": null,
"revision": "04d4497be6b88e524a71778d828790e9589ae1c4",
"version": "0.50700.0"
"revision": "322559c794d7d4ed4d3053c6fac4890941abb0b4",
"version": null
}
},
{
Expand Down
19 changes: 4 additions & 15 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,18 @@ import PackageDescription

#if os(macOS)
private let addCryptoSwift = false
private let staticSwiftSyntax = true
#else
private let addCryptoSwift = true
private let staticSwiftSyntax = false
#endif

let frameworkDependencies: [Target.Dependency] = [
.product(name: "SourceKittenFramework", package: "SourceKitten"),
.product(name: "SwiftSyntax", package: "SwiftSyntax"),
.product(name: "SwiftSyntaxBuilder", package: "SwiftSyntax"),
.product(name: "SwiftSyntaxParser", package: "SwiftSyntax"),
.product(name: "SwiftParser", package: "SwiftSyntax"),
"Yams",
]
+ (addCryptoSwift ? ["CryptoSwift"] : [])
+ (staticSwiftSyntax ? ["lib_InternalSwiftSyntaxParser"] : [])

let package = Package(
name: "SwiftLint",
Expand All @@ -28,7 +25,7 @@ let package = Package(
],
dependencies: [
.package(name: "swift-argument-parser", url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.1.3")),
.package(name: "SwiftSyntax", url: "https://github.com/apple/swift-syntax.git", .exact("0.50700.0")),
.package(name: "SwiftSyntax", url: "https://github.com/rintaro/swift-syntax.git", .revision("322559c794d7d4ed4d3053c6fac4890941abb0b4")),
.package(url: "https://github.com/jpsim/SourceKitten.git", from: "0.33.0"),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.1"),
.package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.9.0"),
Expand All @@ -46,11 +43,7 @@ let package = Package(
),
.target(
name: "SwiftLintFramework",
dependencies: frameworkDependencies,
// Pass `-dead_strip_dylibs` to ignore the dynamic version of `lib_InternalSwiftSyntaxParser`
// that ships with SwiftSyntax because we want the static version from
// `StaticInternalSwiftSyntaxParser`.
linkerSettings: staticSwiftSyntax ? [.unsafeFlags(["-Xlinker", "-dead_strip_dylibs"])] : []
dependencies: frameworkDependencies
),
.testTarget(
name: "SwiftLintFrameworkTests",
Expand All @@ -61,9 +54,5 @@ let package = Package(
"Resources",
]
),
] + (staticSwiftSyntax ? [.binaryTarget(
name: "lib_InternalSwiftSyntaxParser",
url: "https://github.com/keith/StaticInternalSwiftSyntaxParser/releases/download/5.7/lib_InternalSwiftSyntaxParser.xcframework.zip",
checksum: "99803975d10b2664fc37cc223a39b4e37fe3c79d3d6a2c44432007206d49db15"
)] : [])
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ extension Configuration {

switch rulesMode {
case .allEnabled:
return
break

case .only(let onlyRules):
if Set(onlyRules).isDisjoint(with: rule.description.allIdentifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftSyntax

extension SourceFileSyntax {
func windowsOfThreeTokens() -> [(TokenSyntax, TokenSyntax, TokenSyntax)] {
Array(tokens)
Array(tokens(viewMode: .sourceAccurate))
.windows(ofCount: 3)
.map { tokens in
let previous = tokens[tokens.startIndex]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Foundation
import SourceKittenFramework
import SwiftParser
import SwiftSyntax
#if canImport(SwiftSyntaxParser)
import SwiftSyntaxParser
#endif

private let warnSyntaxParserFailureOnceImpl: Void = {
queuedPrintError("Could not parse the syntax tree for at least one file. Results may be invalid.")
Expand Down Expand Up @@ -38,7 +36,7 @@ private var structureDictionaryCache = Cache({ file in

private var syntaxTreeCache = Cache({ file -> SourceFileSyntax? in
do {
return try SyntaxParser.parse(source: file.contents)
return try Parser.parse(source: file.contents)
} catch {
warnSyntaxParserFailureOnce()
return nil
Expand Down
2 changes: 1 addition & 1 deletion Source/SwiftLintFramework/Helpers/CommandVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class CommandVisitor: SyntaxVisitor {

init(locationConverter: SourceLocationConverter) {
self.locationConverter = locationConverter
super.init()
super.init(viewMode: .sourceAccurate)
}

override func visitPost(_ node: TokenSyntax) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public struct BlockBasedKVORule: SwiftSyntaxRule, ConfigurationProviderRule {
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ private extension DiscouragedObjectLiteralRule {

init(configuration: ObjectLiteralConfiguration) {
self.configuration = configuration
super.init(viewMode: .sourceAccurate)
}

override func visitPost(_ node: ObjectLiteralExprSyntax) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public struct DiscouragedOptionalBooleanRule: OptInRule, ConfigurationProviderRu
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public struct FallthroughRule: SwiftSyntaxRule, ConfigurationProviderRule, OptIn
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct ForceCastRule: ConfigurationProviderRule, SwiftSyntaxRule {
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
ForceCastRuleVisitor()
ForceCastRuleVisitor(viewMode: .sourceAccurate)
}
}

Expand All @@ -29,4 +29,10 @@ private final class ForceCastRuleVisitor: SyntaxVisitor, ViolationsSyntaxVisitor
violationPositions.append(node.asTok.positionAfterSkippingLeadingTrivia)
}
}

override func visitPost(_ node: UnresolvedAsExprSyntax) {
if node.questionOrExclamationMark?.tokenKind == .exclamationMark {
violationPositions.append(node.asTok.positionAfterSkippingLeadingTrivia)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct ForceTryRule: ConfigurationProviderRule, SwiftSyntaxRule {
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public struct ForceUnwrappingRule: OptInRule, SwiftSyntaxRule, ConfigurationProv
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
ForceUnwrappingVisitor()
ForceUnwrappingVisitor(viewMode: .sourceAccurate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private extension GenericTypeNameRule {

init(configuration: NameConfiguration) {
self.configuration = configuration
super.init(viewMode: .sourceAccurate)
}

override func visitPost(_ node: GenericParameterSyntax) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public struct IfLetShadowingRule: OptInRule, SwiftSyntaxCorrectableRule, Configu
public init() {}

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}

public func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
Expand Down Expand Up @@ -135,7 +135,7 @@ private class Rewriter: SyntaxRewriter, ViolationsSyntaxRewriter {
private extension OptionalBindingConditionSyntax {
var isShadowingOptionalBinding: Bool {
if let id = pattern.as(IdentifierPatternSyntax.self),
let value = initializer.value.as(IdentifierExprSyntax.self),
let value = initializer?.value.as(IdentifierExprSyntax.self),
id.identifier.text == value.identifier.text {
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public struct RedundantNilCoalescingRule: OptInRule, SwiftSyntaxCorrectableRule,
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}

public func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public struct ReturnValueFromVoidFunctionRule: ConfigurationProviderRule, OptInR
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
ReturnValueFromVoidFunctionVisitor()
ReturnValueFromVoidFunctionVisitor(viewMode: .sourceAccurate)
}
}

Expand Down
17 changes: 14 additions & 3 deletions Source/SwiftLintFramework/Rules/Idiomatic/SyntacticSugarRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public struct SyntacticSugarRule: CorrectableRule, ConfigurationProviderRule, So
)

public func validate(file: SwiftLintFile) -> [StyleViolation] {
let visitor = SyntacticSugarRuleVisitor()
let visitor = SyntacticSugarRuleVisitor(viewMode: .sourceAccurate)
return visitor.walk(file: file) { visitor in
flattenViolations(visitor.violations)
}.map { violation in
Expand All @@ -34,7 +34,7 @@ public struct SyntacticSugarRule: CorrectableRule, ConfigurationProviderRule, So
}

public func correct(file: SwiftLintFile) -> [Correction] {
let visitor = SyntacticSugarRuleVisitor()
let visitor = SyntacticSugarRuleVisitor(viewMode: .sourceAccurate)
return visitor.walk(file: file) { visitor in
var context = CorrectingContext(rule: self, file: file, contents: file.contents)
context.correctViolations(visitor.violations)
Expand Down Expand Up @@ -144,6 +144,17 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor {
}
}

override func visitPost(_ node: UnresolvedAsExprSyntax) {
// json["recommendations"] as? ↓Array<[String: Any]>
if
let parent = node.parent,
let typeName = parent.children(viewMode: .sourceAccurate).last?.as(TypeExprSyntax.self),
let violation = violation(in: typeName.type)
{
violations.append(violation)
}
}

override func visitPost(_ node: TypeInitializerClauseSyntax) {
// typealias Document = ↓Dictionary<String, AnyBSON?>
if let violation = violation(in: node.value) {
Expand Down Expand Up @@ -172,7 +183,7 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor {
// let x = ↓Array<String>.array(of: object)
// Skip checks for 'self' or \T Dictionary<Key, Value>.self
if let parent = node.parent?.as(MemberAccessExprSyntax.self),
let lastToken = Array(parent.tokens).last?.tokenKind,
let lastToken = Array(parent.tokens(viewMode: .sourceAccurate)).last?.tokenKind,
[.selfKeyword, .identifier("Type"), .identifier("none"), .identifier("Index")].contains(lastToken) {
return
}
Expand Down
26 changes: 12 additions & 14 deletions Source/SwiftLintFramework/Rules/Idiomatic/ToggleBoolRule.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import SwiftSyntax
import SwiftSyntaxBuilder

public struct ToggleBoolRule: SwiftSyntaxCorrectableRule, ConfigurationProviderRule, OptInRule {
public var configuration = SeverityConfiguration(.warning)
Expand Down Expand Up @@ -32,7 +31,7 @@ public struct ToggleBoolRule: SwiftSyntaxCorrectableRule, ConfigurationProviderR
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
Visitor()
Visitor(viewMode: .sourceAccurate)
}

public func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
Expand Down Expand Up @@ -81,19 +80,18 @@ private extension ToggleBoolRule {

correctionPositions.append(node.positionAfterSkippingLeadingTrivia)

let functionCall = FunctionCallExprSyntax { functionCall in
functionCall.useCalledExpression(
ExprSyntax(
MemberAccessExprSyntax { memberAccess in
memberAccess.useBase(node.first!.withoutTrivia())
memberAccess.useDot(.period)
memberAccess.useName(.identifier("toggle"))
}
let functionCall = FunctionCallExprSyntax(
calledExpression: ExprSyntax(
MemberAccessExprSyntax(
base: node.first!.withoutTrivia(),
dot: .periodToken(),
name: .identifier("toggle"),
declNameArguments: nil
)
)
functionCall.useLeftParen(.leftParen)
functionCall.useRightParen(.rightParen)
}
),
leftParen: .leftParenToken(), argumentList: .init([]), rightParen: .rightParenToken(),
trailingClosure: nil, additionalTrailingClosures: nil
)

let newNode = node
.replacing(childAt: 0, with: ExprSyntax(functionCall))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public struct UnavailableConditionRule: ConfigurationProviderRule, SourceKitFree
)

public func validate(file: SwiftLintFile) -> [StyleViolation] {
let visitor = UnavailableConditionRuleVisitor()
let visitor = UnavailableConditionRuleVisitor(viewMode: .sourceAccurate)
return visitor.walk(file: file, handler: \.availabilityChecks).map { check in
StyleViolation(
ruleDescription: Self.description,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct UnneededBreakInSwitchRule: SwiftSyntaxRule, ConfigurationProviderR
)

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
UnneededBreakInSwitchRuleVisitor()
UnneededBreakInSwitchRuleVisitor(viewMode: .sourceAccurate)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public struct UntypedErrorInCatchRule: OptInRule, ConfigurationProviderRule, Swi
])

public func makeVisitor(file: SwiftLintFile) -> ViolationsSyntaxVisitor? {
UntypedErrorInCatchRuleVisitor()
UntypedErrorInCatchRuleVisitor(viewMode: .sourceAccurate)
}

public func makeRewriter(file: SwiftLintFile) -> ViolationsSyntaxRewriter? {
Expand Down Expand Up @@ -167,7 +167,7 @@ private final class UntypedErrorInCatchRuleRewriter: SyntaxRewriter, ViolationsS
return super.visit(
node
.withCatchKeyword(node.catchKeyword.withTrailingTrivia(.spaces(1)))
.withCatchItems(SyntaxFactory.makeBlankCatchItemList())
.withCatchItems(CatchItemListSyntax([]))
)
}
}
Loading

0 comments on commit b89dc15

Please sign in to comment.