Skip to content
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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
[Aaron McTavish](https://github.com/aamctustwo)
[#202](https://github.com/realm/SwiftLint/issues/202)

* Add LegacyConstantRule.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Aaron McTavish](https://github.com/aamctustwo)
[#319](https://github.com/realm/SwiftLint/issues/319)

##### Bug Fixes

* AutoCorrect for TrailingNewlineRule only removes at most one line.
Expand Down
1 change: 1 addition & 0 deletions Source/SwiftLintFramework/Models/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public struct Configuration: Equatable {
ForceCastRule(),
ForceTryRule(),
LeadingWhitespaceRule(),
LegacyConstantRule(),
LegacyConstructorRule(),
NestingRule(),
OpeningBraceRule(),
Expand Down
80 changes: 80 additions & 0 deletions Source/SwiftLintFramework/Rules/LegacyConstantRule.swift
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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not filter on .Identifier like you're doing in validateFile? As it stands, the string references above would get corrected and that's certainly not what we want.

And here you're scanning the entire file for each pattern, whereas validateFile just does it once for all patterns.


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
}
}
4 changes: 4 additions & 0 deletions Source/SwiftLintFrameworkTests/StringRuleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,8 @@ class StringRuleTests: XCTestCase {
func testConditionalBindingCascade() {
verifyRule(ConditionalBindingCascadeRule.description)
}

func testLegacyConstant() {
verifyRule(LegacyConstantRule.description)
}
}
4 changes: 4 additions & 0 deletions SwiftLint.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
006ECFC41C44E99E00EF6364 /* LegacyConstantRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 006ECFC31C44E99E00EF6364 /* LegacyConstantRule.swift */; };
00970C751C3FC3A0007C4A83 /* ConditionalBindingCascadeRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00970C741C3FC3A0007C4A83 /* ConditionalBindingCascadeRule.swift */; };
02FD8AEF1BFC18D60014BFFB /* ExtendedNSStringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02FD8AEE1BFC18D60014BFFB /* ExtendedNSStringTests.swift */; };
1F11B3CF1C252F23002E8FA8 /* ClosingBraceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F11B3CE1C252F23002E8FA8 /* ClosingBraceRule.swift */; };
Expand Down Expand Up @@ -143,6 +144,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
006ECFC31C44E99E00EF6364 /* LegacyConstantRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LegacyConstantRule.swift; sourceTree = "<group>"; };
00970C741C3FC3A0007C4A83 /* ConditionalBindingCascadeRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConditionalBindingCascadeRule.swift; sourceTree = "<group>"; };
02FD8AEE1BFC18D60014BFFB /* ExtendedNSStringTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ExtendedNSStringTests.swift; sourceTree = "<group>"; };
1F11B3CE1C252F23002E8FA8 /* ClosingBraceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ClosingBraceRule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -489,6 +491,7 @@
E816194D1BFBFEAB00946723 /* ForceTryRule.swift */,
E88DEA8F1B099A3100A66CB0 /* FunctionBodyLengthRule.swift */,
E88DEA7D1B098F2A00A66CB0 /* LeadingWhitespaceRule.swift */,
006ECFC31C44E99E00EF6364 /* LegacyConstantRule.swift */,
D44AD2741C0AA3730048F7B0 /* LegacyConstructorRule.swift */,
E88DEA7B1B098D7D00A66CB0 /* LineLengthRule.swift */,
E88DEA951B099CF200A66CB0 /* NestingRule.swift */,
Expand Down Expand Up @@ -759,6 +762,7 @@
E88198441BEA93D200333A11 /* ColonRule.swift in Sources */,
E809EDA11B8A71DF00399043 /* Configuration.swift in Sources */,
E8EA41171C2D1DBE004F9930 /* CheckstyleReporter.swift in Sources */,
006ECFC41C44E99E00EF6364 /* LegacyConstantRule.swift in Sources */,
E88DEA731B0984C400A66CB0 /* String+SwiftLint.swift in Sources */,
E88198591BEA95F100333A11 /* LeadingWhitespaceRule.swift in Sources */,
24E17F721B14BB3F008195BE /* File+Cache.swift in Sources */,
Expand Down