-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add SwiftLint rule to prevent multiple variable declarations on one line #5990
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
Open
AhmedZaghloul19
wants to merge
14
commits into
realm:main
Choose a base branch
from
AhmedZaghloul19:multiple-variable-declaration-rule
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ee27243
Add SwiftLint rule to prevent multiple variable declarations on one line
2a712e0
add the rule verification to the Generated Tests file
AhmedZaghloul19 d542734
add the rule to the test default configurations
AhmedZaghloul19 3d185cd
remove unit tests file
AhmedZaghloul19 8999cf3
change the rule order in the default rules list
AhmedZaghloul19 ec21694
Delete .vscode/settings.json
AhmedZaghloul19 92b6a81
set the rule as opt-in
AhmedZaghloul19 f866597
enhance the rule to be for multiple statements
AhmedZaghloul19 51b29cb
fulfill line length rule in change log
AhmedZaghloul19 70f41fa
change rule name in the config file
AhmedZaghloul19 47ff674
fulfill linting of the new rule
AhmedZaghloul19 fe48a67
Update Source/SwiftLintBuiltInRules/Rules/Idiomatic/MultipleStatement…
AhmedZaghloul19 1c7565d
rename the rule to be 'MultipleStatementsRule'
AhmedZaghloul19 7087a64
fix the indentations
AhmedZaghloul19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
Source/SwiftLintBuiltInRules/Rules/Idiomatic/MultipleStatementsRule.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import SwiftSyntax | ||
|
||
@SwiftSyntaxRule(explicitRewriter: true, optIn: true) | ||
struct MultipleStatementsRule: Rule { | ||
var configuration = SeverityConfiguration<Self>(.warning) | ||
|
||
static let description = RuleDescription( | ||
identifier: "multiple_statements", | ||
name: "Multiple Statements", | ||
description: "Every statement should be on its own line", | ||
kind: .idiomatic, | ||
nonTriggeringExamples: [ | ||
Example( | ||
""" | ||
let a = 1; | ||
let b = 2; | ||
""" | ||
), | ||
Example( | ||
""" | ||
var x = 10 | ||
var y = 20; | ||
""" | ||
), | ||
Example( | ||
""" | ||
let a = 1; | ||
return a | ||
""" | ||
), | ||
Example( | ||
""" | ||
if b { return }; | ||
let a = 1 | ||
""" | ||
), | ||
], | ||
triggeringExamples: [ | ||
Example("let a = 1; return a"), | ||
Example("if b { return }; let a = 1"), | ||
Example("if b { return }; if c { return }"), | ||
Example("let a = 1; let b = 2; let c = 0"), | ||
Example("var x = 10; var y = 20"), | ||
Example("let x = 10; var y = 20"), | ||
], | ||
corrections: [ | ||
Example("let a = 0↓; let b = 0"): | ||
Example( | ||
""" | ||
let a = 0 | ||
let b = 0 | ||
""" | ||
), | ||
Example("let a = 0↓; let b = 0↓; let c = 0"): | ||
Example( | ||
""" | ||
let a = 0 | ||
let b = 0 | ||
let c = 0 | ||
""" | ||
), | ||
Example("let a = 0↓; print(\"Hello\")"): | ||
Example( | ||
""" | ||
let a = 0 | ||
print(\"Hello\") | ||
""" | ||
), | ||
] | ||
) | ||
} | ||
|
||
private extension MultipleStatementsRule { | ||
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> { | ||
override func visitPost(_ node: TokenSyntax) { | ||
if node.isThereStatementAfterSemicolon { | ||
violations.append(node.positionAfterSkippingLeadingTrivia) | ||
} | ||
} | ||
} | ||
|
||
final class Rewriter: ViolationsSyntaxRewriter<ConfigurationType> { | ||
override func visit(_ node: TokenSyntax) -> TokenSyntax { | ||
guard node.isThereStatementAfterSemicolon else { | ||
return super.visit(node) | ||
} | ||
|
||
correctionPositions.append(node.positionAfterSkippingLeadingTrivia) | ||
let newNode = TokenSyntax( | ||
.unknown(""), | ||
leadingTrivia: node.leadingTrivia, | ||
trailingTrivia: .newlines(1), | ||
presence: .present | ||
) | ||
return super.visit(newNode) | ||
} | ||
} | ||
} | ||
|
||
private extension TokenSyntax { | ||
var isThereStatementAfterSemicolon: Bool { | ||
guard tokenKind == .semicolon, | ||
!trailingTrivia.isEmpty else { return false } | ||
|
||
if let nextToken = nextToken(viewMode: .sourceAccurate), | ||
isFollowedOnlyByWhitespaceOrNewline { | ||
return nextToken.leadingTrivia.containsNewlines() == false | ||
} | ||
return true | ||
} | ||
|
||
var isFollowedOnlyByWhitespaceOrNewline: Bool { | ||
guard let nextToken = nextToken(viewMode: .sourceAccurate), | ||
!nextToken.trailingTrivia.isEmpty else { | ||
return true | ||
} | ||
return nextToken.leadingTrivia.allSatisfy { $0.isWhitespaceOrNewline } | ||
} | ||
} | ||
|
||
private extension TriviaPiece { | ||
var isWhitespaceOrNewline: Bool { | ||
switch self { | ||
case .spaces, .tabs, .newlines, .carriageReturns, .carriageReturnLineFeeds: | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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 doesn't look like it got auto-fixed by the rule. The only cases that can be fixed automatically are the ones where the statements are on their own line without leading tokens. We should probably restrict the rewriter to these cases.