-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3416 from noahsark769/noahsark769/comment-spacing…
…-rule-updated Add CommentSpacingRule (#3233)
- Loading branch information
Showing
14 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
145 changes: 145 additions & 0 deletions
145
Source/SwiftLintFramework/Rules/Lint/CommentSpacingRule.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import Foundation | ||
import SourceKittenFramework | ||
|
||
public struct CommentSpacingRule: ConfigurationProviderRule, | ||
SubstitutionCorrectableRule, | ||
AutomaticTestableRule { | ||
public var configuration = SeverityConfiguration(.warning) | ||
|
||
public init() {} | ||
|
||
public static let description = RuleDescription( | ||
identifier: "comment_spacing", | ||
name: "Comment Spacing", | ||
description: "Prefer at least one space after slashes for comments.", | ||
kind: .lint, | ||
nonTriggeringExamples: [ | ||
Example(""" | ||
// This is a comment | ||
"""), | ||
Example(""" | ||
/// Triple slash comment | ||
"""), | ||
Example(""" | ||
// Multiline double-slash | ||
// comment | ||
"""), | ||
Example(""" | ||
/// Multiline triple-slash | ||
/// comment | ||
"""), | ||
Example(""" | ||
/// Multiline triple-slash | ||
/// - This is indented | ||
"""), | ||
Example(""" | ||
// - MARK: Mark comment | ||
"""), | ||
Example(""" | ||
/* Asterisk comment */ | ||
"""), | ||
Example(""" | ||
/* | ||
Multiline asterisk comment | ||
*/ | ||
""") | ||
], | ||
triggeringExamples: [ | ||
Example(""" | ||
//↓Something | ||
"""), | ||
Example(""" | ||
//↓MARK | ||
"""), | ||
Example(""" | ||
//↓👨👨👦👦Something | ||
"""), | ||
Example(""" | ||
func a() { | ||
//↓This needs refactoring | ||
print("Something") | ||
} | ||
//↓We should improve above function | ||
"""), | ||
Example(""" | ||
///↓This is a comment | ||
"""), | ||
Example(""" | ||
/// Multiline triple-slash | ||
///↓This line is incorrect, though | ||
"""), | ||
Example(""" | ||
//↓- MARK: Mark comment | ||
""") | ||
], | ||
corrections: [ | ||
Example("//↓Something"): Example("// Something"), | ||
Example("//↓- MARK: Mark comment"): Example("// - MARK: Mark comment"), | ||
Example(""" | ||
/// Multiline triple-slash | ||
///↓This line is incorrect, though | ||
"""): Example(""" | ||
/// Multiline triple-slash | ||
/// This line is incorrect, though | ||
"""), | ||
Example(""" | ||
func a() { | ||
//↓This needs refactoring | ||
print("Something") | ||
} | ||
//↓We should improve above function | ||
"""): Example(""" | ||
func a() { | ||
// This needs refactoring | ||
print("Something") | ||
} | ||
// We should improve above function | ||
""") | ||
] | ||
) | ||
|
||
public func violationRanges(in file: SwiftLintFile) -> [NSRange] { | ||
// Find all comment tokens in the file and regex search them for violations | ||
let commentTokens = file.syntaxMap.tokens.filter { token in | ||
guard let kind = token.kind else { return false } | ||
return SyntaxKind.commentKinds.contains(kind) | ||
} | ||
return commentTokens | ||
.compactMap { (token: SwiftLintSyntaxToken) -> [NSRange]? in | ||
return file.stringView | ||
.substringWithByteRange(token.range) | ||
.map(StringView.init) | ||
.map { commentBody in | ||
// Look for 2-3 slash characters followed immediately by a non-whitespace, non-slash | ||
// character (this is a violation) | ||
regex(#"^(\/){2,3}[^\s\/]"#).matches(in: commentBody, options: .anchored).compactMap { result in | ||
// Set the location to be directly before the first non-slash, | ||
// non-whitespace character which was matched | ||
return file.stringView.byteRangeToNSRange( | ||
ByteRange( | ||
// Safe to mix NSRange offsets with byte offsets here because the regex can't | ||
// contain multi-byte characters | ||
location: ByteCount(token.range.lowerBound.value + result.range.upperBound - 1), | ||
length: 0 | ||
) | ||
) | ||
} | ||
} | ||
} | ||
.flatMap { $0 } | ||
} | ||
|
||
public func validate(file: SwiftLintFile) -> [StyleViolation] { | ||
return violationRanges(in: file).map { range in | ||
StyleViolation( | ||
ruleDescription: Self.description, | ||
severity: configuration.severity, | ||
location: Location(file: file, characterOffset: range.location) | ||
) | ||
} | ||
} | ||
|
||
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? { | ||
return (violationRange, " ") | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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