Skip to content

Commit

Permalink
Update SwiftSyntax (#5198)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny authored Sep 14, 2023
1 parent 2df6516 commit 35a7881
Show file tree
Hide file tree
Showing 35 changed files with 94 additions and 146 deletions.
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/apple/swift-syntax.git",
"state" : {
"revision" : "e42bece52df2496d60b1b2a762fee9ffde7fc205",
"version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-07-a"
"revision" : "057bdfb4632cc758973d46efaf98e43d07ee4bdb",
"version" : "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-28-a"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser.git", .upToNextMinor(from: "1.2.1")),
.package(url: "https://github.com/apple/swift-syntax.git", exact: "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-07-a"),
.package(url: "https://github.com/apple/swift-syntax.git", exact: "509.0.0-swift-DEVELOPMENT-SNAPSHOT-2023-08-28-a"),
.package(url: "https://github.com/jpsim/SourceKitten.git", .upToNextMinor(from: "0.34.1")),
.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 @@ -148,8 +148,8 @@ private extension ConvenienceTypeRule {
attributes: AttributeListSyntax?,
members: MemberBlockSyntax) -> Bool {
guard inheritance.isNilOrEmpty,
!attributes.containsObjcMembers,
!attributes.containsObjc,
attributes?.containsObjcMembers == false,
attributes?.containsObjc == false,
!members.members.isEmpty else {
return false
}
Expand Down Expand Up @@ -202,23 +202,17 @@ private extension InheritanceClauseSyntax? {
}
}

private extension AttributeListSyntax? {
private extension AttributeListSyntax {
var containsObjcMembers: Bool {
contains(attributeNamed: "objcMembers")
}

var containsObjc: Bool {
contains(attributeNamed: "objc")
}
}

private extension AttributeListSyntax? {
var hasUnavailableAttribute: Bool {
guard let attrs = self else {
return false
}

return attrs.contains { elem in
contains { elem in
guard let attr = elem.as(AttributeSyntax.self),
let arguments = attr.arguments?.as(AvailabilityArgumentListSyntax.self) else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ private extension NoExtensionAccessModifierRule {
override var skippableDeclarations: [DeclSyntaxProtocol.Type] { .all }

override func visitPost(_ node: ExtensionDeclSyntax) {
if let modifiers = node.modifiers, modifiers.isNotEmpty {
let modifiers = node.modifiers
if modifiers.isNotEmpty {
violations.append(modifiers.positionAfterSkippingLeadingTrivia)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -166,7 +166,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -178,7 +178,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -190,7 +190,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -202,7 +202,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -214,7 +214,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -226,7 +226,7 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}

Expand All @@ -238,24 +238,22 @@ private extension PrivateOverFilePrivateRule {
}

correctionPositions.append(modifier.positionAfterSkippingLeadingTrivia)
let newNode = node.with(\.modifiers, node.modifiers?.replacing(fileprivateModifierIndex: modifierIndex))
let newNode = node.with(\.modifiers, node.modifiers.replacing(fileprivateModifierIndex: modifierIndex))
return DeclSyntax(newNode)
}
}
}

private extension DeclModifierListSyntax? {
private extension DeclModifierListSyntax {
var fileprivateModifierIndex: DeclModifierListSyntax.Index? {
self?.firstIndex(where: { $0.name.tokenKind == .keyword(.fileprivate) })
firstIndex(where: { $0.name.tokenKind == .keyword(.fileprivate) })
}

var fileprivateModifier: DeclModifierSyntax? {
fileprivateModifierIndex.flatMap { self?[$0] }
fileprivateModifierIndex.flatMap { self[$0] }
}
}

private extension DeclModifierListSyntax {
func replacing(fileprivateModifierIndex: DeclModifierListSyntax.Index) -> DeclModifierListSyntax? {
func replacing(fileprivateModifierIndex: DeclModifierListSyntax.Index) -> DeclModifierListSyntax {
let fileprivateModifier = self[fileprivateModifierIndex]
return with(
\.[fileprivateModifierIndex],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ private extension AttributeListSyntax {
} else if parent?.isFunctionOrStoredProperty == true,
let parentClassDecl = parent?.parent?.parent?.parent?.parent?.as(ClassDeclSyntax.self),
parentClassDecl.attributes.contains(attributeNamed: "objcMembers") {
return parent?.functionOrVariableModifiers.isPrivateOrFileprivate == true ? nil : objcAttribute
return parent?.functionOrVariableModifiers?.isPrivateOrFileprivate == true ? nil : objcAttribute
} else if let parentExtensionDecl = parent?.parent?.parent?.parent?.parent?.as(ExtensionDeclSyntax.self),
parentExtensionDecl.attributes?.objCAttribute != nil {
parentExtensionDecl.attributes.objCAttribute != nil {
return objcAttribute
} else {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ private extension RedundantSetAccessControlRule {
}

override func visitPost(_ node: VariableDeclSyntax) {
guard let modifiers = node.modifiers, let setAccessor = modifiers.setAccessor else {
let modifiers = node.modifiers
guard let setAccessor = modifiers.setAccessor else {
return
}

Expand Down Expand Up @@ -122,21 +123,7 @@ private extension SyntaxProtocol {

private extension DeclSyntax {
var modifiers: DeclModifierListSyntax? {
if let decl = self.as(ClassDeclSyntax.self) {
return decl.modifiers ?? DeclModifierListSyntax([])
} else if let decl = self.as(ActorDeclSyntax.self) {
return decl.modifiers ?? DeclModifierListSyntax([])
} else if let decl = self.as(StructDeclSyntax.self) {
return decl.modifiers ?? DeclModifierListSyntax([])
} else if let decl = self.as(ProtocolDeclSyntax.self) {
return decl.modifiers ?? DeclModifierListSyntax([])
} else if let decl = self.as(ExtensionDeclSyntax.self) {
return decl.modifiers ?? DeclModifierListSyntax([])
} else if let decl = self.as(EnumDeclSyntax.self) {
return decl.modifiers ?? DeclModifierListSyntax([])
}

return nil
self.asProtocol(WithModifiersSyntax.self)?.modifiers
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private extension TypeNameRule {
}

private func violation(identifier: TokenSyntax,
modifiers: DeclModifierListSyntax?,
modifiers: DeclModifierListSyntax,
inheritedTypes: InheritedTypeListSyntax?) -> ReasonedRuleViolation? {
let originalName = identifier.text
let nameConfiguration = configuration.nameConfiguration
Expand Down Expand Up @@ -135,7 +135,7 @@ private extension String {
return substring(from: 0, length: lastPreviewsIndex)
}

func strippingLeadingUnderscoreIfPrivate(modifiers: DeclModifierListSyntax?) -> String {
func strippingLeadingUnderscoreIfPrivate(modifiers: DeclModifierListSyntax) -> String {
if first == "_", modifiers.isPrivateOrFileprivate {
return String(self[index(after: startIndex)...])
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,9 @@ private extension FunctionDeclSyntax {
}
}

private extension AttributeListSyntax? {
private extension AttributeListSyntax {
var hasUnavailableAttribute: Bool {
guard let attrs = self else {
return false
}

return attrs.contains { elem in
contains { elem in
guard let attr = elem.as(AttributeSyntax.self),
let arguments = attr.arguments?.as(AvailabilityArgumentListSyntax.self) else {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ private extension StructDeclSyntax {
let member = memberItem.decl
// Collect all stored variables into a list.
if let varDecl = member.as(VariableDeclSyntax.self) {
let modifiers = varDecl.modifiers
if modifiers == nil || !modifiers.isStatic {
if !varDecl.modifiers.isStatic {
storedProperties.append(varDecl)
}
} else if let initDecl = member.as(InitializerDeclSyntax.self),
Expand Down Expand Up @@ -301,9 +300,7 @@ private enum AccessLevel {
private func synthesizedInitializerAccessLevel(using storedProperties: [VariableDeclSyntax]) -> AccessLevel {
var hasFileprivate = false
for property in storedProperties {
guard let modifiers = property.modifiers else {
continue
}
let modifiers = property.modifiers

// Private takes precedence, so finding 1 private property defines the access level.
if modifiers.contains(where: { $0.name.tokenKind == .keyword(.private) && $0.detail == nil }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,9 @@ private extension CatchItemSyntax {

private final class UntypedErrorInCatchRuleVisitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: CatchClauseSyntax) {
guard let item = node.catchItems?.onlyElement,
item.isIdentifierPattern else {
guard let item = node.catchItems.onlyElement, item.isIdentifierPattern else {
return
}

violations.append(node.catchKeyword.positionAfterSkippingLeadingTrivia)
}
}
Expand All @@ -142,7 +140,7 @@ private final class UntypedErrorInCatchRuleRewriter: SyntaxRewriter, ViolationsS

override func visit(_ node: CatchClauseSyntax) -> CatchClauseSyntax {
guard
let item = node.catchItems?.onlyElement,
let item = node.catchItems.onlyElement,
item.isIdentifierPattern,
!node.isContainedIn(regions: disabledRegions, locationConverter: locationConverter)
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ private extension FunctionCallExprSyntax {
}

var hasNoArguments: Bool {
trailingClosure == nil &&
(additionalTrailingClosures?.isEmpty ?? true) &&
arguments.isEmpty
trailingClosure == nil
&& additionalTrailingClosures.isEmpty
&& arguments.isEmpty
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private extension DiscardedNotificationCenterObserverRule {
}
}

private extension AttributeListSyntax? {
private extension AttributeListSyntax {
var hasDiscardableResultAttribute: Bool {
contains(attributeNamed: "discardableResult")
}
Expand Down
11 changes: 3 additions & 8 deletions Source/SwiftLintBuiltInRules/Rules/Lint/DynamicInlineRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,10 @@ struct DynamicInlineRule: SwiftSyntaxRule, ConfigurationProviderRule {
private extension DynamicInlineRule {
private final class Visitor: ViolationsSyntaxVisitor {
override func visitPost(_ node: FunctionDeclSyntax) {
guard let modifiers = node.modifiers,
let attributes = node.attributes,
modifiers.contains(where: { $0.name.text == "dynamic" }),
attributes.contains(where: { $0.as(AttributeSyntax.self)?.isInlineAlways == true })
else {
return
if node.modifiers.contains(where: { $0.name.text == "dynamic" }),
node.attributes.contains(where: { $0.as(AttributeSyntax.self)?.isInlineAlways == true }) {
violations.append(node.funcKeyword.positionAfterSkippingLeadingTrivia)
}

violations.append(node.funcKeyword.positionAfterSkippingLeadingTrivia)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@ private extension DeclModifierSyntax {
switch name.tokenKind {
case .keyword(.internal)
where nearestNominalParent.modifiers.isPrivate ||
nearestNominalParent.modifiers.isFileprivate:
nearestNominalParent.modifiers?.isFileprivate == true:
return true
case .keyword(.internal)
where !nearestNominalParent.modifiers.containsACLModifier:
guard let nominalExtension = nearestNominalParent.nearestNominalExtensionDeclParent() else {
return false
}
return nominalExtension.modifiers.isPrivate ||
nominalExtension.modifiers.isFileprivate
nominalExtension.modifiers?.isFileprivate == true
case .keyword(.public)
where nearestNominalParent.modifiers.isPrivate ||
nearestNominalParent.modifiers.isFileprivate ||
nearestNominalParent.modifiers.isInternal:
nearestNominalParent.modifiers?.isFileprivate == true ||
nearestNominalParent.modifiers.isInternal:
return true
case .keyword(.public)
where !nearestNominalParent.modifiers.containsACLModifier:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private extension SyntaxProtocol {
}
}

private extension AttributeListSyntax? {
private extension AttributeListSyntax {
var isObjc: Bool {
contains(attributeNamed: "objc") || contains(attributeNamed: "objcMembers")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ private extension PrivateOutletRule {
guard
let decl = node.decl.as(VariableDeclSyntax.self),
decl.attributes.contains(attributeNamed: "IBOutlet"),
decl.modifiers?.isPrivateOrFilePrivate != true
!decl.modifiers.isPrivateOrFilePrivate
else {
return
}

if allowPrivateSet && decl.modifiers?.isPrivateOrFilePrivateSet == true {
if allowPrivateSet && decl.modifiers.isPrivateOrFilePrivateSet {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,10 @@ private extension InheritedTypeListSyntax {
}
}

private extension AttributeListSyntax? {
private extension AttributeListSyntax {
/// Returns `true` if the attribute's identifier is equal to `State` or `StateObject`
var hasStateAttribute: Bool {
guard let attributes = self else { return false }

return attributes.contains { attr in
contains { attr in
guard let stateAttr = attr.as(AttributeSyntax.self),
let identifier = stateAttr.attributeName.as(IdentifierTypeSyntax.self) else {
return false
Expand Down
Loading

0 comments on commit 35a7881

Please sign in to comment.