nicklockwood / SwiftFormat

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

Rule `redundantSelf` shouldn't remove self for `@dynamicMemberLookup` #1141

Open malaba opened 2 years ago

malaba commented 2 years ago

What happens:

It appear that when the definition of the type with @dynamicMemberLookup isn't in the same file as an extension that use those member access, the mandatory self is removed by swiftformat.

Xcode project here.

The code compile, but once you run swiftformat at the root directory, it doesn't compile anymore.

Test on latest: Xcode v13.2.1, Swiftformat 0.49.4.

What is expected:

That swiftformat see that dynamic member lookup need self and it isn't removed.

Or if that is impossible (file parsed one by one without context from other files ?) then an option to specify some member lookup need the self (similar to --selfrequired for @ autoclosure).

nicklockwood commented 2 years ago

@malaba this is a known limitation of SwiftFormat's per-file formatting implementation. Currently no information is shared between files in a project, so it cannot know about a @dynamicMemberLookup declaration in another file. The only workaround for now is to use --selfrequired or disable the redundantSelf rule for that file.

domkm commented 7 months ago

@nicklockwood Could --selfrequired perhaps accept type names in addition to function names?