forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
realm#2079 - Implemented subclass rule
- Loading branch information
driver733
committed
Mar 2, 2018
1 parent
7e79790
commit 39d2892
Showing
7 changed files
with
124 additions
and
2 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
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,46 @@ | ||
// | ||
// SubClassRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Mikhail Yakushin on 03/02/18. | ||
// Copyright © 2018 Realm. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
|
||
public struct SubClassRule: ASTRule, ConfigurationProviderRule, OptInRule { | ||
public var configuration = SeverityLevelsConfiguration(warning: 0, error: 0) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "subclass", | ||
name: "Subclass", | ||
description: "Subclasses are prohibited.", | ||
kind: .style | ||
) | ||
|
||
public func validate(file: File, kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard SwiftDeclarationKind.functionKinds.contains(kind), | ||
let offset = dictionary.offset, | ||
case let contentsNSString = file.contents.bridge() | ||
else { | ||
return [] | ||
} | ||
if contentsNSString.contains("super.") || contentsNSString.contains("super()") { | ||
return [ | ||
StyleViolation( | ||
ruleDescription: type(of: self).description, | ||
severity: configuration.params.first!.severity, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: "Subclasses are prohibited." | ||
) | ||
] | ||
} else { | ||
return [] | ||
} | ||
|
||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// SubClassRuleTests.swift | ||
// SwiftLint | ||
// | ||
// Created by Mikhail Yakushin on 03/02/18. | ||
// Copyright © 2018 Realm. All rights reserved. | ||
// | ||
|
||
import SwiftLintFramework | ||
import XCTest | ||
|
||
class SubClassRuleTests: XCTestCase { | ||
|
||
func testSubClassRuleSuperDot() { | ||
let nonTriggeringExamples = | ||
"class MyType: SuperType {" + | ||
"public init() { super.init() }" + | ||
"}" | ||
XCTAssertNotEqual(violations(nonTriggeringExamples), []) | ||
} | ||
|
||
func testSubClassRuleSuper() { | ||
let nonTriggeringExamples = | ||
"class MyType: SuperType {" + | ||
"public init() { super() }" + | ||
"}" | ||
XCTAssertNotEqual(violations(nonTriggeringExamples), []) | ||
} | ||
|
||
func testSubClassRuleTestsValid() { | ||
let nonTriggeringExamples = | ||
"class MyType: SuperType {" + | ||
"public init() { this() }" + | ||
"}" | ||
XCTAssertEqual(violations(nonTriggeringExamples), []) | ||
} | ||
|
||
private func violations(_ string: String) -> [StyleViolation] { | ||
let config = makeConfig(nil, SubClassRule.description.identifier)! | ||
return SwiftLintFrameworkTests.violations(string, config: config) | ||
} | ||
|
||
} |