Skip to content

Commit c248cf7

Browse files
committed
Extra Rules
1 parent 124b79f commit c248cf7

File tree

6 files changed

+56
-4
lines changed

6 files changed

+56
-4
lines changed

WORKSPACE

+8-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ swift_rules_extra_dependencies()
2929

3030
http_archive(
3131
name = "SwiftLint",
32-
sha256 = "574c43473643203be6691173f771899656a0928c875e2f547aab17bebb5403f5",
33-
strip_prefix = "SwiftLint-949ab6289a6e9d52b84c8cc43817703c3bf68f77",
34-
url = "https://github.com/realm/SwiftLint/archive/949ab6289a6e9d52b84c8cc43817703c3bf68f77.tar.gz",
32+
sha256 = "7dcb520cd80724584126aeb89bc7db55cb35267aa668613f28084202a2d03ede",
33+
strip_prefix = "SwiftLint-1dab8181b8597c493463dbab48aa7380dcd99123",
34+
url = "https://github.com/realm/SwiftLint/archive/1dab8181b8597c493463dbab48aa7380dcd99123.tar.gz",
35+
)
36+
37+
local_repository(
38+
name = "swiftlint_extra_rules",
39+
path = "swiftlint_extra_rules",
3540
)
3641

3742
load("@SwiftLint//bazel:repos.bzl", "swiftlint_repos")

file.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
let a : Int = 0
1+
let forbidden : Int = 1

swiftlint_extra_rules/BUILD

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
filegroup(
2+
name = "extra_rules",
3+
srcs = glob(["**/*.swift"]),
4+
visibility = ["//visibility:public"],
5+
)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
func extraRules() -> [Rule.Type] {
2+
[
3+
ForbiddenVarRule.self
4+
]
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import SourceKittenFramework
2+
import SwiftSyntax
3+
4+
public struct ForbiddenVarRule: ConfigurationProviderRule, AutomaticTestableRule, SourceKitFreeRule {
5+
public var configuration = SeverityConfiguration(.warning)
6+
7+
public init() {}
8+
9+
public static let description = RuleDescription(
10+
identifier: "forbidden_var",
11+
name: "Forbidden Var",
12+
description: "Can't name a variable 'forbidden'",
13+
kind: .style,
14+
nonTriggeringExamples: [Example("let okay = 0")],
15+
triggeringExamples: [Example("let ↓forbidden = 0")]
16+
)
17+
18+
public func validate(file: SwiftLintFile) -> [StyleViolation] {
19+
ForbiddenVarRuleVisitor()
20+
.walk(file: file, handler: \.positions)
21+
.map { position in
22+
StyleViolation(ruleDescription: Self.description,
23+
severity: configuration.severity,
24+
location: Location(file: file, byteOffset: ByteCount(position)))
25+
}
26+
}
27+
}
28+
29+
private final class ForbiddenVarRuleVisitor: SyntaxVisitor {
30+
var positions: [AbsolutePosition] = []
31+
32+
override func visitPost(_ node: IdentifierPatternSyntax) {
33+
if node.identifier.text == "forbidden" {
34+
positions.append(node.positionAfterSkippingLeadingTrivia)
35+
}
36+
}
37+
}

swiftlint_extra_rules/WORKSPACE

Whitespace-only changes.

0 commit comments

Comments
 (0)