-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#2079 - Implemented the new sublclass rule. #2081
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// 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: "Subclassing is prohibited.", | ||
kind: .style | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should provide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would be probably |
||
) | ||
|
||
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()") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not the right way of doing this since you can have subclasses that don't call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marcelofabri What would be the right way in your opinion? The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO we should have a rule that triggers if you don't add There's not a lot of value (and in fact I'd argue that it's worst) to trigger a violation on cases where you don't have an alternative, usually dealing with dependencies (either system frameworks like UIKit or 3rd party ones). |
||
return [ | ||
StyleViolation( | ||
ruleDescription: type(of: self).description, | ||
severity: configuration.params.first!.severity, | ||
location: Location(file: file, byteOffset: offset), | ||
reason: "Subclassing is prohibited." | ||
) | ||
] | ||
} else { | ||
return [] | ||
} | ||
|
||
} | ||
|
||
} |
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) | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should use
SeverityConfiguration