Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds opt-in prefixed_toplevel_constant rule
Implements #1907.
- Loading branch information
1 parent
34d2fb3
commit 1295c5d
Showing
7 changed files
with
157 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
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
62 changes: 62 additions & 0 deletions
62
Source/SwiftLintFramework/Rules/PrefixedTopLevelConstantRule.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,62 @@ | ||
// | ||
// PrefixedConstantRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Ornithologist Coder on 1/5/18. | ||
// Copyright © 2018 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct PrefixedTopLevelConstantRule: ASTRule, OptInRule, ConfigurationProviderRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "prefixed_toplevel_constant", | ||
name: "Prefixed Top-Level Constant", | ||
description: "Top-level constants should be prefixed by `k`.", | ||
kind: .style, | ||
nonTriggeringExamples: [ | ||
"private let kFoo = 20.0", | ||
"public let kFoo = false", | ||
"internal let kFoo = \"Foo\"", | ||
"let kFoo = true", | ||
"struct Foo {\n" + | ||
" let bar = 20.0\n" + | ||
"}", | ||
"private var foo = 20.0", | ||
"public var foo = false", | ||
"internal var foo = \"Foo\"", | ||
"var foo = true" | ||
], | ||
triggeringExamples: [ | ||
"private let ↓Foo = 20.0", | ||
"public let ↓Foo = false", | ||
"internal let ↓Foo = \"Foo\"", | ||
"let ↓Foo = true", | ||
"let ↓foo = 2, ↓bar = true" | ||
] | ||
) | ||
|
||
public func validate(file: File, | ||
kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard | ||
kind == .varGlobal, | ||
let offset = dictionary.offset, | ||
let nameOffset = dictionary.nameOffset, | ||
let name = dictionary.name, | ||
let content = file.contents.bridge().substringWithByteRange(start: offset, length: nameOffset - offset), | ||
content.hasPrefix("let") && !name.hasPrefix("k") | ||
else { | ||
return [] | ||
} | ||
|
||
return [StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: nameOffset))] | ||
} | ||
} |
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
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