From c1daacbc349aad3b15b08f980b97c5601d4ef068 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Fri, 29 Jan 2021 12:15:25 -0500 Subject: [PATCH 1/2] [ExplicitSelfRule] Fix violation location and misplaced corrections --- CHANGELOG.md | 4 +++ .../Rules/Style/ExplicitSelfRule.swift | 32 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ea247e7d2f..c33e96ce0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,10 @@ * Fix typos in configuration options for `file_name` rule. [advantis](https://github.com/advantis) +* Fix violation location and misplaced corrections for some function + references in `explicit_self` rule. + [JP Simard](https://github.com/jpsim) + ## 0.42.0: He Chutes, He Scores #### Breaking diff --git a/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift b/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift index 31cddb259b..4d6cf97f38 100644 --- a/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift @@ -66,6 +66,14 @@ public struct ExplicitSelfRule: CorrectableRule, ConfigurationProviderRule, Anal } """), Example(""" + struct A { + func f1(a b: Int) {} + func f2() { + ↓f1(a: 0) + } + } + """), + Example(""" @propertyWrapper struct Wrapper { let wrappedValue: Value @@ -119,6 +127,22 @@ public struct ExplicitSelfRule: CorrectableRule, ConfigurationProviderRule, Anal } """), Example(""" + struct A { + func f1(a b: Int) {} + func f2() { + ↓f1(a: 0) + } + } + """): + Example(""" + struct A { + func f1(a b: Int) {} + func f2() { + self.f1(a: 0) + } + } + """), + Example(""" @propertyWrapper struct Wrapper { let wrappedValue: Value @@ -243,8 +267,12 @@ private extension SwiftLintFile { // prefixes `$` and `_`), while `key.name` contains the prefix. Hence we need to check for explicit access // at a corrected offset as well. var prefixLength: Int64 = 0 - if let name = cursorInfo["key.name"] as? String, let length = cursorInfo["key.length"] as? Int64 { - prefixLength = Int64(name.count) - length + let sourceKittenDictionary = SourceKittenDictionary(cursorInfo) + if sourceKittenDictionary.kind == "source.lang.swift.ref.var.instance", + let name = sourceKittenDictionary.name, + let length = sourceKittenDictionary.length + { + prefixLength = Int64(name.count - length.value) if prefixLength > 0, isExplicitAccess(at: offset - ByteCount(prefixLength)) { return nil } From 6f2573d6cdc4122a4eee65c0d2d5c911ab6f98c8 Mon Sep 17 00:00:00 2001 From: JP Simard Date: Fri, 29 Jan 2021 12:55:10 -0500 Subject: [PATCH 2/2] Split explicit self examples into a separate file --- .../Rules/Style/ExplicitSelfRule.swift | 175 +----------------- .../Style/ExplicitSelfRuleExamples.swift | 173 +++++++++++++++++ 2 files changed, 177 insertions(+), 171 deletions(-) create mode 100644 Source/SwiftLintFramework/Rules/Style/ExplicitSelfRuleExamples.swift diff --git a/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift b/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift index 4d6cf97f38..7b288cdcff 100644 --- a/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift +++ b/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift @@ -11,175 +11,9 @@ public struct ExplicitSelfRule: CorrectableRule, ConfigurationProviderRule, Anal name: "Explicit Self", description: "Instance variables and functions should be explicitly accessed with 'self.'.", kind: .style, - nonTriggeringExamples: [ - Example(""" - struct A { - func f1() {} - func f2() { - self.f1() - } - } - """), - Example(""" - struct A { - let p1: Int - func f1() { - _ = self.p1 - } - } - """), - Example(""" - @propertyWrapper - struct Wrapper { - let wrappedValue: Value - var projectedValue: [Value] { - [self.wrappedValue] - } - } - struct A { - @Wrapper var p1: Int - func f1() { - self.$p1 - self._p1 - } - } - func f1() { - A(p1: 10).$p1 - } - """) - ], - triggeringExamples: [ - Example(""" - struct A { - func f1() {} - func f2() { - ↓f1() - } - } - """), - Example(""" - struct A { - let p1: Int - func f1() { - _ = ↓p1 - } - } - """), - Example(""" - struct A { - func f1(a b: Int) {} - func f2() { - ↓f1(a: 0) - } - } - """), - Example(""" - @propertyWrapper - struct Wrapper { - let wrappedValue: Value - var projectedValue: [Value] { - [self.wrappedValue] - } - } - struct A { - @Wrapper var p1: Int - func f1() { - ↓$p1 - ↓_p1 - } - } - func f1() { - A(p1: 10).$p1 - } - """) - ], - corrections: [ - Example(""" - struct A { - func f1() {} - func f2() { - ↓f1() - } - } - """): - Example(""" - struct A { - func f1() {} - func f2() { - self.f1() - } - } - """), - Example(""" - struct A { - let p1: Int - func f1() { - _ = ↓p1 - } - } - """): - Example(""" - struct A { - let p1: Int - func f1() { - _ = self.p1 - } - } - """), - Example(""" - struct A { - func f1(a b: Int) {} - func f2() { - ↓f1(a: 0) - } - } - """): - Example(""" - struct A { - func f1(a b: Int) {} - func f2() { - self.f1(a: 0) - } - } - """), - Example(""" - @propertyWrapper - struct Wrapper { - let wrappedValue: Value - var projectedValue: [Value] { - [self.wrappedValue] - } - } - struct A { - @Wrapper var p1: Int - func f1() { - ↓$p1 - ↓_p1 - } - } - func f1() { - A(p1: 10).$p1 - } - """): Example(""" - @propertyWrapper - struct Wrapper { - let wrappedValue: Value - var projectedValue: [Value] { - [self.wrappedValue] - } - } - struct A { - @Wrapper var p1: Int - func f1() { - self.$p1 - self._p1 - } - } - func f1() { - A(p1: 10).$p1 - } - """) - ], + nonTriggeringExamples: ExplicitSelfRuleExamples.nonTriggeringExamples, + triggeringExamples: ExplicitSelfRuleExamples.triggeringExamples, + corrections: ExplicitSelfRuleExamples.corrections, requiresFileOnDisk: true ) @@ -270,8 +104,7 @@ private extension SwiftLintFile { let sourceKittenDictionary = SourceKittenDictionary(cursorInfo) if sourceKittenDictionary.kind == "source.lang.swift.ref.var.instance", let name = sourceKittenDictionary.name, - let length = sourceKittenDictionary.length - { + let length = sourceKittenDictionary.length { prefixLength = Int64(name.count - length.value) if prefixLength > 0, isExplicitAccess(at: offset - ByteCount(prefixLength)) { return nil diff --git a/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRuleExamples.swift b/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRuleExamples.swift new file mode 100644 index 0000000000..39626a427b --- /dev/null +++ b/Source/SwiftLintFramework/Rules/Style/ExplicitSelfRuleExamples.swift @@ -0,0 +1,173 @@ +struct ExplicitSelfRuleExamples { + static let nonTriggeringExamples = [ + Example(""" + struct A { + func f1() {} + func f2() { + self.f1() + } + } + """), + Example(""" + struct A { + let p1: Int + func f1() { + _ = self.p1 + } + } + """), + Example(""" + @propertyWrapper + struct Wrapper { + let wrappedValue: Value + var projectedValue: [Value] { + [self.wrappedValue] + } + } + struct A { + @Wrapper var p1: Int + func f1() { + self.$p1 + self._p1 + } + } + func f1() { + A(p1: 10).$p1 + } + """) + ] + + static let triggeringExamples = [ + Example(""" + struct A { + func f1() {} + func f2() { + ↓f1() + } + } + """), + Example(""" + struct A { + let p1: Int + func f1() { + _ = ↓p1 + } + } + """), + Example(""" + struct A { + func f1(a b: Int) {} + func f2() { + ↓f1(a: 0) + } + } + """), + Example(""" + @propertyWrapper + struct Wrapper { + let wrappedValue: Value + var projectedValue: [Value] { + [self.wrappedValue] + } + } + struct A { + @Wrapper var p1: Int + func f1() { + ↓$p1 + ↓_p1 + } + } + func f1() { + A(p1: 10).$p1 + } + """) + ] + + static let corrections = [ + Example(""" + struct A { + func f1() {} + func f2() { + ↓f1() + } + } + """): + Example(""" + struct A { + func f1() {} + func f2() { + self.f1() + } + } + """), + Example(""" + struct A { + let p1: Int + func f1() { + _ = ↓p1 + } + } + """): + Example(""" + struct A { + let p1: Int + func f1() { + _ = self.p1 + } + } + """), + Example(""" + struct A { + func f1(a b: Int) {} + func f2() { + ↓f1(a: 0) + } + } + """): + Example(""" + struct A { + func f1(a b: Int) {} + func f2() { + self.f1(a: 0) + } + } + """), + Example(""" + @propertyWrapper + struct Wrapper { + let wrappedValue: Value + var projectedValue: [Value] { + [self.wrappedValue] + } + } + struct A { + @Wrapper var p1: Int + func f1() { + ↓$p1 + ↓_p1 + } + } + func f1() { + A(p1: 10).$p1 + } + """): Example(""" + @propertyWrapper + struct Wrapper { + let wrappedValue: Value + var projectedValue: [Value] { + [self.wrappedValue] + } + } + struct A { + @Wrapper var p1: Int + func f1() { + self.$p1 + self._p1 + } + } + func f1() { + A(p1: 10).$p1 + } + """) + ] +}