Skip to content
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

Exclude @Suite types and @Test functions from no_magic_numbers analysis #5968

Merged
merged 1 commit into from
Feb 12, 2025
Merged
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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -12,7 +12,10 @@

### Enhancements

* None.
* Exclude types with a `@Suite` attribute and functions annotated with `@Test` from `no_magic_numbers` rule.
Also treat a type as a `@Suite` if it contains `@Test` functions.
[SimplyDanny](https://github.com/SimplyDanny)
[#5964](https://github.com/realm/SwiftLint/issues/5964)

### Bug Fixes

Original file line number Diff line number Diff line change
@@ -81,9 +81,26 @@ struct NoMagicNumbersRule: Rule {
Example("let (lowerBound, upperBound) = (400, 599)"),
Example("let a = (5, 10)"),
Example("let notFound = (statusCode: 404, description: \"Not Found\", isError: true)"),
Example("#Preview { ContentView(value: 5) }"),
Example("@Test func f() { let _ = 2 + 2 }"),
Example("""
#Preview {
ContentView(value: 5)
@Suite struct Test {
@Test func f() {
func g() { let _ = 2 + 2 }
let _ = 2 + 2
}
}
"""),
Example("""
@Suite actor Test {
private var a: Int { 2 }
@Test func f() { let _ = 2 + a }
}
"""),
Example("""
class Test { // @Suite implicitly
private var a: Int { 2 }
@Test func f() { let _ = 2 + a }
}
"""),
],
@@ -127,12 +144,12 @@ private extension NoMagicNumbersRule {
private var nonTestClasses: Set<String> = []
private var possibleViolations: [String: Set<AbsolutePosition>] = [:]

override func visit(_ node: PatternBindingSyntax) -> SyntaxVisitorContinueKind {
node.isSimpleTupleAssignment ? .skipChildren : .visitChildren
override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
node.isTestSuite ? .skipChildren : .visitChildren
}

override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind {
node.macroName.text == "Preview" ? .skipChildren : .visitChildren
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
node.isTestSuite ? .skipChildren : .visitChildren
}

override func visitPost(_ node: ClassDeclSyntax) {
@@ -145,20 +162,40 @@ private extension NoMagicNumbersRule {
}
}

override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
node.isTestSuite ? .skipChildren : .visitChildren
}

override func visitPost(_ node: FloatLiteralExprSyntax) {
guard node.literal.isMagicNumber else {
return
}
collectViolation(forNode: node)
}

override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
node.attributes.contains(attributeNamed: "Test") ? .skipChildren : .visitChildren
}

override func visitPost(_ node: IntegerLiteralExprSyntax) {
guard node.literal.isMagicNumber else {
return
}
collectViolation(forNode: node)
}

override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind {
node.macroName.text == "Preview" ? .skipChildren : .visitChildren
}

override func visit(_ node: PatternBindingSyntax) -> SyntaxVisitorContinueKind {
node.isSimpleTupleAssignment ? .skipChildren : .visitChildren
}

override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
node.isTestSuite ? .skipChildren : .visitChildren
}

private func collectViolation(forNode node: some ExprSyntaxProtocol) {
if node.isMemberOfATestClass(configuration.testParentClasses) {
return
@@ -190,6 +227,17 @@ private extension NoMagicNumbersRule {
}
}

private extension DeclGroupSyntax {
var isTestSuite: Bool {
if attributes.contains(attributeNamed: "Suite") {
return true
}
return memberBlock.members.contains {
$0.decl.as(FunctionDeclSyntax.self)?.attributes.contains(attributeNamed: "Test") == true
}
}
}

private extension TokenSyntax {
var isMagicNumber: Bool {
guard let number = Double(text.replacingOccurrences(of: "_", with: "")) else {