Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

#### Enhancements

* Adds `subclass` opt-in rule to prohibit
subclassing.
[Mikhail Yakushin](https://github.com/driver733)
[#2079](https://github.com/realm/SwiftLint/issues/2079)

* Add a new `excluded` config parameter to the `explicit_type_interface` rule
to exempt certain types of variables from the rule.
[Rounak Jain](https://github.com/rounak)
Expand Down
11 changes: 11 additions & 0 deletions Rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
* [Sorted Imports](#sorted-imports)
* [Statement Position](#statement-position)
* [Strict fileprivate](#strict-fileprivate)
* [Subclass](#subclass)
* [Superfluous Disable Command](#superfluous-disable-command)
* [Switch and Case Statement Alignment](#switch-and-case-statement-alignment)
* [Switch Case on Newline](#switch-case-on-newline)
Expand Down Expand Up @@ -12119,6 +12120,16 @@ struct Inter {



## Subclass

Identifier | Enabled by default | Supports autocorrection | Kind
--- | --- | --- | ---
`subclass` | Disabled | No | style

Subclassing is prohibited.



## Superfluous Disable Command

Identifier | Enabled by default | Supports autocorrection | Kind
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/MasterRuleList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public let masterRuleList = RuleList(rules: [
SortedImportsRule.self,
StatementPositionRule.self,
StrictFilePrivateRule.self,
SubClassRule.self,
SuperfluousDisableCommandRule.self,
SwitchCaseAlignmentRule.self,
SwitchCaseOnNewlineRule.self,
Expand Down
47 changes: 47 additions & 0 deletions Source/SwiftLintFramework/Rules/SubClassRule.swift
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should use SeverityConfiguration


public init() {}

public static let description = RuleDescription(
identifier: "subclass",
name: "Subclass",
description: "Subclassing is prohibited.",
kind: .style
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you should provide triggeringExamples and nonTriggeringExamples

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this would be probably .idiomatic kind and not .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()") {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 super

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@marcelofabri What would be the right way in your opinion? The SourceKit does not distinguish the inheritance between the interfaces and superclasses. It only shows that the class inherits from something.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 final to a class, as @SDGGiesbrecht suggested.

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 []
}

}

}
8 changes: 8 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@
B89F3BCF1FD5EE1400931E59 /* RequiredEnumCaseRuleConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = B89F3BC71FD5ED7D00931E59 /* RequiredEnumCaseRuleConfiguration.swift */; };
BB00B4E91F5216090079869F /* MultipleClosuresWithTrailingClosureRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB00B4E71F5216070079869F /* MultipleClosuresWithTrailingClosureRule.swift */; };
BFF028AE1CBCF8A500B38A9D /* TrailingWhitespaceConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF48D2D61CBCCA5F0080BDAE /* TrailingWhitespaceConfiguration.swift */; };
C2E0926C065C57CD27EAACC7 /* SubClassRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2E0932F4A37171BCB5D5D27 /* SubClassRule.swift */; };
C2E09BB35101215D85319786 /* SubClassRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2E09BACD1C291F22F7A49C1 /* SubClassRuleTests.swift */; };
C328A2F71E6759AE00A9E4D7 /* ExplicitTypeInterfaceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C328A2F51E67595500A9E4D7 /* ExplicitTypeInterfaceRule.swift */; };
C3DE5DAC1E7DF9CA00761483 /* FatalErrorMessageRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C3DE5DAA1E7DF99B00761483 /* FatalErrorMessageRule.swift */; };
C946FECB1EAE67EE007DD778 /* LetVarWhitespaceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = C946FEC91EAE5E20007DD778 /* LetVarWhitespaceRule.swift */; };
Expand Down Expand Up @@ -492,6 +494,8 @@
B89F3BCB1FD5EDA900931E59 /* RequiredEnumCaseRuleTestCase.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RequiredEnumCaseRuleTestCase.swift; sourceTree = "<group>"; };
BB00B4E71F5216070079869F /* MultipleClosuresWithTrailingClosureRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MultipleClosuresWithTrailingClosureRule.swift; sourceTree = "<group>"; };
BF48D2D61CBCCA5F0080BDAE /* TrailingWhitespaceConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TrailingWhitespaceConfiguration.swift; sourceTree = "<group>"; };
C2E0932F4A37171BCB5D5D27 /* SubClassRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubClassRule.swift; sourceTree = "<group>"; };
C2E09BACD1C291F22F7A49C1 /* SubClassRuleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SubClassRuleTests.swift; sourceTree = "<group>"; };
C328A2F51E67595500A9E4D7 /* ExplicitTypeInterfaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExplicitTypeInterfaceRule.swift; sourceTree = "<group>"; };
C3DE5DAA1E7DF99B00761483 /* FatalErrorMessageRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FatalErrorMessageRule.swift; sourceTree = "<group>"; };
C946FEC91EAE5E20007DD778 /* LetVarWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LetVarWhitespaceRule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -971,6 +975,7 @@
D0D1212219E878CC005E4BAA /* Configuration */,
3B12C9BE1C3209AC000B423F /* Resources */,
D0D1217C19E87B05005E4BAA /* Supporting Files */,
C2E09BACD1C291F22F7A49C1 /* SubClassRuleTests.swift */,
);
name = SwiftLintFrameworkTests;
path = Tests/SwiftLintFrameworkTests;
Expand Down Expand Up @@ -1162,6 +1167,7 @@
094384FF1D5D2382009168CF /* WeakDelegateRule.swift */,
626D02961F31CBCC0054788D /* XCTFailMessageRule.swift */,
1872906F1FC37A9B0016BEA2 /* YodaConditionRule.swift */,
C2E0932F4A37171BCB5D5D27 /* SubClassRule.swift */,
);
path = Rules;
sourceTree = "<group>";
Expand Down Expand Up @@ -1702,6 +1708,7 @@
A73469421FB121BA009B57C7 /* CallPairRule.swift in Sources */,
D47079AF1DFE520000027086 /* VoidReturnRule.swift in Sources */,
B3935EE74B1E8E14FBD65E7F /* String+XML.swift in Sources */,
C2E0926C065C57CD27EAACC7 /* SubClassRule.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -1755,6 +1762,7 @@
3B63D46F1E1F09DF0057BE35 /* LineLengthRuleTests.swift in Sources */,
3BCC04D41C502BAB006073C3 /* RuleConfigurationTests.swift in Sources */,
E809EDA31B8A73FB00399043 /* ConfigurationTests.swift in Sources */,
C2E09BB35101215D85319786 /* SubClassRuleTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 8 additions & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,14 @@ extension RulesTests {
]
}

extension SubClassRuleTests {
static var allTests: [(String, (SubClassRuleTests) -> () throws -> Void)] = [
("testSubClassRuleSuperDot", testSubClassRuleSuperDot),
("testSubClassRuleSuper", testSubClassRuleSuper),
("testSubClassRuleTestsValid", testSubClassRuleTestsValid)
]
}

extension SourceKitCrashTests {
static var allTests: [(String, (SourceKitCrashTests) -> () throws -> Void)] = [
("testAssertHandlerIsNotCalledOnNormalFile", testAssertHandlerIsNotCalledOnNormalFile),
Expand Down
43 changes: 43 additions & 0 deletions Tests/SwiftLintFrameworkTests/SubClassRuleTests.swift
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)
}

}