Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for SwiftSyntax 6.1 release #6003

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax.git",
"state" : {
"revision" : "cb53fa1bd3219b0b23ded7dfdd3b2baff266fd25",
"version" : "600.0.0"
"revision" : "8ceb17262cf9a7c89ffaf2d1d62c32cee76151aa",
"version" : "601.0.0-prerelease-2025-02-12"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", 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-prerelease-2025-02-12"),
.package(url: "https://github.com/jpsim/SourceKitten.git", .upToNextMinor(from: "0.35.0")),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.6"),
.package(url: "https://github.com/scottrhoyt/SwiftyTextTable.git", from: "0.9.0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ private final class ImportPathVisitor: SyntaxVisitor {
}
}

private typealias ByteSourceRange = Range<AbsolutePosition>

private final class IfConfigClauseVisitor: SyntaxVisitor {
var ifConfigRanges = [ByteSourceRange]()

override func visitPost(_ node: IfConfigClauseSyntax) {
ifConfigRanges.append(node.totalByteRange)
ifConfigRanges.append(node.range)
}
}

Expand All @@ -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)
}
}

Expand All @@ -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<AbsolutePosition>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,10 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor {

override func visitPost(_ node: SameTypeRequirementSyntax) {
// @_specialize(where S == ↓Array<Character>)
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)
}
}
Expand All @@ -180,7 +180,7 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor {
// If there's no type, check all inner generics like in the case of 'Box<Array<T>>'
node.genericArgumentClause.arguments
.lazy
.compactMap { self.violation(in: $0.argument) }
.compactMap { self.violation(in: $0.argument.as(TypeSyntax.self)) }
.first
.map { violations.append($0) }
}
Expand All @@ -203,7 +203,7 @@ private final class SyntacticSugarRuleVisitor: SyntaxVisitor {

// If there's no type, check all inner generics like in the case of 'Box<Array<T>>'
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
}

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct LocalDocCommentRule: SwiftSyntaxRule, OptInRule {

private extension LocalDocCommentRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
private let docCommentRanges: [ByteSourceRange]
private let docCommentRanges: [Range<AbsolutePosition>]

init(configuration: ConfigurationType,
file: SwiftLintFile,
Expand All @@ -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))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,12 @@ public extension AbsolutePosition {
}
}

public extension ByteSourceRange {
public extension Range<AbsolutePosition> {
func toSourceKittenByteRange() -> ByteRange {
ByteRange(location: ByteCount(offset), length: ByteCount(length))
ByteRange(
location: ByteCount(lowerBound),
length: ByteCount(upperBound.utf8Offset) - ByteCount(lowerBound.utf8Offset)
)
}
}

Expand Down Expand Up @@ -341,7 +344,7 @@ public extension DeclReferenceExprSyntax {

public extension ClosureCaptureSyntax {
var capturesSelf: Bool {
expression.as(DeclReferenceExprSyntax.self)?.isSelf == true
name.text == "self"
}

var capturesWeakly: Bool {
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintCoreMacros/RuleConfigurationMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading