Rule Request: imports_at_top
Nobody has claimed this yet.
- Dominant language
- Swift
- Stars
- 19.7k
- Forks
- 2.3k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 11
Description
New Issue Checklist
- Updated SwiftLint to the latest version
- I searched for existing GitHub issues
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/#endifimport 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by examining the existing sorted_imports rule and how SwiftLint handles import groups and opt-in rules. The work is done when imports after declarations are reported and, if correction is supported, moved into the leading import section while header comments and leading conditional import groups remain allowed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 58/100