-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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
82 changes: 82 additions & 0 deletions
82
Source/SwiftLintFramework/Rules/Idiomatic/UnavailableConditionRule.swift
This file contains 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,82 @@ | ||
import SourceKittenFramework | ||
import SwiftSyntax | ||
|
||
public struct UnavailableConditionRule: ConfigurationProviderRule, AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "unavailable_condition", | ||
name: "Unavailable Condition", | ||
description: "Use #unavailable instead of #available with an empty body.", | ||
kind: .idiomatic, | ||
minSwiftVersion: .fiveDotSix, | ||
nonTriggeringExamples: [ | ||
Example(""" | ||
if #unavailable(iOS 13) { | ||
loadMainWindow() | ||
} | ||
"""), | ||
Example(""" | ||
if #available(iOS 9.0, *) { | ||
doSomething() | ||
} else { | ||
legacyDoSomething() | ||
} | ||
""") | ||
], | ||
triggeringExamples: [ | ||
Example(""" | ||
if ↓#available(iOS 14.0) { | ||
} else { | ||
oldIos13TrackingLogic(isEnabled: ASIdentifierManager.shared().isAdvertisingTrackingEnabled) | ||
} | ||
"""), | ||
Example(""" | ||
if ↓#available(iOS 14.0) { | ||
// we don't need to do anything here | ||
} else { | ||
oldIos13TrackingLogic(isEnabled: ASIdentifierManager.shared().isAdvertisingTrackingEnabled) | ||
} | ||
"""), | ||
Example(""" | ||
if ↓#available(iOS 13, *) {} else { | ||
loadMainWindow() | ||
} | ||
""") | ||
] | ||
) | ||
|
||
public func validate(file: SwiftLintFile) -> [StyleViolation] { | ||
guard let tree = file.syntaxTree else { | ||
return [] | ||
} | ||
|
||
let visitor = UnavailableConditionRuleVisitor() | ||
visitor.walk(tree) | ||
return visitor.positions.map { position in | ||
StyleViolation(ruleDescription: Self.description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: ByteCount(position.utf8Offset))) | ||
} | ||
} | ||
} | ||
|
||
private final class UnavailableConditionRuleVisitor: SyntaxVisitor { | ||
private(set) var positions: [AbsolutePosition] = [] | ||
|
||
override func visitPost(_ node: IfStmtSyntax) { | ||
guard node.body.statements.withoutTrivia().isEmpty else { | ||
return | ||
} | ||
|
||
guard node.conditions.count == 1, let condition = node.conditions.first, | ||
let availability = condition.condition.as(AvailabilityConditionSyntax.self) else { | ||
return | ||
} | ||
|
||
positions.append(availability.positionAfterSkippingLeadingTrivia) | ||
} | ||
} |
This file contains 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