mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
8d9c501cb8
* Add optional return_value_from_void_function rule using SwiftSyntax * Use Script/bootstrap in CI * Make SwiftSyntax optional * Make SwiftSyntax optional in SPM * Fix Package.swift * Try again * Add minSwiftVersion * Fix thread sanitizer issue * Take 2 * Fix false positive on nested computed variables * Remove support for Xcode 10.x * Fix rule description * Enable opt-in rule in configuration file * Extract code into `SyntaxRule` protocol * nit: make property private * Missing docs * Fix MasterRuleList.swift * Update CHANGELOG * Remove unused imports * Use Example type * Change rule kind to .idiomatic * Update CHANGELOG * Bump deployment target to macOS 10.12 * Simplify SyntaxRule.validate(file:visitor) * Make TSan happy * Use script/bootstrap in the README
45 lines
1.4 KiB
Swift
45 lines
1.4 KiB
Swift
import Foundation
|
|
#if canImport(SwiftSyntax)
|
|
import SwiftSyntax
|
|
#endif
|
|
|
|
/// A rule that leverages the SwiftSyntax library.
|
|
public protocol SyntaxRule: Rule {}
|
|
|
|
#if canImport(SwiftSyntax)
|
|
/// A SwiftSyntax visitor that collects data to provide violations for a specific rule.
|
|
public protocol SyntaxRuleVisitor: SyntaxVisitor {
|
|
/// The rule that uses this visitor.
|
|
associatedtype Rule: SyntaxRule
|
|
|
|
/// Returns the violations that should be calculated based on data that was accumulated during the `visit` methods.
|
|
func violations(for rule: Rule, in file: SwiftLintFile) -> [StyleViolation]
|
|
}
|
|
|
|
public extension SyntaxRule {
|
|
/// Wraps computation of violations based on a visitor.
|
|
func validate<Visitor: SyntaxRuleVisitor>(file: SwiftLintFile,
|
|
visitor: Visitor) -> [StyleViolation] where Visitor.Rule == Self {
|
|
let lock = NSLock()
|
|
var visitor = visitor
|
|
|
|
// https://bugs.swift.org/browse/SR-11170
|
|
let work = DispatchWorkItem {
|
|
lock.lock()
|
|
file.syntax.walk(&visitor)
|
|
lock.unlock()
|
|
}
|
|
let thread = Thread {
|
|
work.perform()
|
|
}
|
|
thread.stackSize = 8 << 20 // 8 MB.
|
|
thread.start()
|
|
work.wait()
|
|
|
|
lock.lock()
|
|
defer { lock.unlock() }
|
|
return visitor.violations(for: self, in: file)
|
|
}
|
|
}
|
|
#endif
|