nicklockwood / SwiftFormat

A command-line tool and Xcode Extension for formatting Swift code
MIT License
7.9k stars 639 forks source link

--selfrequired option ignores `self` in string interpolation parameters #1128

Closed CraigsOverItAll closed 2 years ago

CraigsOverItAll commented 2 years ago

I think this is a bug (or I'm holding it wrong 😉 ) — Interpolated string parameters with self have self removed even with the method names being listed via --selfrequired option.

I hit this while moving to Logger which takes interpolated strings as an autoclosure. After excluding print and all of the Logger convenience methods via the .swiftformat config file (below) the problem still exists.

.swiftformat config file

--swiftversion 5.5

# Exclude default Logger methods which use autoclosures
--selfrequired print,debug,info,notice,warning,trace,error,critical

Source file: main.swift

import OSLog

class Example {
    let logger = Logger(subsystem: "Subsystem", category: "Example")
    private(set) var properties: [String] = []

    func add(property: String) {
        guard !property.isEmpty else {
            logger.debug("Attempt to add empty string.")
            return
        }

        properties.append(property)
        print("New property \(property) (\(self.properties.count) properties in total)")
        print(self.properties)

        logger.debug("New property \(property) (\(self.properties.count) properties in total)")
    }
}

Output

% swiftformat --version
0.49.2
% swiftformat --lint .
Running SwiftFormat...
(lint mode - no files will be changed.)
Reading config file at /Users/craig/Desktop/SFBug/Swift Format Bug/.swiftformat
/Users/craig/Desktop/SFBug/Swift Format Bug/Swift Format Bug/main.swift:14:1: warning: (redundantSelf) Insert/remove explicit self where applicable.
/Users/craig/Desktop/SFBug/Swift Format Bug/Swift Format Bug/main.swift:17:1: warning: (redundantSelf) Insert/remove explicit self where applicable.
SwiftFormat completed in 0.03s.
Source input did not pass lint check.
1/1 files require formatting.

I think the config file is working and that the --selfrequired is being honoured as removing print from the list of methods triggers a warning on line 15 as well.

% swiftformat --lint .
Running SwiftFormat...
(lint mode - no files will be changed.)
Reading config file at /Users/craig/Desktop/SFBug/Swift Format Bug/.swiftformat
/Users/craig/Desktop/SFBug/Swift Format Bug/Swift Format Bug/main.swift:14:1: warning: (redundantSelf) Insert/remove explicit self where applicable.
/Users/craig/Desktop/SFBug/Swift Format Bug/Swift Format Bug/main.swift:15:1: warning: (redundantSelf) Insert/remove explicit self where applicable.
/Users/craig/Desktop/SFBug/Swift Format Bug/Swift Format Bug/main.swift:17:1: warning: (redundantSelf) Insert/remove explicit self where applicable.
SwiftFormat completed in 0.02s.
Source input did not pass lint check.
1/1 files require formatting.
nicklockwood commented 2 years ago

I think the config file is working and that the --selfrequired is being honoured as removing print from the list of methods triggers a warning on line 15 as well.

Yep, it looks like the issue is that --selfrequired isn't applied inside string interpolation.

CraigsOverItAll commented 2 years ago

Thanks @nicklockwood — can confirm it's working for our use cases. 😁

nicklockwood commented 2 years ago

@CraigsOverItAll fix landed in 0.49.3