Skip to content

Commit

Permalink
[ExplicitSelfRule] Fix violation location and misplaced corrections (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpsim authored Jan 29, 2021
1 parent 59eb887 commit bbf1ad4
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 147 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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
Expand Down
155 changes: 8 additions & 147 deletions Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,151 +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<Value> {
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("""
@propertyWrapper
struct Wrapper<Value> {
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("""
@propertyWrapper
struct Wrapper<Value> {
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<Value> {
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
)

Expand Down Expand Up @@ -243,8 +101,11 @@ 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
}
Expand Down
173 changes: 173 additions & 0 deletions Source/SwiftLintFramework/Rules/Style/ExplicitSelfRuleExamples.swift
Original file line number Diff line number Diff line change
@@ -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<Value> {
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<Value> {
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<Value> {
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<Value> {
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
}
""")
]
}

0 comments on commit bbf1ad4

Please sign in to comment.