mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
57 lines
2.1 KiB
Swift
57 lines
2.1 KiB
Swift
//
|
|
// LeadingWhitespaceRule.swift
|
|
// SwiftLint
|
|
//
|
|
// Created by JP Simard on 5/16/15.
|
|
// Copyright © 2015 Realm. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SourceKittenFramework
|
|
|
|
public struct LeadingWhitespaceRule: CorrectableRule, ConfigurationProviderRule, SourceKitFreeRule {
|
|
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "leading_whitespace",
|
|
name: "Leading Whitespace",
|
|
description: "Files should not contain leading whitespace.",
|
|
nonTriggeringExamples: [ "//\n" ],
|
|
triggeringExamples: [ "\n", " //\n" ],
|
|
corrections: ["\n //": "//"]
|
|
)
|
|
|
|
public func validate(file: File) -> [StyleViolation] {
|
|
let countOfLeadingWhitespace = file.contents.countOfLeadingCharacters(in: .whitespacesAndNewlines)
|
|
if countOfLeadingWhitespace == 0 {
|
|
return []
|
|
}
|
|
return [StyleViolation(ruleDescription: type(of: self).description,
|
|
severity: configuration.severity,
|
|
location: Location(file: file.path, line: 1),
|
|
reason: "File shouldn't start with whitespace: " +
|
|
"currently starts with \(countOfLeadingWhitespace) whitespace characters")]
|
|
}
|
|
|
|
public func correct(file: File) -> [Correction] {
|
|
let whitespaceAndNewline = CharacterSet.whitespacesAndNewlines
|
|
let spaceCount = file.contents.countOfLeadingCharacters(in: whitespaceAndNewline)
|
|
guard spaceCount > 0,
|
|
let firstLineRange = file.lines.first?.range,
|
|
!file.ruleEnabled(violatingRanges: [firstLineRange], for: self).isEmpty else {
|
|
return []
|
|
}
|
|
|
|
let indexEnd = file.contents.index(
|
|
file.contents.startIndex,
|
|
offsetBy:spaceCount,
|
|
limitedBy: file.contents.endIndex) ?? file.contents.endIndex
|
|
file.write(file.contents.substring(from: indexEnd))
|
|
let location = Location(file: file.path, line: max(file.lines.count, 1))
|
|
return [Correction(ruleDescription: type(of: self).description, location: location)]
|
|
}
|
|
}
|