Skip to content

Commit 77779ac

Browse files
committed
Move attribute lookup to separate extension
1 parent 1c9d743 commit 77779ac

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

Sources/SwiftFormat/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_library(SwiftFormat
4242
Core/SyntaxLintRule.swift
4343
Core/SyntaxProtocol+Convenience.swift
4444
Core/Trivia+Convenience.swift
45+
Core/WithAttributesSyntax+Convenience.swift
4546
Core/WithSemicolonSyntax.swift
4647
PrettyPrint/Comment.swift
4748
PrettyPrint/Indent+Length.swift
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
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+
}
27+
}
28+
}

Sources/SwiftFormat/Rules/AlwaysUseLowerCamelCase.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +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.attributes.contains {
104-
// Allow underscore for test functions with the `@Test` attribute.
105-
$0.as(AttributeSyntax.self)?.attributeName.as(IdentifierTypeSyntax.self)?.name.text == "Test"
106-
}
103+
let allowUnderscores = testCaseFuncs.contains(node) || node.hasAttribute("Test")
107104

108105
diagnoseLowerCamelCaseViolations(
109106
node.name, allowUnderscores: allowUnderscores,

Tests/SwiftFormatTests/Rules/AlwaysUseLowerCamelCaseTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ final class AlwaysUseLowerCamelCaseTests: LintOrFormatRuleTestCase {
217217
"""
218218
@Test
219219
func function_With_Test_Attribute() {}
220-
@Test("Description for test functions",
220+
@Testing.Test("Description for test functions",
221221
.tags(.testTag))
222222
func function_With_Test_Attribute_And_Args() {}
223223
func 1️⃣function_Without_Test_Attribute() {}

0 commit comments

Comments
 (0)