Skip to content

Update swift-format for renamed children in SwiftSyntax #562

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

Merged
merged 1 commit into from
Jul 23, 2023
Merged
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
112 changes: 56 additions & 56 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public final class AllPublicDeclarationsHaveDocumentation: SyntaxLintRule {
}

public override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseMissingDocComment(DeclSyntax(node), name: node.identifier.text, modifiers: node.modifiers)
diagnoseMissingDocComment(DeclSyntax(node), name: node.name.text, modifiers: node.modifiers)
return .skipChildren
}

Expand All @@ -56,17 +56,17 @@ public final class AllPublicDeclarationsHaveDocumentation: SyntaxLintRule {
}

public override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseMissingDocComment(DeclSyntax(node), name: node.identifier.text, modifiers: node.modifiers)
diagnoseMissingDocComment(DeclSyntax(node), name: node.name.text, modifiers: node.modifiers)
return .skipChildren
}

public override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseMissingDocComment(DeclSyntax(node), name: node.identifier.text, modifiers: node.modifiers)
diagnoseMissingDocComment(DeclSyntax(node), name: node.name.text, modifiers: node.modifiers)
return .skipChildren
}

public override func visit(_ node: TypealiasDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseMissingDocComment(DeclSyntax(node), name: node.identifier.text, modifiers: node.modifiers)
diagnoseMissingDocComment(DeclSyntax(node), name: node.name.text, modifiers: node.modifiers)
return .skipChildren
}

Expand Down
32 changes: 16 additions & 16 deletions Sources/SwiftFormatRules/AlwaysUseLowerCamelCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,23 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
}

public override func visit(_ node: ClosureSignatureSyntax) -> SyntaxVisitorContinueKind {
if let parameterClause = node.parameterClause {
if let closureParamList = parameterClause.as(ClosureParamListSyntax.self) {
if let input = node.parameterClause {
if let closureParamList = input.as(ClosureParamListSyntax.self) {
for param in closureParamList {
diagnoseLowerCamelCaseViolations(
param.name, allowUnderscores: false, description: identifierDescription(for: node))
}
} else if let closureParameterClause = parameterClause.as(ClosureParameterClauseSyntax.self) {
for param in closureParameterClause.parameterList {
} else if let parameterClause = input.as(ClosureParameterClauseSyntax.self) {
for param in parameterClause.parameters {
diagnoseLowerCamelCaseViolations(
param.firstName, allowUnderscores: false, description: identifierDescription(for: node))
if let secondName = param.secondName {
diagnoseLowerCamelCaseViolations(
secondName, allowUnderscores: false, description: identifierDescription(for: node))
}
}
} else if let enumCaseParameterClause = parameterClause.as(EnumCaseParameterClauseSyntax.self) {
for param in enumCaseParameterClause.parameterList {
} else if let parameterClause = input.as(EnumCaseParameterClauseSyntax.self) {
for param in parameterClause.parameters {
if let firstName = param.firstName {
diagnoseLowerCamelCaseViolations(
firstName, allowUnderscores: false, description: identifierDescription(for: node))
Expand All @@ -96,8 +96,8 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
secondName, allowUnderscores: false, description: identifierDescription(for: node))
}
}
} else if let parameterClause = parameterClause.as(ParameterClauseSyntax.self) {
for param in parameterClause.parameterList {
} else if let parameterClause = input.as(ParameterClauseSyntax.self) {
for param in parameterClause.parameters {
diagnoseLowerCamelCaseViolations(
param.firstName, allowUnderscores: false, description: identifierDescription(for: node))
if let secondName = param.secondName {
Expand All @@ -122,9 +122,9 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
// underscores to separate phrases in very detailed test names.
let allowUnderscores = testCaseFuncs.contains(node)
diagnoseLowerCamelCaseViolations(
node.identifier, allowUnderscores: allowUnderscores,
node.name, allowUnderscores: allowUnderscores,
description: identifierDescription(for: node))
for param in node.signature.parameterClause.parameterList {
for param in node.signature.parameterClause.parameters {
// These identifiers aren't described using `identifierDescription(for:)` because no single
// node can disambiguate the argument label from the parameter name.
diagnoseLowerCamelCaseViolations(
Expand All @@ -139,7 +139,7 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {

public override func visit(_ node: EnumCaseElementSyntax) -> SyntaxVisitorContinueKind {
diagnoseLowerCamelCaseViolations(
node.identifier, allowUnderscores: false, description: identifierDescription(for: node))
node.name, allowUnderscores: false, description: identifierDescription(for: node))
return .skipChildren
}

Expand All @@ -160,9 +160,9 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
} else if let functionDecl = member.decl.as(FunctionDeclSyntax.self) {
// Identify test methods using the same heuristics as XCTest: name starts with "test", has
// no arguments, and returns a void type.
if functionDecl.identifier.text.starts(with: "test")
&& functionDecl.signature.parameterClause.parameterList.isEmpty
&& (functionDecl.signature.returnClause.map(\.isVoid) ?? true)
if functionDecl.name.text.starts(with: "test")
&& functionDecl.signature.parameterClause.parameters.isEmpty
&& (functionDecl.signature.returnClause.map(\.isVoid) ?? true)
{
set.insert(functionDecl)
}
Expand Down Expand Up @@ -203,10 +203,10 @@ fileprivate func identifierDescription<NodeType: SyntaxProtocol>(for node: NodeT
extension ReturnClauseSyntax {
/// Whether this return clause specifies an explicit `Void` return type.
fileprivate var isVoid: Bool {
if let returnTypeIdentifier = returnType.as(SimpleTypeIdentifierSyntax.self) {
if let returnTypeIdentifier = type.as(SimpleTypeIdentifierSyntax.self) {
return returnTypeIdentifier.name.text == "Void"
}
if let returnTypeTuple = returnType.as(TupleTypeSyntax.self) {
if let returnTypeTuple = type.as(TupleTypeSyntax.self) {
return returnTypeTuple.elements.isEmpty
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public final class AmbiguousTrailingClosureOverload: SyntaxLintRule {
let decl = decls[0]
diagnose(
.ambiguousTrailingClosureOverload(decl.fullDeclName),
on: decl.identifier,
on: decl.name,
notes: decls.dropFirst().map { decl in
Finding.Note(
message: .otherAmbiguousOverloadHere(decl.fullDeclName),
location: Finding.Location(
decl.identifier.startLocation(converter: self.context.sourceLocationConverter))
decl.name.startLocation(converter: self.context.sourceLocationConverter))
)
})
}
Expand All @@ -39,13 +39,13 @@ public final class AmbiguousTrailingClosureOverload: SyntaxLintRule {
var overloads = [String: [FunctionDeclSyntax]]()
var staticOverloads = [String: [FunctionDeclSyntax]]()
for fn in functions {
let params = fn.signature.parameterClause.parameterList
let params = fn.signature.parameterClause.parameters
guard let firstParam = params.firstAndOnly else { continue }
guard firstParam.type.is(FunctionTypeSyntax.self) else { continue }
if let mods = fn.modifiers, mods.has(modifier: "static") || mods.has(modifier: "class") {
staticOverloads[fn.identifier.text, default: []].append(fn)
staticOverloads[fn.name.text, default: []].append(fn)
} else {
overloads[fn.identifier.text, default: []].append(fn)
overloads[fn.name.text, default: []].append(fn)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ import SwiftSyntax
public final class DontRepeatTypeInStaticProperties: SyntaxLintRule {

public override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.identifier.text)
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.name.text)
return .skipChildren
}

public override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.identifier.text)
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.name.text)
return .skipChildren
}

public override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.identifier.text)
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.name.text)
return .skipChildren
}

public override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.identifier.text)
diagnoseStaticMembers(node.memberBlock.members, endingWith: node.name.text)
return .skipChildren
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/FullyIndirectEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
return DeclSyntax(node)
}

diagnose(.moveIndirectKeywordToEnumDecl(name: node.identifier.text), on: node.identifier)
diagnose(.moveIndirectKeywordToEnumDecl(name: node.name.text), on: node.name)

// Removes 'indirect' keyword from cases, reformats
let newMembers = enumMembers.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import SwiftSyntax
extension FunctionDeclSyntax {
/// Constructs a name for a function that includes parameter labels, i.e. `foo(_:bar:)`.
var fullDeclName: String {
let params = signature.parameterClause.parameterList.map { param in
let params = signature.parameterClause.parameters.map { param in
"\(param.firstName.text):"
}
return "\(identifier.text)(\(params.joined()))"
return "\(name.text)(\(params.joined()))"
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/NeverForceUnwrap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class NeverForceUnwrap: SyntaxLintRule {
guard context.importsXCTest == .doesNotImportXCTest else { return .skipChildren }
guard let questionOrExclamation = node.questionOrExclamationMark else { return .skipChildren }
guard questionOrExclamation.tokenKind == .exclamationMark else { return .skipChildren }
diagnose(.doNotForceCast(name: node.typeName.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description), on: node)
diagnose(.doNotForceCast(name: node.type.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description), on: node)
return .skipChildren
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import SwiftSyntax
public final class NoEmptyTrailingClosureParentheses: SyntaxFormatRule {

public override func visit(_ node: FunctionCallExprSyntax) -> ExprSyntax {
guard node.argumentList.count == 0 else { return super.visit(node) }
guard node.arguments.count == 0 else { return super.visit(node) }

guard let trailingClosure = node.trailingClosure,
node.argumentList.isEmpty && node.leftParen != nil else
node.arguments.isEmpty && node.leftParen != nil else
{
return super.visit(node)
}
Expand Down
6 changes: 3 additions & 3 deletions Sources/SwiftFormatRules/NoLabelsInCasePatterns.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class NoLabelsInCasePatterns: SyntaxFormatRule {

// Search function call argument list for violations
var newArgs: [TupleExprElementSyntax] = []
for argument in funcCall.argumentList {
for argument in funcCall.arguments {
guard let label = argument.label else {
newArgs.append(argument)
continue
Expand All @@ -50,7 +50,7 @@ public final class NoLabelsInCasePatterns: SyntaxFormatRule {
}

// Remove label if it's the same as the value identifier
let name = valueBinding.valuePattern.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description
let name = valueBinding.pattern.with(\.leadingTrivia, []).with(\.trailingTrivia, []).description
guard name == label.text else {
newArgs.append(argument)
continue
Expand All @@ -60,7 +60,7 @@ public final class NoLabelsInCasePatterns: SyntaxFormatRule {
}

let newArgList = TupleExprElementListSyntax(newArgs)
let newFuncCall = funcCall.with(\.argumentList, newArgList)
let newFuncCall = funcCall.with(\.arguments, newArgList)
let newExpPat = expPat.with(\.expression, ExprSyntax(newFuncCall))
let newItem = item.with(\.pattern, PatternSyntax(newExpPat))
newCaseItems.append(newItem)
Expand Down
18 changes: 9 additions & 9 deletions Sources/SwiftFormatRules/NoLeadingUnderscores.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ public final class NoLeadingUnderscores: SyntaxLintRule {
public override class var isOptIn: Bool { return true }

public override func visit(_ node: AssociatedtypeDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: EnumCaseElementSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

Expand Down Expand Up @@ -93,22 +93,22 @@ public final class NoLeadingUnderscores: SyntaxLintRule {
}

public override func visit(_ node: PrecedenceGroupDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

public override func visit(_ node: TypealiasDeclSyntax) -> SyntaxVisitorContinueKind {
diagnoseIfNameStartsWithUnderscore(node.identifier)
diagnoseIfNameStartsWithUnderscore(node.name)
return .visitChildren
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public final class NoVoidReturnOnFunctionSignature: SyntaxFormatRule {
/// it for closure signatures, because that may introduce an ambiguity when closure signatures
/// are inferred.
public override func visit(_ node: FunctionSignatureSyntax) -> FunctionSignatureSyntax {
if let returnType = node.returnClause?.returnType.as(SimpleTypeIdentifierSyntax.self), returnType.name.text == "Void" {
if let returnType = node.returnClause?.type.as(SimpleTypeIdentifierSyntax.self), returnType.name.text == "Void" {
diagnose(.removeRedundantReturn("Void"), on: returnType)
return node.with(\.returnClause, nil)
}
if let tupleReturnType = node.returnClause?.returnType.as(TupleTypeSyntax.self), tupleReturnType.elements.isEmpty {
if let tupleReturnType = node.returnClause?.type.as(TupleTypeSyntax.self), tupleReturnType.elements.isEmpty {
diagnose(.removeRedundantReturn("()"), on: tupleReturnType)
return node.with(\.returnClause, nil)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/OneCasePerLine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public final class OneCasePerLine: SyntaxFormatRule {
if element.associatedValue != nil || element.rawValue != nil {
// Once we reach one of these, we need to write out the ones we've collected so far, then
// emit a separate case declaration with the associated/raw value element.
diagnose(.moveAssociatedOrRawValueCase(name: element.identifier.text), on: element)
diagnose(.moveAssociatedOrRawValueCase(name: element.name.text), on: element)

if let caseDeclForCollectedElements = collector.makeCaseDeclAndReset() {
newMembers.append(member.with(\.decl, DeclSyntax(caseDeclForCollectedElements)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import SwiftSyntax
public final class OnlyOneTrailingClosureArgument: SyntaxLintRule {

public override func visit(_ node: FunctionCallExprSyntax) -> SyntaxVisitorContinueKind {
guard (node.argumentList.contains { $0.expression.is(ClosureExprSyntax.self) }) else {
guard (node.arguments.contains { $0.expression.is(ClosureExprSyntax.self) }) else {
return .skipChildren
}
guard node.trailingClosure != nil else { return .skipChildren }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import SwiftSyntax
/// Format: `-> ()` is replaced with `-> Void`
public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
public override func visit(_ node: FunctionTypeSyntax) -> TypeSyntax {
guard let returnType = node.returnClause.returnType.as(TupleTypeSyntax.self),
guard let returnType = node.returnClause.type.as(TupleTypeSyntax.self),
returnType.elements.count == 0
else {
return super.visit(node)
Expand All @@ -45,13 +45,13 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
let voidKeyword = makeVoidIdentifierType(toReplace: returnType)
var rewrittenNode = node
rewrittenNode.parameters = parameters
rewrittenNode.returnClause.returnType = TypeSyntax(voidKeyword)
rewrittenNode.returnClause.type = TypeSyntax(voidKeyword)
return TypeSyntax(rewrittenNode)
}

public override func visit(_ node: ClosureSignatureSyntax) -> ClosureSignatureSyntax {
guard let returnClause = node.returnClause,
let returnType = returnClause.returnType.as(TupleTypeSyntax.self),
let returnType = returnClause.type.as(TupleTypeSyntax.self),
returnType.elements.count == 0
else {
return super.visit(node)
Expand Down Expand Up @@ -80,7 +80,7 @@ public final class ReturnVoidInsteadOfEmptyTuple: SyntaxFormatRule {
closureParameterClause = node.parameterClause
}
let voidKeyword = makeVoidIdentifierType(toReplace: returnType)
return node.with(\.parameterClause, closureParameterClause).with(\.returnClause, returnClause.with(\.returnType, TypeSyntax(voidKeyword)))
return node.with(\.parameterClause, closureParameterClause).with(\.returnClause, returnClause.with(\.type, TypeSyntax(voidKeyword)))
}

/// Returns a value indicating whether the leading trivia of the given token contained any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public final class UseLetInEveryBoundCaseVariable: SyntaxLintRule {
public override func visit(_ node: ValueBindingPatternSyntax) -> SyntaxVisitorContinueKind {
// Diagnose a pattern binding if it is a function call and the callee is a member access
// expression (e.g., `case let .x(y)` or `case let T.x(y)`).
if canDistributeLetVarThroughPattern(node.valuePattern) {
if canDistributeLetVarThroughPattern(node.pattern) {
diagnose(.useLetInBoundCaseVariables, on: node)
}
return .visitChildren
Expand Down
Loading