Repository navigation
Conversation
For example, if `CGFloat` is used in a file where only `UIKit` is imported but not `CoreGraphics`, this will be a violation even if the file previously compiled. This is because Swift allows referencing some declarations that are only transitively imported. Enabling the `require_explicit_imports` configuration option will require that the module of every declaration referenced in a source file be explicitly imported. This will add significant noise to the imports list, but has a few advantages: 1. It will be easier to understand all the dependencies explicitly referenced in a source file. 2. Correcting the `unused_import` rule will no longer introduce compilation errors in files that compiled prior to the correction. Missing imports will always be added to the top of the file, which may cause `sorted_imports` violations. You may run SwiftLint autocorrect with the `sorted_imports` rule enabled to re-sort those import statements afterwards.
Here's an example of your CHANGELOG entry: * Add unused_import config option to require imports for each module used.
[jpsim](https://github.com/jpsim)
[#issue_number](https://github.com/realm/SwiftLint/issues/issue_number)note: There are two invisible spaces after the entry's text. Generated by 🚫 Danger |
This was referenced Feb 16, 2020
Collaborator
Author
|
Continuing in #3123. |
jpsim
deleted the
add-unused_import-config-option-to-require-imports-for-each-module-used
branch
February 18, 2020 21:03
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is a proof of concept, see TODOs for what would need to be cleaned up
For example, if
CGFloatis used in a file where onlyUIKitis imported but notCoreGraphics, this will be a violation even if the file previously compiled.This is because Swift allows referencing some declarations that are only transitively imported.
Enabling the
require_explicit_importsconfiguration option will require that the module of every declaration referenced in a source file be explicitly imported.This will add significant noise to the imports list, but has a few advantages:
unused_importrule will no longer introduce compilation errors in files that compiled prior to the correction.Missing imports will always be added to the top of the file, which may cause
sorted_importsviolations. You may run SwiftLint autocorrect with thesorted_importsrule enabled to re-sort those import statements afterwards.