@@ -15,7 +15,8 @@ struct InvalidSwiftLintCommandRule: Rule, SourceKitFreeRule {
15
15
Example ( " // swiftlint:disable:previous unused_import " ) ,
16
16
Example ( " // swiftlint:disable:this unused_import " ) ,
17
17
Example ( " //swiftlint:disable:this unused_import " ) ,
18
- Example ( " _ = \" 🤵🏼♀️ \" // swiftlint:disable:this unused_import " ) ,
18
+ Example ( " _ = \" 🤵🏼♀️ \" // swiftlint:disable:this unused_import " , excludeFromDocumentation: true ) ,
19
+ Example ( " _ = \" 🤵🏼♀️ 🤵🏼♀️ \" // swiftlint:disable:this unused_import " , excludeFromDocumentation: true ) ,
19
20
] ,
20
21
triggeringExamples: [
21
22
Example ( " // ↓swiftlint: " ) ,
@@ -33,6 +34,7 @@ struct InvalidSwiftLintCommandRule: Rule, SourceKitFreeRule {
33
34
Example ( " // ↓swiftlint:enable: " ) ,
34
35
Example ( " // ↓swiftlint:disable: unused_import " ) ,
35
36
Example ( " // s↓swiftlint:disable unused_import " ) ,
37
+ Example ( " // 🤵🏼♀️swiftlint:disable unused_import " , excludeFromDocumentation: true ) ,
36
38
] . skipWrappingInCommentTests ( )
37
39
)
38
40
@@ -42,15 +44,13 @@ struct InvalidSwiftLintCommandRule: Rule, SourceKitFreeRule {
42
44
43
45
private func badPrefixViolations( in file: SwiftLintFile ) -> [ StyleViolation ] {
44
46
( file. commands + file. invalidCommands) . compactMap { command in
45
- if let precedingCharacter = command. precedingCharacter ( in: file) ? . unicodeScalars. first,
46
- !CharacterSet. whitespaces. union ( CharacterSet ( charactersIn: " / " ) ) . contains ( precedingCharacter) {
47
- return styleViolation (
47
+ command. isPrecededByInvalidCharacter ( in: file)
48
+ ? styleViolation (
48
49
for: command,
49
50
in: file,
50
51
reason: " swiftlint command should be preceded by whitespace or a comment character "
51
52
)
52
- }
53
- return nil
53
+ : nil
54
54
}
55
55
}
56
56
@@ -61,53 +61,26 @@ struct InvalidSwiftLintCommandRule: Rule, SourceKitFreeRule {
61
61
}
62
62
63
63
private func styleViolation( for command: Command , in file: SwiftLintFile , reason: String ) -> StyleViolation {
64
- let character = command. startingCharacterPosition ( in: file)
65
- let characterOffset = character. flatMap {
66
- if let line = command. lineOfCommand ( in: file) {
67
- return line. distance ( from: line. startIndex, to: $0)
68
- }
69
- return nil
70
- }
71
- return StyleViolation (
64
+ StyleViolation (
72
65
ruleDescription: Self . description,
73
66
severity: configuration. severity,
74
- location: Location ( file: file. path, line: command. line, character: characterOffset ) ,
67
+ location: Location ( file: file. path, line: command. line, character: command . character ) ,
75
68
reason: reason
76
69
)
77
70
}
78
71
}
79
72
80
73
private extension Command {
81
- func lineOfCommand( in file: SwiftLintFile ) -> String ? {
82
- guard line > 0 , line <= file. lines. count else {
83
- return nil
84
- }
85
- return file. lines [ line - 1 ] . content
86
- }
87
-
88
- func startingCharacterPosition( in file: SwiftLintFile ) -> String . Index ? {
89
- guard let line = lineOfCommand ( in: file) , line. isNotEmpty else {
90
- return nil
91
- }
92
- if let commandIndex = line. range ( of: " swiftlint: " ) ? . lowerBound {
93
- let distance = line. distance ( from: line. startIndex, to: commandIndex)
94
- return line. index ( line. startIndex, offsetBy: distance + 1 )
95
- }
96
- if let character {
97
- return line. index ( line. startIndex, offsetBy: character)
98
- }
99
- return nil
100
- }
101
-
102
- func precedingCharacter( in file: SwiftLintFile ) -> Character ? {
103
- guard let startingCharacterPosition = startingCharacterPosition ( in: file) ,
104
- let line = lineOfCommand ( in: file) else {
105
- return nil
74
+ func isPrecededByInvalidCharacter( in file: SwiftLintFile ) -> Bool {
75
+ guard line > 0 , let character, character > 1 , line <= file. lines. count else {
76
+ return false
106
77
}
107
- guard line. distance ( from: line. startIndex, to: startingCharacterPosition) > 2 else {
108
- return nil
78
+ let line = file. lines [ line - 1 ] . content
79
+ guard line. count > character,
80
+ let char = line [ line. index ( line. startIndex, offsetBy: character - 2 ) ] . unicodeScalars. first else {
81
+ return false
109
82
}
110
- return line [ line . index ( startingCharacterPosition , offsetBy : - 2 ) ... ] . first
83
+ return !CharacterSet . whitespaces . union ( CharacterSet ( charactersIn : " / " ) ) . contains ( char )
111
84
}
112
85
113
86
func invalidReason( ) -> String ? {
0 commit comments