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

Cyclomatic complexity ignores case statments #1299

Merged

Conversation

mlwelles
Copy link
Contributor

@mlwelles mlwelles commented Feb 9, 2017

Per #1298

I've had a couple of instances where pull requests from my team have include dispatch functions that handle every case in an enum trigger cyclomatic complexity warnings / errors. We've all looked over the code, and agree we like it.

So that we don't trigger with examples we made some changes to have the rule optionally disregard case statements in scoring. I've got a PR ready, will submit if there's any interest.

Is there?

@SwiftLintBot
Copy link

SwiftLintBot commented Feb 9, 2017

12 Messages
📖 Linting WordPress-iOS with this PR took 18.71s vs 18.16s on master (3% slower)
📖 Linting swift with this PR took 18.1s vs 15.25s on master (18% slower)
📖 Linting Aerial with this PR took 0.75s vs 0.54s on master (38% slower)
📖 Linting SourceKitten with this PR took 2.2s vs 1.64s on master (34% slower)
📖 Linting Sourcery with this PR took 4.67s vs 3.59s on master (30% slower)
📖 Linting ios-oss with this PR took 26.38s vs 23.31s on master (13% slower)
📖 Linting Alamofire with this PR took 4.66s vs 4.99s on master (6% faster)
📖 Linting firefox-ios with this PR took 25.99s vs 26.17s on master (0% faster)
📖 Linting Nimble with this PR took 2.66s vs 2.7s on master (1% faster)
📖 Linting Quick with this PR took 0.84s vs 0.79s on master (6% slower)
📖 Linting realm-cocoa with this PR took 4.17s vs 4.21s on master (0% faster)
📖 Linting Moya with this PR took 0.62s vs 0.62s on master (0% slower)

Generated by 🚫 danger

@codecov-io
Copy link

codecov-io commented Feb 10, 2017

Codecov Report

Merging #1299 into master will decrease coverage by -0.01%.
The diff coverage is 81.98%.

@@            Coverage Diff             @@
##           master    #1299      +/-   ##
==========================================
- Coverage   81.93%   81.93%   -0.01%     
==========================================
  Files         170      173       +3     
  Lines        8660     8817     +157     
==========================================
+ Hits         7096     7224     +128     
- Misses       1564     1593      +29
Impacted Files Coverage Δ
...LintFramework/Rules/CyclomaticComplexityRule.swift 88% <100%> (ø)
...FrameworkTests/CyclomaticComplexityRuleTests.swift 75.67% <75.67%> (ø)
...igurations/CyclomaticComplexityConfiguration.swift 81.08% <81.08%> (ø)
...Tests/CyclomaticComplexityConfigurationTests.swift 84.33% <84.33%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a964167...bc3435f. Read the comment docs.

Copy link
Collaborator

@jpsim jpsim left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in reviewing this, it's awesome! I have a few suggestions.

case error = "error"
case ignoresCaseStatements = "ignores_case_statements"

static func all() -> [ConfigurationKey] {
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 unused

.ignoresCaseStatements
]
}
static func allValues() -> [String] {
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 unused

.repeatWhile,
.while,
.case
])
Copy link
Collaborator

Choose a reason for hiding this comment

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

indentation is off here

return length.consoleDescription + ", ignores switch statements: \(ignoresCaseStatements)"
}

public static let defaultComplexityStatements: Set<StatementKind> = Set([
Copy link
Collaborator

Choose a reason for hiding this comment

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

could use array literal conversion here:

public static let defaultComplexityStatements: Set<StatementKind> = [
        .forEach,
        ...

.case
])

var length: SeverityLevelsConfiguration
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be private(set)? We use that pattern a lot in configurations.


private(set) public var complexityStatements: Set<StatementKind>

var ignoresCaseStatements: Bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be private(set)?

}
}

var params: [RuleParameter<Int>] {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be private(set)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This one, no, it's a virtual getter -- returns length.params. The, others yes, updating PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jpsim -- Following up on this, since it's the one remaining issue. I can't add a set to this accessor, it's read-only -- returns length.params, which is itself read-only.

@mlwelles
Copy link
Contributor Author

I've updated to add tests for the configuration class, and made all the requested changes except for adding private(set) to params -- since it's a read only property. LMK if there's anything else needed. Thanks!

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.

Besides my inline comments, LinuxMain.swift needs to be updated to add the new test classes so they're run on Linux as well.

try configuration.apply(configuration: config3)
XCTAssertEqual(configuration.length, length2)
XCTAssertFalse(configuration.ignoresCaseStatements)
} catch {
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can mark the test as throws and avoid the do/catch here

@@ -46,6 +46,9 @@
[Marcelo Fabri](https://github.com/marcelofabri)
[#1278](https://github.com/realm/SwiftLint/issues/1278)

* Add `ignores_case_statements` as option to `CyclomaticComplexityRule`.
[Michael L. Welles](https://github.com/mlwelles)
[#1298](https://github.com/realm/SwiftLint/issues/1298)
Copy link
Collaborator

Choose a reason for hiding this comment

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

missing new line

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, updated LinuxMain + the items mentioned inline.

@@ -30,5 +30,7 @@ XCTMain([
testCase(TrailingCommaRuleTests.allTests),
testCase(VerticalWhitespaceRuleTests.allTests),
testCase(YamlParserTests.allTests),
testCase(YamlSwiftLintTests.allTests)
testCase(YamlSwiftLintTests.allTests),
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should should be kept ordered

@mwelles-mdsol
Copy link
Contributor

@jpsim @marcelofabri Anything else needed for this?

@jpsim
Copy link
Collaborator

jpsim commented Mar 10, 2017

Thanks for your hard work and patience with this @mlwelles! 👏

@jpsim jpsim merged commit 1d27ae1 into realm:master Mar 10, 2017
@mwelles-mdsol mwelles-mdsol deleted the cyclomatic_complexity_ignores_case_statments branch March 10, 2017 23:20
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