Skip to content

Commit 8019514

Browse files
committed
Update hasAttribute extension signature to check module name as well.
1 parent c7d8018 commit 8019514

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

Sources/SwiftFormat/Core/SyntaxProtocol+Convenience.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ extension SyntaxProtocol {
154154
var hasTestAncestor: Bool {
155155
var parent = self.parent
156156
while let existingParent = parent {
157-
if let functionDecl = existingParent.as(FunctionDeclSyntax.self), functionDecl.hasAttribute("Test") {
157+
if let functionDecl = existingParent.as(FunctionDeclSyntax.self),
158+
functionDecl.hasAttribute("Test", inModule: "Testing") {
158159
return true
159160
}
160161
parent = existingParent.parent

Sources/SwiftFormat/Core/WithAttributesSyntax+Convenience.swift

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@
1313
import SwiftSyntax
1414

1515
extension WithAttributesSyntax {
16-
/// Indicates whether the node has attribute with the given `name`.
17-
///
18-
/// - Parameter name: The name of the attribute to lookup.
19-
/// - Returns: True if the node has an attribute with the given `name`, otherwise false.
20-
func hasAttribute(_ name: String) -> Bool {
21-
attributes.contains { attribute in
22-
let attributeName = attribute.as(AttributeSyntax.self)?.attributeName
23-
return attributeName?.as(IdentifierTypeSyntax.self)?.name.text == name
24-
// support @Module.Attribute syntax as well
25-
|| attributeName?.as(MemberTypeSyntax.self)?.name.text == name
26-
}
16+
/// Indicates whether the node has attribute with the given `name`.
17+
///
18+
/// - Parameter name: The name of the attribute to lookup.
19+
/// - Parameter module: The module name to lookup the attribute in.
20+
/// - Returns: True if the node has an attribute with the given `name`, otherwise false.
21+
func hasAttribute(_ name: String, inModule module: String) -> Bool {
22+
attributes.contains { attribute in
23+
let attributeName = attribute.as(AttributeSyntax.self)?.attributeName
24+
if let identifier = attributeName?.as(IdentifierTypeSyntax.self) {
25+
return identifier.name.text == name
26+
}
27+
// support @Module.Attribute syntax as well
28+
if let memberType = attributeName?.as(MemberTypeSyntax.self) {
29+
return memberType.name.text == name
30+
&& memberType.baseType.as(IdentifierTypeSyntax.self)?.name.text == module
31+
}
32+
return false
2733
}
34+
}
2835
}

Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public final class AlwaysUseLowerCamelCase: SyntaxLintRule {
100100

101101
// We allow underscores in test names, because there's an existing convention of using
102102
// underscores to separate phrases in very detailed test names.
103-
let allowUnderscores = testCaseFuncs.contains(node) || node.hasAttribute("Test")
103+
let allowUnderscores = testCaseFuncs.contains(node) || node.hasAttribute("Test", inModule: "Testing")
104104

105105
diagnoseLowerCamelCaseViolations(
106106
node.name, allowUnderscores: allowUnderscores,

Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,13 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
223223
func 1️⃣function_Without_Test_Attribute() {}
224224
@objc
225225
func 2️⃣function_With_Non_Test_Attribute() {}
226+
@Foo.Test
227+
func 3️⃣function_With_Test_Attribute_From_Foo_Module() {}
226228
""",
227229
findings: [
228230
FindingSpec("1️⃣", message: "rename the function 'function_Without_Test_Attribute' using lowerCamelCase"),
229231
FindingSpec("2️⃣", message: "rename the function 'function_With_Non_Test_Attribute' using lowerCamelCase"),
232+
FindingSpec("3️⃣", message: "rename the function 'function_With_Test_Attribute_From_Foo_Module' using lowerCamelCase"),
230233
]
231234
)
232235
}

0 commit comments

Comments
 (0)