Skip to content

Commit

Permalink
Stop triggering no_magic_numbers rule on literals used in ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed Jan 20, 2024
1 parent 2b6e0ee commit 17fab3f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#1762](https://github.com/realm/SwiftLint/pull/1762)

* Stop triggering `no_magic_numbers` rule on literals used in range
expressions assigned to variables.
[SimplyDanny](https://github.com/SimplyDanny)
[#5430](https://github.com/realm/SwiftLint/pull/5430)

* Add `affect_initializers` option to allow `unneeded_override` rule
to affect initializers.
[leonardosrodrigues0](https://github.com/leonardosrodrigues0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import SwiftSyntax

@SwiftSyntaxRule
@SwiftSyntaxRule(foldExpressions: true)
struct NoMagicNumbersRule: OptInRule {
var configuration = NoMagicNumbersConfiguration()

Expand Down Expand Up @@ -75,6 +75,9 @@ struct NoMagicNumbersRule: OptInRule {
Example("let foo = 2 >> 2"),
Example("let foo = 2 << 2"),
Example("let a = b / 100.0"),
Example("let range = 2 ..< 12"),
Example("let range = ...12"),
Example("let range = 12..."),
Example("let (lowerBound, upperBound) = (400, 599)"),
Example("let a = (5, 10)"),
Example("""
Expand All @@ -87,6 +90,10 @@ struct NoMagicNumbersRule: OptInRule {
Example("array[↓42]"),
Example("let box = array[↓12 + ↓14]"),
Example("let a = b + ↓2.0"),
Example("let range = 2 ... ↓12 + 1"),
Example("let range = ↓2*↓6..."),
Example("let slice = array[↓2...↓4]"),
Example("for i in ↓3 ..< ↓8 {}"),
Example("Color.primary.opacity(isAnimate ? ↓0.1 : ↓1.5)"),
Example("""
class MyTest: XCTestCase {}
Expand Down Expand Up @@ -181,8 +188,24 @@ private extension TokenSyntax {
guard let grandparent = parent?.parent else {
return true
}
return !grandparent.is(InitializerClauseSyntax.self)
&& grandparent.as(PrefixOperatorExprSyntax.self)?.parent?.is(InitializerClauseSyntax.self) != true
if grandparent.is(InitializerClauseSyntax.self) {
return false
}
let operatorParent = grandparent.as(PrefixOperatorExprSyntax.self)?.parent
?? grandparent.as(PostfixOperatorExprSyntax.self)?.parent
?? grandparent.asAcceptedInfixOperator?.parent
return operatorParent?.is(InitializerClauseSyntax.self) != true
}
}

private extension Syntax {
var asAcceptedInfixOperator: InfixOperatorExprSyntax? {
if let infixOp = `as`(InfixOperatorExprSyntax.self),
let operatorSymbol = infixOp.operator.as(BinaryOperatorExprSyntax.self)?.operator.tokenKind,
[.binaryOperator("..."), .binaryOperator("..<")].contains(operatorSymbol) {
return infixOp
}
return nil
}
}

Expand Down Expand Up @@ -212,18 +235,11 @@ private extension ExprSyntaxProtocol {
}

func isOperandOfBitwiseShiftOperation() -> Bool {
guard
let siblings = parent?.as(ExprListSyntax.self)?.children(viewMode: .sourceAccurate),
siblings.count == 3
else {
return false
if let operation = parent?.as(InfixOperatorExprSyntax.self) {
return [
.binaryOperator("<<"), .binaryOperator(">>")
].contains(operation.operator.as(BinaryOperatorExprSyntax.self)?.operator.tokenKind)
}

let operatorIndex = siblings.index(after: siblings.startIndex)
if let tokenKind = siblings[operatorIndex].as(BinaryOperatorExprSyntax.self)?.operator.tokenKind {
return tokenKind == .binaryOperator("<<") || tokenKind == .binaryOperator(">>")
}

return false
}
}
Expand Down

0 comments on commit 17fab3f

Please sign in to comment.