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

[ExplicitSelfRule] Fix violation location and misplaced corrections #3507

Merged
Merged
Changes from 1 commit
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
Prev Previous commit
Split explicit self examples into a separate file
  • Loading branch information
jpsim committed Jan 29, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 6f2573d6cdc4122a4eee65c0d2d5c911ab6f98c8
175 changes: 4 additions & 171 deletions Source/SwiftLintFramework/Rules/Style/ExplicitSelfRule.swift
Original file line number Diff line number Diff line change
@@ -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<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("""
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
}
""")
],
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
}
""")
],
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
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
}
""")
]
}