Skip to content

Handle removing system imports #25

@keith

Description

@keith

Right now only your own imports are correctly handled. System imports bring a new set of issues. Here are a few I hit while working on this:

  • You have to handle @objc (and other attributes) requiring a Foundation import
  • extensions on TimeInterval don't find definition because definition is NSTimeInterval. I think the tool would need to understand apinotes to handle this case.
  • Some things come from places you might not expect, and that can be ok. SwiftLint provides a way to map imports. When we were using that we used this config:
unused_import:
  require_explicit_imports: true
  always_keep_imports:
    - AVFoundation # Never translate to AVFAudio, which is only available in 14.5+
  allowed_transitive_imports:
    - module: AVFoundation
      allowed_transitive_imports:
        - AVFAudio
    - module: Foundation
      allowed_transitive_imports:
        - CoreFoundation
        - Darwin
        - ObjectiveC
    - module: CoreMedia
      allowed_transitive_imports:
        - _SwiftCoreMediaOverlayShims
    - module: GoogleMaps
      allowed_transitive_imports:
        - GoogleMapsBase
    - module: GoogleNavigation
      allowed_transitive_imports:
        - GoogleMaps
    - module: UIKit
      allowed_transitive_imports:
        - CoreFoundation
        - CoreGraphics
        - CoreText
        - Darwin
        - ObjectiveC
        - QuartzCore

But this can depend on how strict you want to be. Besides AVFoundation / AVFAudio which you can only fix if you have a high deployment target.

  • Some things you might want to allow shadowing of, like GoogleMapsBase vs GoogleMaps in the example config above, where that framework is distributed as 3 separate frameworks to avoid GitHub's file size limit, but realistically you likely want users to only ever import GoogleMaps, so I think this being user configurable is required.
  • There are some other cases I haven't debugged enough that cause some issues as well, but this is a sense of the work needed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions