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

Disable SeverityLevelsConfig error ruleParam if only warning is set #431

Closed
wants to merge 1 commit into from
Closed

Disable SeverityLevelsConfig error ruleParam if only warning is set #431

wants to merge 1 commit into from

Conversation

robinkunde
Copy link
Contributor

SeverityLevelsConfig is used for both rules where the warning threshold must logically be lower than the error threshold and vice versa.

In order to prevent the generation of invalid configs via implicit definition in yaml, the error ruleParameter should be disable in such cases. For example, the README contains this code right now:

# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 110

If 110 is larger than whatever default value SwiftLint define for error, this would be an invalid config.

This commit also updates the test names since ViolationLevel is no more.

@@ -58,7 +58,7 @@ class RuleTests: XCTestCase {
XCTAssertFalse([RuleMock1(), RuleMock2()] == [RuleMock1()])
}

func testViolationLevelRuleInitsWithConfigDictionary() {
func testSecurityLevelRuleInitsWithConfigDictionary() {
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 be testSeverityLevelRuleInitsWithConfigDictionary. All the other tests changed in this file now also say "Security" where they should say "Severity".

@jpsim
Copy link
Collaborator

jpsim commented Jan 27, 2016

Thanks for the PR, @robinkunde! This is fine, but having error be an Optional<Int> would be more appropriate.

@robinkunde
Copy link
Contributor Author

@jpsim That's much Swiftier solution! I've amended the PR accordingly.

@@ -14,11 +14,19 @@ public struct NameConfig: RuleConfig, Equatable {
var excluded: Set<String>

var minLengthThreshold: Int {
return max(minLength.warning, minLength.error)
Copy link
Collaborator

Choose a reason for hiding this comment

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

This can just be return max(minLength.warning, minLength.error ?? minLength.warning)

@jpsim
Copy link
Collaborator

jpsim commented Jan 27, 2016

Sorry for all the nitpicky comments, @robinkunde, this is definitely going in the right direction.

Please also add a changelog entry following our guidelines. This is technically a breaking change.

@robinkunde
Copy link
Contributor Author

No worries, I appreciate the pointers. I've updated the commit.

}
if let errorNumber = config["error"] as? Int {
error = config["error"] as? Int
} else if let errorNumber = config["error"] as? Int {
error = errorNumber
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm pretty sure this can just be replaced with:

if let warningNumber = config["warning"] as? Int {
    warning = warningNumber
}
error = config["error"] as? Int

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, that's what I tried to convey in my original message.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about it, but it would change the behavior of the method in cases of config:

  • being an empty dictionary
  • containing keys besides 'warning' and 'error' (because of typos, for example)

The first one can be addressed easily enough with a where clause. The second case might be ok too, since we're documenting this as a breaking change. However, since there's no formal config validator, I wasn't quite comfortable going that route.

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

An empty dictionary or dictionaries containing keys other than error and warning are configuration errors. I added validation for this in #438.

@scottrhoyt
Copy link
Contributor

Thanks for the PR, it's looking good!

…This prevents the potential creation of invalid configs by implicit definition in Yaml.
@jpsim
Copy link
Collaborator

jpsim commented Jan 28, 2016

Thanks for your work here, @robinkunde. Merging in #438.

@jpsim jpsim closed this Jan 28, 2016
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.

3 participants