swiftlang / swift-format

Formatting technology for Swift source code
Apache License 2.0
2.49k stars 229 forks source link

[SR-12714] Explicit self. #329

Closed weissi closed 2 years ago

weissi commented 4 years ago
Previous ID SR-12714
Radar None
Original Reporter @weissi
Type New Feature
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | swift-format | |Labels | New Feature | |Assignee | None | |Priority | Medium | md5: 0eee30a579bfbe4ae0442eb637819834

Issue Description:

AFAIK, swift-format doesn't support adding explicit self. everywhere. It would be great if it could do that.

pronebird commented 2 years ago

It does not support trimming implicit self either, so would be good to have both options.

allevato commented 2 years ago

Adding or removing explicit self would likely require semantic information about the symbols, not just the syntax, to distinguish identifiers and know whether they're members of self and that the code could be changed without changing its behavior.

It might be possible to approximate it by tracking symbols in their lexical scopes and use that to determine which symbol is being referred to, we wouldn't have access to the compiler Decls that would let us be 100% confident that self is the same as the container for some property x if it's also in the presence of other shadowing names.

keith commented 2 years ago

FYI SwiftLint has a semantic version of this https://github.com/realm/SwiftLint/#configuration and SwiftFormat has a best effort version where it inserts and removes only ones it's confident about AFAIUI

allevato commented 2 years ago

Closing this as not planned, since we would need semantic information to do this correctly, and I prefer not to implement a syntax-only version that is incomplete.