New Issue Checklist
New Rule Request
Suggested name: imports_at_top
Require file-scope import declarations to appear at the beginning of the source file (after an optional file header / comments), not after types or other declarations.
Swift treats imports as file-scoped regardless of where they are written, so this compiles:
import Testing
struct Example {}
@testable import MyModule
sorted_imports does not catch it. As of 0.62.0 it only sorts a contiguous import block; an import after other code is a separate one-line group, so lint and --fix leave it at the bottom. I could not find an existing issue that asks for this placement check (nearby work is sort order inside a block: #4810, #6665, #4935).
Why
This is the usual Swift convention. Google’s Swift style guide says import statements are the first non-comment tokens in a source file.
Apple’s swift-format already enforces it. OrderedImports:
Lint: If an import appears anywhere other than the beginning of the file it resides in, not lexicographically ordered, or (optionally) not in the appropriate import group, a lint error is raised.
Format: Imports will be reordered and (optionally) grouped at the top of the file.
A dedicated SwiftLint rule (or a sorted_imports option) would close the same gap for projects that use SwiftLint rather than swift-format. I am filing a matching request on nicklockwood/SwiftFormat.
Triggering
import Foundation
struct Foo {}
import UIKit // violation
import Testing
struct FooTests {}
@testable import MyModule // violation
Non-triggering
// File header comment is fine.
import Foundation
import UIKit
struct Foo {}
#if canImport(UIKit)
import UIKit
#endif
import Foundation
struct Foo {}
(Header #if import groups that sit with the leading imports should stay allowed.)
Configuration
Optional, if useful:
- whether to allow
#if / #endif import groups only when they are part of the leading import section
- whether comments between the file header and the first import are allowed (yes by default)
Sorting within the header block can remain sorted_imports. This rule only needs to flag (and ideally correct) imports that are not in that leading section.
Opt-in
Opt-in. Placement is a style convention, not a correctness issue, and some codebases may put #if imports later on purpose. That matches the README guidance for rules that are not general consensus. Correction (hoist to the header, then let sorted_imports order them) would be useful.
New Issue Checklist
New Rule Request
Suggested name:
imports_at_topRequire file-scope
importdeclarations to appear at the beginning of the source file (after an optional file header / comments), not after types or other declarations.Swift treats imports as file-scoped regardless of where they are written, so this compiles:
sorted_importsdoes not catch it. As of 0.62.0 it only sorts a contiguous import block; an import after other code is a separate one-line group, so lint and--fixleave it at the bottom. I could not find an existing issue that asks for this placement check (nearby work is sort order inside a block: #4810, #6665, #4935).Why
This is the usual Swift convention. Google’s Swift style guide says import statements are the first non-comment tokens in a source file.
Apple’s swift-format already enforces it.
OrderedImports:A dedicated SwiftLint rule (or a
sorted_importsoption) would close the same gap for projects that use SwiftLint rather than swift-format. I am filing a matching request on nicklockwood/SwiftFormat.Triggering
Non-triggering
(Header
#ifimport groups that sit with the leading imports should stay allowed.)Configuration
Optional, if useful:
#if/#endifimport groups only when they are part of the leading import sectionSorting within the header block can remain
sorted_imports. This rule only needs to flag (and ideally correct) imports that are not in that leading section.Opt-in
Opt-in. Placement is a style convention, not a correctness issue, and some codebases may put
#ifimports later on purpose. That matches the README guidance for rules that are not general consensus. Correction (hoist to the header, then letsorted_importsorder them) would be useful.