Merge pull request #242 from marcelofabri/legacy-constructors

Prefer Swift constructors to legacy ones
This commit is contained in:
JP Simard
2015-11-29 16:51:43 -08:00
5 changed files with 58 additions and 0 deletions
+4
View File
@@ -6,6 +6,10 @@
##### Enhancements
* Add legacy constructor rule.
[Marcelo Fabri](https://github.com/marcelofabri)
[#202](https://github.com/realm/SwiftLint/issues/202)
* The `VariableNameRule` now allows variable names when the entire name is
capitalized. This allows stylistic usage common in cases like `URL` and other
acronyms.
@@ -128,6 +128,7 @@ public struct Configuration {
ForceCastRule(),
ForceTryRule(),
LeadingWhitespaceRule(),
LegacyConstructorRule(),
NestingRule(),
OpeningBraceRule(),
OperatorFunctionWhitespaceRule(),
@@ -0,0 +1,45 @@
//
// LegacyConstructorRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 29/11/15.
// Copyright © 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct LegacyConstructorRule: Rule {
public init() {}
public static let description = RuleDescription(
identifier: "legacy_constructor",
name: "Legacy Constructor",
description: "Swift constructors are preferred over legacy convenience functions.",
nonTriggeringExamples: [
"CGPoint(x: 10, y: 10)",
"CGSize(width: 10, height: 10)",
"CGRect(x: 0, y: 0, width: 10, height: 10)",
"CGVector(dx: 10, dy: 10)",
"NSRange(location: 10, length: 1)",
],
triggeringExamples: [
"CGPointMake(10, 10)",
"CGSizeMake(10, 10)",
"CGRectMake(0, 0, 10, 10)",
"CGVectorMake(10, 10)",
"NSMakeRange(10, 1)",
]
)
public func validateFile(file: File) -> [StyleViolation] {
let constructors = ["CGRectMake", "CGPointMake", "CGSizeMake", "CGVectorMake",
"NSMakeRange"]
let pattern = "\\b(" + constructors.joinWithSeparator("|") + ")\\b"
return file.matchPattern(pattern, withSyntaxKinds: [.Identifier]).map {
StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, offset: $0.location))
}
}
}
@@ -97,4 +97,8 @@ class StringRuleTests: XCTestCase {
func testTrailingSemicolon() {
verifyRule(TrailingSemicolonRule.description)
}
func testLegacyConstructor() {
verifyRule(LegacyConstructorRule.description)
}
}
+4
View File
@@ -18,6 +18,7 @@
D0D1217819E87B05005E4BAA /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; };
D0E7B65319E9C6AD00EDBA4D /* SwiftLintFramework.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0D1216D19E87B05005E4BAA /* SwiftLintFramework.framework */; };
D0E7B65619E9C76900EDBA4D /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0D1211B19E87861005E4BAA /* main.swift */; };
D44AD2761C0AA5350048F7B0 /* LegacyConstructorRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = D44AD2741C0AA3730048F7B0 /* LegacyConstructorRule.swift */; };
E57B23C11B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = E57B23C01B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift */; };
E809EDA11B8A71DF00399043 /* Configuration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E809EDA01B8A71DF00399043 /* Configuration.swift */; };
E809EDA31B8A73FB00399043 /* ConfigurationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E809EDA21B8A73FB00399043 /* ConfigurationTests.swift */; };
@@ -166,6 +167,7 @@
D0D1217719E87B05005E4BAA /* SwiftLintFrameworkTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftLintFrameworkTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
D0D1217D19E87B05005E4BAA /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
D0E7B63219E9C64500EDBA4D /* swiftlint.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = swiftlint.app; sourceTree = BUILT_PRODUCTS_DIR; };
D44AD2741C0AA3730048F7B0 /* LegacyConstructorRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LegacyConstructorRule.swift; sourceTree = "<group>"; };
E57B23C01B1D8BF000DEA512 /* ReturnArrowWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReturnArrowWhitespaceRule.swift; sourceTree = "<group>"; };
E5A167C81B25A0B000CF2D03 /* OperatorFunctionWhitespaceRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OperatorFunctionWhitespaceRule.swift; sourceTree = "<group>"; };
E809EDA01B8A71DF00399043 /* Configuration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Configuration.swift; sourceTree = "<group>"; };
@@ -441,6 +443,7 @@
E816194D1BFBFEAB00946723 /* ForceTryRule.swift */,
E88DEA8F1B099A3100A66CB0 /* FunctionBodyLengthRule.swift */,
E88DEA7D1B098F2A00A66CB0 /* LeadingWhitespaceRule.swift */,
D44AD2741C0AA3730048F7B0 /* LegacyConstructorRule.swift */,
E88DEA7B1B098D7D00A66CB0 /* LineLengthRule.swift */,
E88DEA951B099CF200A66CB0 /* NestingRule.swift */,
692B1EB11BD7E00F00EAABFF /* OpeningBraceRule.swift */,
@@ -667,6 +670,7 @@
69F88BF71BDA38A6005E7CAE /* OpeningBraceRule.swift in Sources */,
E80E018D1B92C0F60078EB70 /* Command.swift in Sources */,
E88198571BEA953300333A11 /* ForceCastRule.swift in Sources */,
D44AD2761C0AA5350048F7B0 /* LegacyConstructorRule.swift in Sources */,
83D71E281B131ECE000395DE /* RuleDescription.swift in Sources */,
E812249C1B04FADC001783D2 /* Linter.swift in Sources */,
CAF900151BED8B17006A371D /* VariableNameMinLengthRule.swift in Sources */,