-
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
Changes for issue #598 to make line length rule configurable #1264
Changes for issue #598 to make line length rule configurable #1264
Conversation
…an options to LineLengthRule
Generated by 🚫 danger |
@@ -8,34 +8,62 @@ | |||
|
|||
import Foundation | |||
|
|||
public enum LineLengthConfigurationFlag { |
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.
Should this be an OptionSet?
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.
Good suggestion. Changed it and updated the PR.
aeaa2e6
to
3f24204
Compare
…er pull request feedback
It looks like the travis-ci job hung while checking the PR. There's no output I found indicating why the build could not be completed. I'll try and trigger it again with a dummy commit. Is there a better way? |
I'll trigger it manually |
…I build -- it hung and errored after my last push
@@ -17,17 +17,21 @@ extension Structure { | |||
/// - Parameter byteOffset: Int | |||
// swiftlint:disable:next valid_docs | |||
internal func kinds(forByteOffset byteOffset: Int) -> [(kind: String, byteRange: NSRange)] { | |||
var results = [(kind: String, byteRange: NSRange)]() | |||
return kinds().filter { |
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 expect this to be slower than the previous implementation because now we keep navigating in a substructure even if the top structure doesn't contain the range. Just a thing to keep in mind (i.e. we need to run oss-check locally to get reliable results about this one)
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. I'll modify to have it all on a single pass.
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.
Thanks for implementing these!
I have added some comments. They're all small issues, but in general this looks great 🎉
@@ -10,7 +10,7 @@ import Foundation | |||
import SourceKittenFramework | |||
|
|||
public struct LineLengthRule: ConfigurationProviderRule, SourceKitFreeRule { |
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.
this shouldn't conform to SourceKitFreeRule
anymore
|
||
var params: [RuleParameter<Int>] { | ||
return length.params | ||
} | ||
|
||
public init(warning: Int, error: Int?, ignoresURLs: Bool) { | ||
public init(warning: Int, error: Int?, options: LineLengthRuleOptions? = []) { |
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.
if we have a default value, I'd say it shouldn't be optional
@@ -31,15 +31,37 @@ public struct LineLengthRule: ConfigurationProviderRule, SourceKitFreeRule { | |||
) | |||
|
|||
public func validate(file: File) -> [StyleViolation] { | |||
let minValue = configuration.params.map({ $0.value }).min(by: <) | |||
let minValue = configuration.params.map({ $0.value }).min(by: <) ?? Int.max |
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 know this was already this way, but you can just use min()
instead of min(by: <)
} | ||
|
||
if configuration.ignoresComments && | ||
line.index < syntaxKindsByLine.count { |
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.
this is almost identical to the implementation of the if before. What do you think about extracting to a private helper function?
Also, we probably should create a Set
for SyntaxKind.commentKinds()
and one for SwiftDeclarationKind.functionKinds()
in private properties so we can get faster lookups (in theory anyway).
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.
Done, updating PR
public init(rawValue: Int) { self.rawValue = rawValue } | ||
public init() { self.rawValue = 0 } | ||
|
||
static let ignoreUrls = LineLengthRuleOptions(rawValue: 1 << 0) |
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.
these need to be public
as well
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.
done, updating PR
@@ -12,10 +12,47 @@ import XCTest | |||
|
|||
class LineLengthRuleTests: XCTestCase { | |||
|
|||
let longFunctionDeclaration = "public func superDuperLongFunctionDeclaration(a: String, b: String, " + |
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.
these variables should be private
func testLineLength() { | ||
verifyRule(LineLengthRule.description, commentDoesntViolate: false, stringDoesntViolate: false) | ||
} | ||
|
||
func testLineLengthWithIgnoreFunctionDeclaraionsEnabled() { |
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.
typo in testLineLengthWithIgnoreFunctionDeclaraionsEnabled
var maybeStructure = structureIterator.next() | ||
while let line = maybeLine, let structure = maybeStructure { | ||
if NSLocationInRange(structure.byteRange.location, line.byteRange) || | ||
NSLocationInRange(line.byteRange.location, structure.byteRange) { |
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.
do you really need these two NSLocationInRange
checks? It seems redundant for me.
CHANGELOG.md
Outdated
@@ -23,6 +23,12 @@ | |||
[Marcelo Fabri](https://github.com/marcelofabri) | |||
[#1061](https://github.com/realm/SwiftLint/issues/1061) | |||
|
|||
* Add 'ignores_function_declarations' and 'ignores_comments' as options | |||
to LineLengthRule. |
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.
missing two trailing spaces after .
. Also, please scape ignores_function_declarations
in backticks (`)
CHANGELOG.md
Outdated
@@ -1705,7 +1711,7 @@ This release has seen a phenomenal uptake in community contributions! | |||
* The following rules now conform to `ASTRule`: | |||
FunctionBodyLength, Nesting, TypeBodyLength, TypeName, VariableName. | |||
[JP Simard](https://github.com/jpsim) | |||
|
|||
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.
unintentional change?
let lineCommentKinds = syntaxKindsByLine[line.index].filter { | ||
return SyntaxKind.commentKinds().contains($0) | ||
} | ||
if !lineCommentKinds.isEmpty { |
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.
Actually, I don't know if this is the expected behavior. I'd expect that the line would be ignored if it only had comments.
A long line with a small comment in the end should trigger IMO.
… now trigger, configuration will now fail if invalud value types are set for options
filter() setup of the results
Small style changes after #1264
refactor and fix a few things after #1264
How do you use this option? any exampes, I would really appreciate it |
@lukasburns1 try
However I believe this option is only available on |
Is this option now available in release? Tried them out but doesn't seem to work... |
Hi, I took a pass at adding support for new new options on LineLengthRule to allow support for it to not trigger on long function declarations or long comments (or both): 'ignores_function_declarations' and 'ignores_comments', respectively.