-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add ProhibitedSuperRule #971
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f54c9c4
Add ProhibitedSuperRule
3aed7c4
Consolidate guard statements
8fbb4c4
Continue consolidating guard statement
bfd2c37
simplify ProhibitedSuperRule.validateFile(...)
jpsim 80e3427
minor changes to ProhibitedSuperConfiguration
jpsim c47912d
clarify example method in changelog entry
jpsim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// ProhibitedSuperRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Aaron McTavish on 12/12/16. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
|
||
public struct ProhibitedSuperRule: ConfigurationProviderRule, ASTRule, OptInRule { | ||
public var configuration = ProhibitedSuperConfiguration() | ||
|
||
public init() { } | ||
|
||
public static let description = RuleDescription( | ||
identifier: "prohibited_super_call", | ||
name: "Prohibited calls to super", | ||
description: "Some methods should not call super", | ||
nonTriggeringExamples: [ | ||
"class VC: UIViewController {\n" + | ||
"\toverride func loadView() {\n" + | ||
"\t}\n" + | ||
"}\n", | ||
"class NSView {\n" + | ||
"\tfunc updateLayer() {\n" + | ||
"\t\tself.method1()" + | ||
"\t}\n" + | ||
"}\n" | ||
], | ||
triggeringExamples: [ | ||
"class VC: UIViewController {\n" + | ||
"\toverride func loadView() ↓{\n" + | ||
"\t\tsuper.loadView()\n" + | ||
"\t}\n" + | ||
"}\n", | ||
"class VC: NSFileProviderExtension {\n" + | ||
"\toverride func providePlaceholder(at url: URL," + | ||
"completionHandler: @escaping (Error?) -> Void) ↓{\n" + | ||
"\t\tself.method1()\n" + | ||
"\t\tsuper.providePlaceholder(at:url, completionHandler: completionHandler)\n" + | ||
"\t}\n" + | ||
"}\n", | ||
"class VC: NSView {\n" + | ||
"\toverride func updateLayer() ↓{\n" + | ||
"\t\tself.method1()\n" + | ||
"\t\tsuper.updateLayer()\n" + | ||
"\t\tself.method2()\n" + | ||
"\t}\n" + | ||
"}\n" | ||
] | ||
) | ||
|
||
public func validateFile(_ file: File, kind: SwiftDeclarationKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
guard let offset = dictionary["key.bodyoffset"] as? Int64, | ||
let name = dictionary["key.name"] as? String, | ||
let substructure = (dictionary["key.substructure"] as? [SourceKitRepresentable]), | ||
kind == .functionMethodInstance && | ||
configuration.resolvedMethodNames.contains(name) && | ||
dictionary.enclosedSwiftAttributes.contains("source.decl.attribute.override") && | ||
!extractCallsToSuper(name, substructure: substructure).isEmpty | ||
else { return [] } | ||
|
||
return [StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: Int(offset)), | ||
reason: "Method '\(name)' should not call to super function")] | ||
} | ||
|
||
private func extractCallsToSuper(_ name: String, | ||
substructure: [SourceKitRepresentable]) -> [String] { | ||
let superCall = "super.\(name)" | ||
return substructure.flatMap { | ||
guard let elems = $0 as? [String: SourceKitRepresentable], | ||
let type = elems["key.kind"] as? String, | ||
let name = elems["key.name"] as? String, | ||
type == "source.lang.swift.expr.call" && superCall.contains(name) | ||
else { return nil } | ||
return name | ||
} | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
Source/SwiftLintFramework/Rules/RuleConfigurations/ProhibitedSuperConfiguration.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,75 @@ | ||
// | ||
// ProhibitedSuperConfiguration.swift | ||
// SwiftLint | ||
// | ||
// Created by Aaron McTavish on 12/12/16. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct ProhibitedSuperConfiguration: RuleConfiguration, Equatable { | ||
var severityConfiguration = SeverityConfiguration(.warning) | ||
var excluded = [String]() | ||
var included = ["*"] | ||
|
||
private(set) var resolvedMethodNames = [ | ||
// NSFileProviderExtension | ||
"providePlaceholder(at:completionHandler:)", | ||
// NSTextInput | ||
"doCommand(by:)", | ||
// NSView | ||
"updateLayer()", | ||
// UIViewController | ||
"loadView()" | ||
] | ||
|
||
init() {} | ||
|
||
public var consoleDescription: String { | ||
return severityConfiguration.consoleDescription + | ||
", excluded: [\(excluded)]" + | ||
", included: [\(included)]" | ||
} | ||
|
||
public mutating func applyConfiguration(_ configuration: Any) throws { | ||
guard let configuration = configuration as? [String: Any] else { | ||
throw ConfigurationError.unknownConfiguration | ||
} | ||
|
||
if let severityString = configuration["severity"] as? String { | ||
try severityConfiguration.applyConfiguration(severityString) | ||
} | ||
|
||
if let excluded = [String].array(of: configuration["excluded"]) { | ||
self.excluded = excluded | ||
} | ||
|
||
if let included = [String].array(of: configuration["included"]) { | ||
self.included = included | ||
} | ||
|
||
resolvedMethodNames = calculateResolvedMethodNames() | ||
} | ||
|
||
public var severity: ViolationSeverity { | ||
return severityConfiguration.severity | ||
} | ||
|
||
private func calculateResolvedMethodNames() -> [String] { | ||
var names = [String]() | ||
if included.contains("*") && !excluded.contains("*") { | ||
names += resolvedMethodNames | ||
} | ||
names += included.filter { $0 != "*" } | ||
names = names.filter { !excluded.contains($0) } | ||
return names | ||
} | ||
} | ||
|
||
public func == (lhs: ProhibitedSuperConfiguration, | ||
rhs: ProhibitedSuperConfiguration) -> Bool { | ||
return lhs.excluded == rhs.excluded && | ||
lhs.included == rhs.included && | ||
lhs.severityConfiguration == rhs.severityConfiguration | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why should this be an opt-in rule? seems like it should be enabled by default...
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.
I had considered it, but had decided to make it opt-in as the rule is based on method signature and some of the signatures are not particularly unique. Without adding additional checks to see if the class is a descendent of the correct object, I was worried there would likely be some false positives. I thought that might be a good addition in a future PR.
But if you feel that those developers should either change the names of their methods or mark it as excluded in the configuration, I'm happy to make it opt-in. 😸
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.
Yeah actually, I wrote this before realizing that this rule didn't check for superclass inheritance to make sure that only the correct methods were being flagged, so opt-in is probably best. 👌