From cb3001bca2ebb1f4ac320c5cbf8433b7e83c66ea Mon Sep 17 00:00:00 2001 From: BB9z Date: Tue, 24 Oct 2023 03:41:10 +0800 Subject: [PATCH] Keep trailing trivia in `explicit_init` rule correction (#5296) --- CHANGELOG.md | 4 ++++ .../Rules/Idiomatic/ExplicitInitRule.swift | 14 +++++++++++++- .../ExplicitInitRuleTests.swift | 1 - 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4db4eefe2..7300eb35bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,10 @@ #### Bug Fixes +* Fix correction of `explicit_init` rule by keeping significant trivia. + [BB9z](https://github.com/BB9z) + [#5289](https://github.com/realm/SwiftLint/issues/5289) + * Fix invalid corrections for opaque and existential optionals in `syntactic_sugar` rule. [SimplyDanny](https://github.com/SimplyDanny) diff --git a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift index 64fe2eccb8..9968efc064 100644 --- a/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift +++ b/Source/SwiftLintBuiltInRules/Rules/Idiomatic/ExplicitInitRule.swift @@ -162,6 +162,18 @@ struct ExplicitInitRule: OptInRule { + """), + Example(""" + f { e in + // comment + A↓.init(e: e) + } + """): + Example(""" + f { e in + // comment + A(e: e) + } """), Example("_ = GleanMetrics.Tabs.GroupedTabExtra↓.init()"): Example("_ = GleanMetrics.Tabs.GroupedTabExtra()"), @@ -197,7 +209,7 @@ private extension ExplicitInitRule { return super.visit(node) } correctionPositions.append(violationPosition) - let newNode = node.with(\.calledExpression, calledBase.trimmed) + let newNode = node.with(\.calledExpression, calledBase) return super.visit(newNode) } } diff --git a/Tests/SwiftLintFrameworkTests/ExplicitInitRuleTests.swift b/Tests/SwiftLintFrameworkTests/ExplicitInitRuleTests.swift index fb6a58ee9f..b46b1b12e7 100644 --- a/Tests/SwiftLintFrameworkTests/ExplicitInitRuleTests.swift +++ b/Tests/SwiftLintFrameworkTests/ExplicitInitRuleTests.swift @@ -16,7 +16,6 @@ class ExplicitInitRuleTests: SwiftLintTestCase { let description = ExplicitInitRule.description .with(nonTriggeringExamples: nonTriggeringExamples) .with(triggeringExamples: triggeringExamples) - .with(corrections: [:]) verifyRule(description, ruleConfiguration: ["include_bare_init": true]) }