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

Add if_only option to rule conditional_returns_on_newline #2308

Merged
merged 4 commits into from
Jul 24, 2018

Conversation

Jeehut
Copy link
Collaborator

@Jeehut Jeehut commented Jul 23, 2018

Fixes #2307.

@Jeehut Jeehut force-pushed the conditional-returns-on-newline branch from 5332a56 to 51a9a62 Compare July 23, 2018 08:23
@SwiftLintBot
Copy link

SwiftLintBot commented Jul 23, 2018

12 Messages
📖 Linting Aerial with this PR took 0.49s vs 0.4s on master (22% slower)
📖 Linting Alamofire with this PR took 4.26s vs 3.44s on master (23% slower)
📖 Linting Firefox with this PR took 16.9s vs 12.3s on master (37% slower)
📖 Linting Kickstarter with this PR took 22.17s vs 18.3s on master (21% slower)
📖 Linting Moya with this PR took 2.38s vs 1.97s on master (20% slower)
📖 Linting Nimble with this PR took 2.2s vs 1.78s on master (23% slower)
📖 Linting Quick with this PR took 0.56s vs 0.54s on master (3% slower)
📖 Linting Realm with this PR took 4.07s vs 3.28s on master (24% slower)
📖 Linting SourceKitten with this PR took 1.13s vs 1.06s on master (6% slower)
📖 Linting Sourcery with this PR took 5.61s vs 4.81s on master (16% slower)
📖 Linting Swift with this PR took 35.42s vs 28.98s on master (22% slower)
📖 Linting WordPress with this PR took 17.17s vs 16.22s on master (5% slower)

Generated by 🚫 Danger

@codecov-io
Copy link

codecov-io commented Jul 23, 2018

Codecov Report

Merging #2308 into master will decrease coverage by 0.01%.
The diff coverage is 85.1%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2308      +/-   ##
==========================================
- Coverage   92.16%   92.15%   -0.02%     
==========================================
  Files         292      294       +2     
  Lines       14738    14780      +42     
==========================================
+ Hits        13584    13620      +36     
- Misses       1154     1160       +6
Impacted Files Coverage Δ
...tFrameworkTests/AutomaticRuleTests.generated.swift 100% <ø> (ø) ⬆️
...rkTests/ConditionalReturnsOnNewlineRuleTests.swift 100% <100%> (ø)
...mework/Rules/ConditionalReturnsOnNewlineRule.swift 100% <100%> (ø) ⬆️
...ons/ConditionalReturnsOnNewlineConfiguration.swift 46.15% <46.15%> (ø)
...iftLintFramework/Extensions/String+SwiftLint.swift 85.71% <0%> (-3.58%) ⬇️
...SwiftLintFramework/Extensions/File+SwiftLint.swift 97.23% <0%> (+0.46%) ⬆️
...ntFramework/Rules/UnusedClosureParameterRule.swift 100% <0%> (+1.09%) ⬆️
Source/SwiftLintFramework/Models/Command.swift 98.7% <0%> (+1.29%) ⬆️

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 b49ae70...1452da4. Read the comment docs.

@@ -30,19 +30,21 @@ public struct ConditionalReturnsOnNewlineRule: ConfigurationProviderRule, Rule,
)

public func validate(file: File) -> [StyleViolation] {
let pattern = "(guard|if)[^\n]*return"
let pattern: String = configuration.ifOnly ? "(if)[^\n]*return" : "(guard|if)[^\n]*return"
Copy link
Collaborator

Choose a reason for hiding this comment

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

we usually don't add explicit types

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

return file.rangesAndTokens(matching: pattern).filter { _, tokens in
guard let firstToken = tokens.first, let lastToken = tokens.last,
SyntaxKind(rawValue: firstToken.type) == .keyword &&
SyntaxKind(rawValue: lastToken.type) == .keyword else {
return false
}

return ["if", "guard"].contains(content(for: firstToken, file: file)) &&
let searchTokens: [String] = configuration.ifOnly ? ["if"] : ["if", "guard"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

we usually don't add explicit types

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was merely to speed up the compiler since it seems to be slow on type-checking ternary operators. But I can remove it, if you want.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed.

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.

Can you please add some tests to make sure we don't break this in the future?

I recommend removing the AutomaticTestableRule conformance and creating a new ConditionalReturnsOnNewlineRuleTests that has a test for the default configuration and one for setting if_only: true

@Jeehut
Copy link
Collaborator Author

Jeehut commented Jul 24, 2018

@marcelofabri I just removed the AutomaticTestableRule and introduced a manual test which covers the configuration as suggested. I also addressed your other feeback. I hope it looks good now!

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.

Looks good! Thanks! 🚀

@marcelofabri marcelofabri merged commit e9c6d6a into realm:master Jul 24, 2018
@Jeehut Jeehut deleted the conditional-returns-on-newline branch November 30, 2018 15:26
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.

4 participants