realm / SwiftLint

A tool to enforce Swift style and conventions.
https://realm.github.io/SwiftLint
MIT License
18.45k stars 2.2k forks source link

Unused import rule transitive dependencies #5622

Open PaulTaykalo opened 2 weeks ago

PaulTaykalo commented 2 weeks ago

This PR tries to resolve an issue, when unused import resolved incorrectly, since swiflint doesn't check transitive imports

For example, if we have

import Cocoa

let viewController = NSViewController()

Cocoa module is actually

import AppKit
import Foundation
import CoreData

So this PR actually tries to account transitive_modules configuration to prevent incorrect import removals

Alternative

As an alternative we can try to generate interfaces of all imported modules (once per module) And parse their imports on the top level

So , for example, if we found some missing modules, we'll check if those can be found in the generated interface.

For example,

import SwiftUI

Will allow to use any of the underlying modules

/// Generated SwifTUI interface
import Accessibility
import AppKit
import Combine
import CoreData
import CoreFoundation
import CoreGraphics
import CoreTransferable
import Darwin
import DeveloperToolsSupport
import Foundation
import OSLog
import Observation
import Spatial
import SwiftUICore
import Symbols
import TargetConditionals
import UniformTypeIdentifiers
import _Concurrency
import _StringProcessing
import _SwiftConcurrencyShims
import os
import os.log
import simd

Related Issues: https://github.com/realm/SwiftLint/issues/5167