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 SwiftLint rule to prevent multiple variable declarations on one line #5990

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* Add new `opaque_over_existential` opt-in rule that triggers when the existential `any` type of a
function parameter can be replaced with an opaque `some` type.
[SimplyDanny](https://github.com/SimplyDanny)
* Add new `multiple_variable_declaration` opt-in rule that triggers when the there's any declaration of variable and/or constants at the same line.
[Ahmad Zaghloul](https://github.com/AhmedZaghloul19)

### Bug Fixes

Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintBuiltInRules/Models/BuiltInRules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public let builtInRules: [any Rule.Type] = [
MultilineParametersBracketsRule.self,
MultilineParametersRule.self,
MultipleClosuresWithTrailingClosureRule.self,
MultipleVariableDeclarationRule.self,
NSLocalizedStringKeyRule.self,
NSLocalizedStringRequireBundleRule.self,
NSNumberInitAsFunctionReferenceRule.self,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import SwiftSyntax

@SwiftSyntaxRule(optIn: true)
struct MultipleVariableDeclarationRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)

static let description = RuleDescription(
identifier: "multiple_variable_declaration",
name: "Multiple Variable Declaration",
description: "Variables should not be declared on the same line",
kind: .style,
nonTriggeringExamples: [
Example(
"""
let a = 1
let b = 2
"""
),
Example(
"""
var x = 10
var y = 20
"""
),
Example(
"""
let x = 10
var y = 20
"""
),
],
triggeringExamples: [
Example("let a = 1; let b = 2"),
Example("var x = 10; var y = 20"),
Example("let x = 10; var y = 20"),
]
)
}

private extension MultipleVariableDeclarationRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
private var lastVariableLine: Int?

override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
let converter = file.locationConverter
let currentLocation = converter
.location(for: node.positionAfterSkippingLeadingTrivia)
let currentLine = currentLocation.line
guard currentLine > 0 else {
return .skipChildren
}

if let lastLine = lastVariableLine, lastLine == currentLine {
violations.append(node.positionAfterSkippingLeadingTrivia)
}
lastVariableLine = currentLine
return .visitChildren
}
}
}
6 changes: 6 additions & 0 deletions Tests/GeneratedTests/GeneratedTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,12 @@ final class MultipleClosuresWithTrailingClosureRuleGeneratedTests: SwiftLintTest
}
}

final class MultipleVariableDeclarationRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(MultipleVariableDeclarationRule.description)
}
}

final class NSLocalizedStringKeyRuleGeneratedTests: SwiftLintTestCase {
func testWithDefaultConfiguration() {
verifyRule(NSLocalizedStringKeyRule.description)
Expand Down
4 changes: 4 additions & 0 deletions Tests/IntegrationTests/default_rule_configurations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,10 @@ multiple_closures_with_trailing_closure:
severity: warning
meta:
opt-in: false
multiple_variable_declaration:
severity: warning
meta:
opt-in: true
nesting:
type_level:
warning: 1
Expand Down