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

Changes for issue #598 to make line length rule configurable #1264

Merged
merged 9 commits into from
Feb 6, 2017

Conversation

mlwelles
Copy link
Contributor

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.

@SwiftLintBot
Copy link

SwiftLintBot commented Jan 30, 2017

12 Messages
📖 Linting WordPress-iOS with this PR took 14.46s vs 15.91s on master (9% faster)
📖 Linting swift with this PR took 13.79s vs 13.37s on master (3% slower)
📖 Linting Aerial with this PR took 0.5s vs 0.5s on master (0% slower)
📖 Linting SourceKitten with this PR took 1.4s vs 1.52s on master (7% faster)
📖 Linting Sourcery with this PR took 2.8s vs 2.87s on master (2% faster)
📖 Linting ios-oss with this PR took 19.67s vs 19.37s on master (1% slower)
📖 Linting Alamofire with this PR took 3.8s vs 3.72s on master (2% slower)
📖 Linting firefox-ios with this PR took 21.91s vs 21.75s on master (0% slower)
📖 Linting Nimble with this PR took 2.17s vs 2.26s on master (3% faster)
📖 Linting Quick with this PR took 0.66s vs 0.67s on master (1% faster)
📖 Linting realm-cocoa with this PR took 3.46s vs 3.44s on master (0% slower)
📖 Linting Moya with this PR took 0.55s vs 0.53s on master (3% slower)

Generated by 🚫 danger

@@ -8,34 +8,62 @@

import Foundation

public enum LineLengthConfigurationFlag {
Copy link

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?

Copy link
Contributor Author

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.

@mlwelles mlwelles force-pushed the line-length-rule-configurable branch from aeaa2e6 to 3f24204 Compare January 31, 2017 18:23
@mlwelles
Copy link
Contributor Author

mlwelles commented Feb 1, 2017

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?

@marcelofabri
Copy link
Collaborator

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 {
Copy link
Collaborator

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)

Copy link
Contributor Author

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.

Copy link
Collaborator

@marcelofabri marcelofabri left a 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 {
Copy link
Collaborator

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? = []) {
Copy link
Collaborator

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
Copy link
Collaborator

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 {
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 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).

Copy link
Contributor Author

@mlwelles mlwelles Feb 6, 2017

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)
Copy link
Collaborator

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

Copy link
Contributor Author

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, " +
Copy link
Collaborator

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() {
Copy link
Collaborator

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) {
Copy link
Collaborator

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.
Copy link
Collaborator

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)

Copy link
Collaborator

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 {
Copy link
Collaborator

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
@marcelofabri marcelofabri merged commit 56bbf12 into realm:master Feb 6, 2017
marcelofabri added a commit that referenced this pull request Feb 7, 2017
@mlwelles mlwelles deleted the line-length-rule-configurable branch February 8, 2017 15:23
jpsim added a commit that referenced this pull request Feb 9, 2017
jpsim added a commit that referenced this pull request Feb 9, 2017
refactor and fix a few things after #1264
@lukasburns
Copy link

How do you use this option? any exampes, I would really appreciate it
thx!

@sammy-SC
Copy link
Contributor

sammy-SC commented Mar 2, 2017

@lukasburns1 try

line_length:
  warning: 120
  ignores_function_declarations: true
  ignores_comments: true

However I believe this option is only available on Master and not in current release.

@eomiso
Copy link

eomiso commented Jan 11, 2022

Is this option now available in release? Tried them out but doesn't seem to work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants