Skip to content

Commit

Permalink
Exclude @Suite types and @Test functions from no_magic_numbers
Browse files Browse the repository at this point in the history
…analysis
  • Loading branch information
SimplyDanny committed Jan 22, 2025
1 parent 854d3f2 commit 6782c05
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
"""),
],
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6782c05

Please sign in to comment.