-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add LegacyConstantRule #344
Changes from 1 commit
9e66449
e13907e
8584f0b
69477da
8e08cf7
cf0255a
e79eba5
98f4ae3
63ae70e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// | ||
// LegacyConstantRule.swift | ||
// SwiftLint | ||
// | ||
// Created by Aaron McTavish on 12/01/2016. | ||
// Copyright © 2016 Realm. All rights reserved. | ||
// | ||
|
||
import SourceKittenFramework | ||
import CoreGraphics | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think you need this import. |
||
|
||
public struct LegacyConstantRule: CorrectableRule { | ||
public static let description = RuleDescription( | ||
identifier: "legacy_constant", | ||
name: "Legacy Constant", | ||
description: "Struct-scoped constants are preferred over legacy global constants.", | ||
nonTriggeringExamples: [ | ||
"CGRect.infinite", | ||
"CGPoint.zero", | ||
"CGRect.zero", | ||
"CGSize.zero", | ||
"CGRect.null" | ||
], | ||
triggeringExamples: [ | ||
"↓CGRectInfinite", | ||
"↓CGPointZero", | ||
"↓CGRectZero", | ||
"↓CGSizeZero", | ||
"↓CGRectNull" | ||
], | ||
corrections: [ | ||
"CGRectInfinite\n": "CGRect.infinite\n", | ||
"CGPointZero\n": "CGPoint.zero\n", | ||
"CGRectZero\n": "CGRect.zero\n", | ||
"CGSizeZero\n": "CGSize.zero\n", | ||
"CGRectNull\n": "CGRect.null\n" | ||
] | ||
) | ||
|
||
public func validateFile(file: File) -> [StyleViolation] { | ||
let constants = ["CGRectInfinite", "CGPointZero", "CGRectZero", "CGSizeZero", | ||
"CGRectNull"] | ||
|
||
let pattern = "\\b(" + constants.joinWithSeparator("|") + ")\\b" | ||
|
||
return file.matchPattern(pattern, withSyntaxKinds: [.Identifier]).map { | ||
StyleViolation(ruleDescription: self.dynamicType.description, | ||
location: Location(file: file, characterOffset: $0.location)) | ||
} | ||
} | ||
|
||
public func correctFile(file: File) -> [Correction] { | ||
let patterns = [ | ||
"CGRectInfinite": "CGRect.infinite", | ||
"CGPointZero": "CGPoint.zero", | ||
"CGRectZero": "CGRect.zero", | ||
"CGSizeZero": "CGSize.zero", | ||
"CGRectNull": "CGRect.null" | ||
] | ||
|
||
let description = self.dynamicType.description | ||
var corrections = [Correction]() | ||
var contents = file.contents | ||
|
||
for (pattern, template) in patterns { | ||
let matches = file.matchPattern(pattern, excludingSyntaxKinds: [.Comment]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not filter on And here you're scanning the entire file for each pattern, whereas |
||
|
||
let regularExpression = regex(pattern) | ||
for range in matches.reverse() { | ||
contents = regularExpression.stringByReplacingMatchesInString(contents, | ||
options: [], range: range, withTemplate: template) | ||
let location = Location(file: file, characterOffset: range.location) | ||
corrections.append(Correction(ruleDescription: description, location: location)) | ||
} | ||
} | ||
|
||
file.write(contents) | ||
return corrections | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing 2 trailing spaces as described in https://github.com/realm/SwiftLint/blob/master/CONTRIBUTING.md#tracking-changes.