-
Notifications
You must be signed in to change notification settings - Fork 253
Add checks for test functions marked with @Test attribute in relevant Rules #767
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
allevato
merged 7 commits into
swiftlang:main
from
hamtiko:allow-underscore-for-testing-funcs
Jul 19, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1c9d743
Add check to allow underscores in test functions marked with @Test at…
hamtiko 5a63aee
Add Swift Testing checks
hamtiko 77779ac
Move attribute lookup to separate extension
hamtiko dca8bec
Merge branch 'adding-swift-testing-checks' into allow-underscore-for-…
hamtiko c7d8018
Use new extension for attribute lookup
hamtiko 8019514
Update hasAttribute extension signature to check module name as well.
hamtiko d2d5dc3
updating the doc for hasAttribute
hamtiko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
Sources/SwiftFormat/Core/WithAttributesSyntax+Convenience.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| import SwiftSyntax | ||
|
|
||
| extension WithAttributesSyntax { | ||
| /// Indicates whether the node has attribute with the given `name`. | ||
| /// | ||
| /// - Parameter name: The name of the attribute to lookup. | ||
| /// - Returns: True if the node has an attribute with the given `name`, otherwise false. | ||
| func hasAttribute(_ name: String) -> Bool { | ||
| attributes.contains { attribute in | ||
| let attributeName = attribute.as(AttributeSyntax.self)?.attributeName | ||
| return attributeName?.as(IdentifierTypeSyntax.self)?.name.text == name | ||
| // support @Module.Attribute syntax as well | ||
| || attributeName?.as(MemberTypeSyntax.self)?.name.text == name | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you're not checking the module name here, this would match anything, right? Like
@Foo.Test, for example.It might be better to make the function signature
hasAttribute(_ name: String, inModule: String)and have the caller always provide it, but it would only be checked if it was aMemberTypeSyntax. SohasAttribute("Test", inModule: "Testing")would still match either@Testor@Testing.Test, but not@Foo.Test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What you think if we will have 2 functions, one will be the current one and another function like you suggested?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better for simplicity to have one function that does both checks. We can't definitively know if
@Testrefers to@Testing.Testwithout the file being type-checked, which we can't do in swift-format. So we're making an assumption that@Testby itself refers to the one inTestingand nothing else, because of how common we expect that pattern to be. If we're handling that as the common case, then there's no reason to not also handle@Testing.Testwith the same query—each of the individual rules shouldn't have to make two calls to find the attribute it's looking for.