Skip to content

Commit 40fb93b

Browse files
committed
Added old comment style check,
1 parent c5f72f9 commit 40fb93b

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

Moulinette.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
2081734F1EEB27ED0077106A /* APIEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2081734E1EEB27ED0077106A /* APIEnvironment.swift */; };
163163
208173511EEB27F90077106A /* Endpoints.swift in Sources */ = {isa = PBXBuildFile; fileRef = 208173501EEB27F90077106A /* Endpoints.swift */; };
164164
209696C01EE9E52B0035C7AE /* CompletionWeakSwiftRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 209696BF1EE9E52B0035C7AE /* CompletionWeakSwiftRule.swift */; };
165+
209B720F20AB882000E7E0DE /* OldCommentStyleCheck.swift in Sources */ = {isa = PBXBuildFile; fileRef = 209B720E20AB882000E7E0DE /* OldCommentStyleCheck.swift */; };
165166
20C40C581ED8D47C00695CA7 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20C40C571ED8D47C00695CA7 /* main.swift */; };
166167
20CE93F31F4CC202000A5CF5 /* AppIconSwiftRuleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20CE93F21F4CC202000A5CF5 /* AppIconSwiftRuleTests.swift */; };
167168
20CE93F41F4CC276000A5CF5 /* AppIconSwiftRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = 200FC7341F4CBB92004335D8 /* AppIconSwiftRule.swift */; };
@@ -300,6 +301,7 @@
300301
208173501EEB27F90077106A /* Endpoints.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Endpoints.swift; sourceTree = "<group>"; };
301302
209696BF1EE9E52B0035C7AE /* CompletionWeakSwiftRule.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CompletionWeakSwiftRule.swift; sourceTree = "<group>"; };
302303
209696C11EE9F4270035C7AE /* RulePriority.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RulePriority.swift; sourceTree = "<group>"; };
304+
209B720E20AB882000E7E0DE /* OldCommentStyleCheck.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OldCommentStyleCheck.swift; sourceTree = "<group>"; };
303305
20C40C541ED8D47C00695CA7 /* Moulinette */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Moulinette; sourceTree = BUILT_PRODUCTS_DIR; };
304306
20C40C571ED8D47C00695CA7 /* main.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = main.swift; sourceTree = "<group>"; };
305307
20CE93F21F4CC202000A5CF5 /* AppIconSwiftRuleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppIconSwiftRuleTests.swift; sourceTree = "<group>"; };
@@ -386,6 +388,7 @@
386388
children = (
387389
201604C42092603200B7F191 /* ContextCheck.swift */,
388390
201604C52092603200B7F191 /* BracketContextCheck.swift */,
391+
209B720E20AB882000E7E0DE /* OldCommentStyleCheck.swift */,
389392
);
390393
path = Checks;
391394
sourceTree = "<group>";
@@ -838,6 +841,7 @@
838841
207CE2C920925ECB0032B3B5 /* LocalizationSwiftRule.swift in Sources */,
839842
4C382C771FE80EE800C0154E /* GitCheckConfigurationSwiftRule.swift in Sources */,
840843
207CE2DE20925F020032B3B5 /* FileCorrection.swift in Sources */,
844+
209B720F20AB882000E7E0DE /* OldCommentStyleCheck.swift in Sources */,
841845
20160509209260EF00B7F191 /* GradeType.swift in Sources */,
842846
2016050A209260EF00B7F191 /* CodeRuleType.swift in Sources */,
843847
20160508209260EF00B7F191 /* RuleCollection.swift in Sources */,

Moulinette/Rules/SinglePublicInternalSwiftRule.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class SinglePublicInternalSwiftRule: SwiftRule {
1616
let priority: RulePriority = .low
1717

1818
private var type: FileType?
19+
private var commentCheck = OldCommentStyleCheck()
1920

2021
private lazy var auditGrader: AuditGrader = {
2122
return PIOSAuditGrader(priority: self.priority)
@@ -24,7 +25,8 @@ final class SinglePublicInternalSwiftRule: SwiftRule {
2425
func run(projectData: ProjectData) -> AuditGrade {
2526
for (fileName, fileComponents) in projectData.applicationComponents.swiftFiles {
2627
fileComponents.forEach {
27-
if let fileType = FileType.type(fileLine: $0) {
28+
commentCheck.check(line: $0)
29+
if !commentCheck.insideCommentContext, let fileType = FileType.type(fileLine: $0) {
2830
if let type = type,
2931
!$0.isComment(),
3032
!fileName.contains("Constants") && !$0.contains("CodingKey") {

Moulinette/Rules/TypeInferenceSwiftRule.swift

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import Foundation
1010

11-
final class TypeInferenceSwiftRule: SwiftRule {
11+
final class TypeInferenceSwiftRule: CorrectableSwiftRule {
1212

1313
let description: String = "Unnecessary Type inference"
1414
let nameId = "type_inference"
@@ -32,6 +32,26 @@ final class TypeInferenceSwiftRule: SwiftRule {
3232
}
3333
return auditGrader.generateGrade()
3434
}
35+
36+
func correct(projectData: ProjectData) -> [FileCorrection] {
37+
return auditGrader.violations.compactMap({ (violation) -> FileCorrection? in
38+
guard let lineNumber = violation.lineNumber,
39+
let fileComponents = projectData.applicationComponents.components[violation.fileName],
40+
let index = violation.componentIndex,
41+
let typeString = fileComponents[index].stringBetween(startString: ":", endString: "="),
42+
let colonIndex = fileComponents[index].index(of: ":") else {
43+
return nil
44+
}
45+
var customString = fileComponents[index].replacingOccurrences(of: typeString, with: "")
46+
customString.remove(at: colonIndex)
47+
customString.insert(" ", at: colonIndex)
48+
return FileCorrection(fileName: violation.fileName,
49+
lineNumber: lineNumber,
50+
customString: customString,
51+
lineInsertions: [],
52+
lineDeletions: [])
53+
})
54+
}
3555
}
3656

3757
private extension TypeInferenceSwiftRule {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// OldCommentStyleCheck.swift
3+
// Moulinette
4+
//
5+
// Created by Jonathan Samudio on 5/15/18.
6+
// Copyright © 2018 Prolific Interactive. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
final class OldCommentStyleCheck {
12+
13+
private(set) var insideCommentContext = false
14+
15+
func check(line: String) {
16+
if line.contains("/**") {
17+
insideCommentContext = true
18+
return
19+
}
20+
21+
if line.contains("*/") {
22+
insideCommentContext = false
23+
}
24+
}
25+
}

Moulinette/Utility/Extensions/String+SwiftCode.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ extension String {
7878
///
7979
/// - Returns: True if the string containing Swift code is a comment.
8080
func isComment() -> Bool {
81-
return Regex.commentRegex().hasMatch(input: self) || self.components(separatedBy: " ").contains("*")
81+
return Regex.commentRegex().hasMatch(input: self) ||
82+
self.components(separatedBy: " ").contains("*") ||
83+
self.components(separatedBy: " ").contains("-")
8284
}
8385

8486
/// Returns true if the string containing Swift code has a pivotal story link.

0 commit comments

Comments
 (0)