Skip to content

Commit c7dbb88

Browse files
committed
[PackageManifest] Address NFC review feedback
Addresses minor comments like indentation, naming, and code style.
1 parent e6bd577 commit c7dbb88

File tree

6 files changed

+27
-38
lines changed

6 files changed

+27
-38
lines changed

Sources/SwiftRefactor/PackageManifest/AddPackageDependency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public struct AddPackageDependency: ManifestEditRefactoringProvider {
6666
) throws -> FunctionCallExprSyntax {
6767
try packageCall.appendingToArrayArgument(
6868
label: "dependencies",
69-
trailingLabels: Self.argumentLabelsAfterDependencies,
69+
labelsAfter: Self.argumentLabelsAfterDependencies,
7070
newElement: dependency.asSyntax()
7171
)
7272
}

Sources/SwiftRefactor/PackageManifest/AddPackageTarget.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,12 @@ public struct AddPackageTarget: ManifestEditRefactoringProvider {
9797
target.dependencies.append(contentsOf: macroTargetDependencies)
9898

9999
default:
100-
break;
100+
break
101101
}
102102

103103
var newPackageCall = try packageCall.appendingToArrayArgument(
104104
label: "targets",
105-
trailingLabels: Self.argumentLabelsAfterTargets,
105+
labelsAfter: Self.argumentLabelsAfterTargets,
106106
newElement: target.asSyntax()
107107
)
108108

Sources/SwiftRefactor/PackageManifest/AddPluginUsage.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public struct AddPluginUsage: ManifestEditRefactoringProvider {
5252

5353
let newTargetCall = try targetCall.appendingToArrayArgument(
5454
label: "plugins",
55-
trailingLabels: Self.argumentLabelsAfterPluginUsages,
55+
labelsAfter: Self.argumentLabelsAfterPluginUsages,
5656
newElement: pluginUsage.asSyntax()
5757
)
5858

Sources/SwiftRefactor/PackageManifest/AddProduct.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public struct AddProduct: ManifestEditRefactoringProvider {
5050

5151
let newPackageCall = try packageCall.appendingToArrayArgument(
5252
label: "products",
53-
trailingLabels: argumentLabelsAfterProducts,
53+
labelsAfter: argumentLabelsAfterProducts,
5454
newElement: product.asSyntax()
5555
)
5656

Sources/SwiftRefactor/PackageManifest/AddTargetDependency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public struct AddTargetDependency: ManifestEditRefactoringProvider {
8383
) throws -> FunctionCallExprSyntax {
8484
try targetCall.appendingToArrayArgument(
8585
label: "dependencies",
86-
trailingLabels: Self.argumentLabelsAfterDependencies,
86+
labelsAfter: Self.argumentLabelsAfterDependencies,
8787
newElement: dependency.asSyntax()
8888
)
8989
}

Sources/SwiftRefactor/PackageManifest/SyntaxEditUtils.swift

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension Trivia {
2626

2727
/// Produce trivia from the last newline to the end, dropping anything
2828
/// prior to that.
29-
func onlyLastLine() -> Trivia {
29+
var onlyLastLine: Trivia {
3030
guard let lastNewline = pieces.lastIndex(where: { $0.isNewline }) else {
3131
return self
3232
}
@@ -122,15 +122,13 @@ extension LabeledExprListSyntax {
122122
/// context.
123123
func insertingArgument(
124124
at position: SyntaxChildrenIndex,
125-
generator: (Trivia, TokenSyntax?) -> LabeledExprSyntax
125+
generator: (_ leadingTrivia: Trivia, _ trailingComma: TokenSyntax?) -> LabeledExprSyntax
126126
) -> LabeledExprListSyntax {
127127
// Turn the arguments into an array so we can manipulate them.
128128
var arguments = Array(self)
129129

130130
let positionIdx = distance(from: startIndex, to: position)
131131

132-
let commaToken = TokenSyntax.commaToken()
133-
134132
// Figure out leading trivia and adjust the prior argument (if there is
135133
// one) by adding a comma, if necessary.
136134
let leadingTrivia: Trivia
@@ -143,7 +141,7 @@ extension LabeledExprListSyntax {
143141

144142
// If the prior argument is missing a trailing comma, add one.
145143
if priorArgument.trailingComma == nil {
146-
arguments[positionIdx - 1].trailingComma = commaToken
144+
arguments[positionIdx - 1].trailingComma = .commaToken()
147145
}
148146
} else if positionIdx + 1 < count {
149147
leadingTrivia = arguments[positionIdx + 1].leadingTrivia
@@ -154,7 +152,7 @@ extension LabeledExprListSyntax {
154152
// Determine whether we need a trailing comma on this argument.
155153
let trailingComma: TokenSyntax?
156154
if position < endIndex {
157-
trailingComma = commaToken
155+
trailingComma = .commaToken()
158156
} else {
159157
trailingComma = nil
160158
}
@@ -194,9 +192,7 @@ extension FunctionCallExprSyntax {
194192
return false
195193
}
196194

197-
guard let stringLiteral = nameArgument.expression.as(StringLiteralExprSyntax.self),
198-
let literalValue = stringLiteral.representedLiteralValue
199-
else {
195+
guard let literalValue = nameArgument.expression.as(StringLiteralExprSyntax.self)?.representedLiteralValue else {
200196
return false
201197
}
202198

@@ -220,24 +216,21 @@ extension ArrayExprSyntax {
220216
) -> ArrayExprSyntax {
221217
var elements = self.elements
222218

223-
let commaToken = TokenSyntax.commaToken()
224-
225219
// If there are already elements, tack it on.
226220
let leadingTrivia: Trivia
227221
let trailingTrivia: Trivia
228222
let leftSquareTrailingTrivia: Trivia
229223
if let last = elements.last {
230224
// The leading trivia of the new element should match that of the
231225
// last element.
232-
leadingTrivia = last.leadingTrivia.onlyLastLine()
226+
leadingTrivia = last.leadingTrivia.onlyLastLine
233227

234228
// Add a trailing comma to the last element if it isn't already
235229
// there.
236230
if last.trailingComma == nil {
237231
var newElements = Array(elements)
238-
newElements[newElements.count - 1].trailingComma = commaToken
239-
newElements[newElements.count - 1].expression.trailingTrivia =
240-
Trivia()
232+
newElements[newElements.count - 1].trailingComma = .commaToken()
233+
newElements[newElements.count - 1].expression.trailingTrivia = Trivia()
241234
newElements[newElements.count - 1].trailingTrivia = last.trailingTrivia
242235
elements = ArrayElementListSyntax(newElements)
243236
}
@@ -257,7 +250,7 @@ extension ArrayExprSyntax {
257250
elements.append(
258251
ArrayElementSyntax(
259252
expression: element.with(\.leadingTrivia, leadingTrivia),
260-
trailingComma: commaToken.with(\.trailingTrivia, trailingTrivia)
253+
trailingComma: .commaToken().with(\.trailingTrivia, trailingTrivia)
261254
)
262255
)
263256

@@ -358,10 +351,10 @@ extension Array<LabeledExprSyntax> {
358351

359352
/// Append an array literal containing elements that can be rendered
360353
/// into expression syntax nodes.
361-
mutating func append<T>(
354+
mutating func append<T: ManifestSyntaxRepresentable>(
362355
label: String?,
363356
arrayLiteral: [T]
364-
) where T: ManifestSyntaxRepresentable, T.PreferredSyntax == ExprSyntax {
357+
) where T.PreferredSyntax == ExprSyntax {
365358
var elements: [ArrayElementSyntax] = []
366359
for element in arrayLiteral {
367360
elements.append(expression: element.asSyntax())
@@ -402,20 +395,20 @@ extension Array<LabeledExprSyntax> {
402395

403396
/// Append an array literal containing elements that can be rendered
404397
/// into expression syntax nodes.
405-
mutating func appendIf<T>(
398+
mutating func appendIf<T: ManifestSyntaxRepresentable>(
406399
label: String?,
407400
arrayLiteral: [T]?
408-
) where T: ManifestSyntaxRepresentable, T.PreferredSyntax == ExprSyntax {
401+
) where T.PreferredSyntax == ExprSyntax {
409402
guard let arrayLiteral else { return }
410403
append(label: label, arrayLiteral: arrayLiteral)
411404
}
412405

413406
/// Append an array literal containing elements that can be rendered
414407
/// into expression syntax nodes, but only if it's not empty.
415-
mutating func appendIfNonEmpty<T>(
408+
mutating func appendIfNonEmpty<T: ManifestSyntaxRepresentable>(
416409
label: String?,
417410
arrayLiteral: [T]
418-
) where T: ManifestSyntaxRepresentable, T.PreferredSyntax == ExprSyntax {
411+
) where T.PreferredSyntax == ExprSyntax {
419412
if arrayLiteral.isEmpty { return }
420413

421414
append(label: label, arrayLiteral: arrayLiteral)
@@ -468,7 +461,7 @@ extension FunctionCallExprSyntax {
468461
/// - Returns: the function call after making this change.
469462
func appendingToArrayArgument(
470463
label: String,
471-
trailingLabels: Set<String>,
464+
labelsAfter: Set<String>,
472465
newElement: ExprSyntax
473466
) throws -> FunctionCallExprSyntax {
474467
// If there is already an argument with this name, append to the array
@@ -483,7 +476,7 @@ extension FunctionCallExprSyntax {
483476

484477
// Format the element appropriately for the context.
485478
let indentation = Trivia(
486-
pieces: arg.leadingTrivia.filter { $0.isSpaceOrTab }
479+
pieces: arg.leadingTrivia.filter(\.isSpaceOrTab)
487480
)
488481
let format = BasicFormat(
489482
indentationWidth: [defaultIndent],
@@ -504,13 +497,13 @@ extension FunctionCallExprSyntax {
504497

505498
// Insert the new argument at the appropriate place in the call.
506499
let insertionPos = arguments.findArgumentInsertionPosition(
507-
labelsAfter: trailingLabels
500+
labelsAfter: labelsAfter
508501
)
509502
let newArguments = arguments.insertingArgument(
510503
at: insertionPos
511504
) { (leadingTrivia, trailingComma) in
512505
// Format the element appropriately for the context.
513-
let indentation = Trivia(pieces: leadingTrivia.filter { $0.isSpaceOrTab })
506+
let indentation = Trivia(pieces: leadingTrivia.filter(\.isSpaceOrTab))
514507
let format = BasicFormat(
515508
indentationWidth: [defaultIndent],
516509
initialIndentation: indentation.appending(defaultIndent)
@@ -521,9 +514,7 @@ extension FunctionCallExprSyntax {
521514
// Form the array.
522515
let newArgument = ArrayExprSyntax(
523516
leadingTrivia: .space,
524-
leftSquare: .leftSquareToken(
525-
trailingTrivia: .newline
526-
),
517+
leftSquare: .leftSquareToken(trailingTrivia: .newline),
527518
elements: ArrayElementListSyntax(
528519
[
529520
ArrayElementSyntax(
@@ -532,9 +523,7 @@ extension FunctionCallExprSyntax {
532523
)
533524
]
534525
),
535-
rightSquare: .rightSquareToken(
536-
leadingTrivia: leadingTrivia
537-
)
526+
rightSquare: .rightSquareToken(leadingTrivia: leadingTrivia)
538527
)
539528

540529
// Create the labeled argument for the array.

0 commit comments

Comments
 (0)