From 2578cb3862ac0ed2a45ea69adb33f91cc9c9b5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Danny=20M=C3=B6sch?= Date: Thu, 3 Apr 2025 21:27:57 +0200 Subject: [PATCH] Use SwiftSyntax version 601.0.0 (#6003) --- BUILD | 12 +++++++++++- MODULE.bazel | 2 +- Package.resolved | 4 ++-- Package.swift | 2 +- .../Rules/Idiomatic/DuplicateImportsRule.swift | 12 +++++++----- .../Rules/Idiomatic/SyntacticSugarRule.swift | 14 ++++++++------ .../Rules/Lint/LocalDocCommentRule.swift | 6 +++--- .../Rules/Style/ClosureParameterPositionRule.swift | 2 +- .../Extensions/SwiftLintFile+BodyLineCount.swift | 12 ++++++------ .../Extensions/SwiftSyntax+SwiftLint.swift | 9 ++++++--- .../RuleConfigurationMacros.swift | 1 + Source/YamsWrapper/Empty.swift | 0 12 files changed, 47 insertions(+), 29 deletions(-) create mode 100644 Source/YamsWrapper/Empty.swift diff --git a/BUILD b/BUILD index ebeb9a6f3..e862723d1 100644 --- a/BUILD +++ b/BUILD @@ -96,7 +96,7 @@ swift_library( "@SwiftSyntax//:SwiftSyntaxBuilder_opt", "@SwiftSyntax//:SwiftSyntax_opt", "@com_github_jpsim_sourcekitten//:SourceKittenFramework", - "@sourcekitten_com_github_jpsim_yams//:Yams", + ":Yams.wrapper", "@swiftlint_com_github_scottrhoyt_swifty_text_table//:SwiftyTextTable", ] + select({ "@platforms//os:linux": ["@com_github_krzyzanowskim_cryptoswift//:CryptoSwift"], @@ -104,6 +104,16 @@ swift_library( }), ) +swift_library( + name = "Yams.wrapper", + srcs = ["Source/YamsWrapper/Empty.swift"], + module_name = "YamsWrapper", + visibility = ["//visibility:private"], + deps = [ + "@sourcekitten_com_github_jpsim_yams//:Yams", + ], +) + swift_library( name = "SwiftLintBuiltInRules", package_name = "SwiftLint", diff --git a/MODULE.bazel b/MODULE.bazel index f1ff78835..ebea53e70 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -14,7 +14,7 @@ bazel_dep(name = "rules_shell", version = "0.4.0", repo_name = "build_bazel_rule bazel_dep(name = "rules_swift", version = "2.1.1", repo_name = "build_bazel_rules_swift") bazel_dep(name = "sourcekitten", version = "0.37.0", repo_name = "com_github_jpsim_sourcekitten") bazel_dep(name = "swift_argument_parser", version = "1.3.1.1", repo_name = "sourcekitten_com_github_apple_swift_argument_parser") -bazel_dep(name = "swift-syntax", version = "600.0.0", repo_name = "SwiftSyntax") +bazel_dep(name = "swift-syntax", version = "601.0.0", repo_name = "SwiftSyntax") bazel_dep(name = "yams", version = "5.3.0", repo_name = "sourcekitten_com_github_jpsim_yams") swiftlint_repos = use_extension("//bazel:repos.bzl", "swiftlint_repos_bzlmod") diff --git a/Package.resolved b/Package.resolved index f486d0cf3..82bf79fda 100644 --- a/Package.resolved +++ b/Package.resolved @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/swiftlang/swift-syntax.git", "state" : { - "revision" : "cb53fa1bd3219b0b23ded7dfdd3b2baff266fd25", - "version" : "600.0.0" + "revision" : "1103c45ece4f7fe160b8f75b4ea1ee2e5fac1841", + "version" : "601.0.0" } }, { diff --git a/Package.swift b/Package.swift index 294f98a2a..c39c3b84b 100644 --- a/Package.swift +++ b/Package.swift @@ -33,7 +33,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMajor(from: "1.2.1")), - .package(url: "https://github.com/swiftlang/swift-syntax.git", exact: "600.0.0"), + .package(url: "https://github.com/swiftlang/swift-syntax.git", exact: "601.0.0"), .package(url: "https://github.com/jpsim/SourceKitten.git", .upToNextMajor(from: "0.37.0")), .package(url: "https://github.com/jpsim/Yams.git", .upToNextMajor(from: "5.3.0")), .package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", .upToNextMajor(from: "0.9.0")), diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DuplicateImportsRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DuplicateImportsRule.swift index b9b1bf33c..945fa2e95 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DuplicateImportsRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/DuplicateImportsRule.swift @@ -66,11 +66,13 @@ private final class ImportPathVisitor: SyntaxVisitor { } } +private typealias ByteSourceRange = Range + private final class IfConfigClauseVisitor: SyntaxVisitor { var ifConfigRanges = [ByteSourceRange]() override func visitPost(_ node: IfConfigClauseSyntax) { - ifConfigRanges.append(node.totalByteRange) + ifConfigRanges.append(node.range) } } @@ -79,8 +81,8 @@ private struct ImportPathUsage: Hashable { let value: ByteSourceRange func hash(into hasher: inout Hasher) { - hasher.combine(value.offset) - hasher.combine(value.length) + hasher.combine(value.lowerBound.utf8Offset) + hasher.combine(value.length.utf8Length) } } @@ -103,8 +105,8 @@ private extension SwiftLintFile { .walk(file: self, handler: \.ifConfigRanges) func ranges(for position: AbsolutePosition) -> [ByteSourceRange] { - let positionRange = ByteSourceRange(offset: position.utf8Offset, length: 0) - return ifConfigRanges.filter { $0.intersectsOrTouches(positionRange) } + let positionRange = position..<(position + SourceLength(utf8Length: 1)) + return ifConfigRanges.filter { $0.overlapsOrTouches(positionRange) } } var violationPositions = Set() diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift index 2ae8d4149..4c4404afb 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/SyntacticSugarRule.swift @@ -151,10 +151,10 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor { override func visitPost(_ node: SameTypeRequirementSyntax) { // @_specialize(where S == ↓Array) - if let violation = violation(in: node.leftType) { + if let violation = violation(in: node.leftType.as(TypeSyntax.self)) { violations.append(violation) } - if let violation = violation(in: node.rightType) { + if let violation = violation(in: node.rightType.as(TypeSyntax.self)) { violations.append(violation) } } @@ -180,7 +180,7 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor { // If there's no type, check all inner generics like in the case of 'Box>' node.genericArgumentClause.arguments .lazy - .compactMap { self.violation(in: $0.argument) } + .compactMap { self.violation(in: $0.argument.as(TypeSyntax.self)) } .first .map { violations.append($0) } } @@ -203,7 +203,7 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor { // If there's no type, check all inner generics like in the case of 'Box>' guard let genericArguments = simpleType.genericArgumentClause else { return nil } - let innerTypes = genericArguments.arguments.compactMap { violation(in: $0.argument) } + let innerTypes = genericArguments.arguments.compactMap { violation(in: $0.argument.as(TypeSyntax.self)) } return innerTypes.first } @@ -238,8 +238,10 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor { correctionType = .dictionary(commaStart: lastArgumentEnd, commaEnd: comma.endPosition) } - let firstInnerViolation = violation(in: firstGenericType.argument) - let secondInnerViolation = generic.arguments.count > 1 ? violation(in: lastGenericType.argument) : nil + let firstInnerViolation = violation(in: firstGenericType.argument.as(TypeSyntax.self)) + let secondInnerViolation = generic.arguments.count > 1 + ? violation(in: lastGenericType.argument.as(TypeSyntax.self)) + : nil return SyntacticSugarRuleViolation( position: node.positionAfterSkippingLeadingTrivia, diff --git a/Source/SwiftLintBuiltInRules/Rules/Lint/LocalDocCommentRule.swift b/Source/SwiftLintBuiltInRules/Rules/Lint/LocalDocCommentRule.swift index bf9dc3b8c..94a309023 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Lint/LocalDocCommentRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Lint/LocalDocCommentRule.swift @@ -51,7 +51,7 @@ struct LocalDocCommentRule: SwiftSyntaxRule, OptInRule { private extension LocalDocCommentRule { final class Visitor: ViolationsSyntaxVisitor { - private let docCommentRanges: [ByteSourceRange] + private let docCommentRanges: [Range] init(configuration: ConfigurationType, file: SwiftLintFile, @@ -67,9 +67,9 @@ private extension LocalDocCommentRule { return } - let violatingRange = docCommentRanges.first { $0.intersects(body.totalByteRange) } + let violatingRange = docCommentRanges.first { $0.overlaps(body.range) } if let violatingRange { - violations.append(AbsolutePosition(utf8Offset: violatingRange.offset)) + violations.append(AbsolutePosition(utf8Offset: violatingRange.lowerBound.utf8Offset)) } } } diff --git a/Source/SwiftLintBuiltInRules/Rules/Style/ClosureParameterPositionRule.swift b/Source/SwiftLintBuiltInRules/Rules/Style/ClosureParameterPositionRule.swift index 4acb0078b..4092b327e 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Style/ClosureParameterPositionRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Style/ClosureParameterPositionRule.swift @@ -132,7 +132,7 @@ private extension ClosureSignatureSyntax { var positionsToCheck: [AbsolutePosition] { var positions: [AbsolutePosition] = [] if let captureItems = capture?.items { - positions.append(contentsOf: captureItems.map(\.expression.positionAfterSkippingLeadingTrivia)) + positions.append(contentsOf: captureItems.map(\.name.positionAfterSkippingLeadingTrivia)) } if let input = parameterClause?.as(ClosureShorthandParameterListSyntax.self) { diff --git a/Source/SwiftLintCore/Extensions/SwiftLintFile+BodyLineCount.swift b/Source/SwiftLintCore/Extensions/SwiftLintFile+BodyLineCount.swift index 416b69689..ca76509ac 100644 --- a/Source/SwiftLintCore/Extensions/SwiftLintFile+BodyLineCount.swift +++ b/Source/SwiftLintCore/Extensions/SwiftLintFile+BodyLineCount.swift @@ -44,12 +44,12 @@ public extension SwiftLintFile { .tokens(viewMode: .sourceAccurate) .reduce(into: []) { linesWithTokens, token in if case .stringSegment = token.tokenKind { - let sourceRange = token - .trimmed - .sourceRange(converter: locationConverter) - let startLine = sourceRange.start.line - let endLine = sourceRange.end.line - linesWithTokens.formUnion(startLine...endLine) + let sourceRange = token.sourceRange( + converter: locationConverter, + afterLeadingTrivia: true, + afterTrailingTrivia: true + ) + linesWithTokens.formUnion(sourceRange.start.line...sourceRange.end.line) } else { let line = locationConverter.location(for: token.positionAfterSkippingLeadingTrivia).line linesWithTokens.insert(line) diff --git a/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift b/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift index 31abe66b1..af40515e4 100644 --- a/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift +++ b/Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift @@ -41,9 +41,12 @@ public extension AbsolutePosition { } } -public extension ByteSourceRange { +public extension Range { func toSourceKittenByteRange() -> ByteRange { - ByteRange(location: ByteCount(offset), length: ByteCount(length)) + ByteRange( + location: ByteCount(lowerBound), + length: ByteCount(upperBound.utf8Offset) - ByteCount(lowerBound.utf8Offset) + ) } } @@ -345,7 +348,7 @@ public extension DeclReferenceExprSyntax { public extension ClosureCaptureSyntax { var capturesSelf: Bool { - expression.as(DeclReferenceExprSyntax.self)?.isSelf == true + name.text == "self" } var capturesWeakly: Bool { diff --git a/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift b/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift index 51ba4427d..f63235284 100644 --- a/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift +++ b/Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift @@ -8,6 +8,7 @@ enum AutoConfigParser: MemberMacro { static func expansion( of _: AttributeSyntax, providingMembersOf declaration: some DeclGroupSyntax, + conformingTo _: [TypeSyntax], in context: some MacroExpansionContext ) throws -> [DeclSyntax] { guard let configuration = declaration.as(StructDeclSyntax.self) else { diff --git a/Source/YamsWrapper/Empty.swift b/Source/YamsWrapper/Empty.swift new file mode 100644 index 000000000..e69de29bb