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

Actually implement NoPlaygroundLiterals rule. #613

Merged
merged 1 commit into from
Sep 4, 2023

Conversation

allevato
Copy link
Member

@allevato allevato commented Sep 4, 2023

This file has been stubbed out forever, so actually implement it. It was originally meant to be a format rule, but those replacements are either tricky or undesirable:

  • #fileLiteral is implemented as Bundle.main.url(forResource:withExtension:), but I don't want to "endorse" Bundle.main since it's almost always better to use Bundle(for: AnyClass) so that code works when it's pulled into a framework target or when running unit tests.
  • #colorLiteral and #imageLiteral are platform-dependent, being implemented as either {NS,UI}Color and {NS,UI}Image respectively. Sometimes this can be determined by checking for imports of AppKit/Cocoa or UIKit, but other frameworks like SwiftUI can re-export those and thus the source file may not actually have an import that we can unambiguously determine the right replacement for. It can also be impossible to determine if there are multi-platform conditions that import all of those modules.

So, we just make these literals lint warnings and give the user rough ideas about how to fix them.

This file has been stubbed out forever, so actually implement it.
It was originally meant to be a format rule, but those replacements
are either tricky or undesirable:

- `#fileLiteral` is implemented as `Bundle.main.url(forResource:withExtension:)`,
  but I don't want to "endorse" `Bundle.main` since it's almost always better to
  use `Bundle(for: AnyClass)` so that code works when it's pulled into a
  framework target or when running unit tests.
- `#colorLiteral` and `#imageLiteral` are platform-dependent, being implemented
  as either `{NS,UI}Color` and `{NS,UI}Image` respectively. Sometimes this can
  be determined by checking for imports of `AppKit/Cocoa` or `UIKit`, but other
  frameworks like `SwiftUI` can re-export those and thus the source file may not
  actually have an import that we can unambiguously determine the right
  replacement for. It can also be impossible to determine if there are
  multi-platform conditions that import all of those modules.

So, we just make these literals lint warnings and give the user rough ideas
about how to fix them.
@allevato allevato merged commit 7cd38d8 into swiftlang:main Sep 4, 2023
@allevato allevato deleted the playground-literals branch September 4, 2023 19:39
allevato added a commit to allevato/swift-format that referenced this pull request Sep 14, 2023
Actually implement `NoPlaygroundLiterals` rule.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant