@@ -82,25 +82,58 @@ public class RuleMask {
82
82
return nil
83
83
}
84
84
85
+ /// Returns the list of line comments in the given trivia that are on a line by themselves
86
+ /// (excluding leading whitespace).
87
+ ///
88
+ /// - Parameters:
89
+ /// - trivia: The trivia collection to scan for comments.
90
+ /// - isFirstToken: True if the trivia came from the first token in the file.
91
+ /// - Returns: The list of lone line comments from the trivia.
92
+ private func loneLineComments( in trivia: Trivia , isFirstToken: Bool ) -> [ String ] {
93
+ var currentComment : String ? = nil
94
+ var lineComments = [ String] ( )
95
+
96
+ for piece in trivia. reversed ( ) {
97
+ switch piece {
98
+ case . lineComment( let text) :
99
+ currentComment = text
100
+ case . spaces, . tabs:
101
+ break // Intentionally do nothing.
102
+ case . carriageReturnLineFeeds, . carriageReturns, . newlines:
103
+ if let text = currentComment {
104
+ lineComments. insert ( text, at: 0 )
105
+ currentComment = nil
106
+ }
107
+ default :
108
+ // If anything other than spaces intervened between the line comment and a newline, then the
109
+ // comment isn't on a line by itself, so reset our state.
110
+ currentComment = nil
111
+ }
112
+ }
113
+
114
+ // For the first token in the file, there may not be a newline preceding the first line comment,
115
+ // so check for that here.
116
+ if isFirstToken, let text = currentComment {
117
+ lineComments. insert ( text, at: 0 )
118
+ }
119
+
120
+ return lineComments
121
+ }
122
+
85
123
/// Generate the dictionary (ruleMap) by walking the syntax tokens.
86
124
private func generateDictionary( _ node: Syntax ) {
87
125
var disableStart : [ String : Int ] = [ : ]
88
126
var enableStart : [ String : Int ] = [ : ]
89
127
90
- for token in node. tokens {
91
- guard let leadingTrivia = token. leadingTrivia else { continue }
128
+ var isFirstToken = true
92
129
93
- // Flags must be on lines by themselves: not at the end of an existing line.
94
- var firstPiece = true
130
+ for token in node . tokens {
131
+ defer { isFirstToken = false }
95
132
96
- for piece in leadingTrivia {
97
- guard case . lineComment( let text) = piece else {
98
- firstPiece = false
99
- continue
100
- }
101
- guard !firstPiece else { continue }
133
+ guard let leadingTrivia = token. leadingTrivia else { continue }
102
134
103
- if let ruleName = getRule ( regex: disableRegex, text: text) {
135
+ for comment in loneLineComments ( in: leadingTrivia, isFirstToken: isFirstToken) {
136
+ if let ruleName = getRule ( regex: disableRegex, text: comment) {
104
137
guard !disableStart. keys. contains ( ruleName) else { continue }
105
138
guard let disableStartLine = getLine ( token) else { continue }
106
139
@@ -113,7 +146,7 @@ public class RuleMask {
113
146
114
147
disableStart [ ruleName] = disableStartLine
115
148
}
116
- else if let ruleName = getRule ( regex: enableRegex, text: text ) {
149
+ else if let ruleName = getRule ( regex: enableRegex, text: comment ) {
117
150
guard !enableStart. keys. contains ( ruleName) else { continue }
118
151
guard let enableStartLine = getLine ( token) else { continue }
119
152
@@ -126,8 +159,6 @@ public class RuleMask {
126
159
127
160
enableStart [ ruleName] = enableStartLine
128
161
}
129
-
130
- firstPiece = false
131
162
}
132
163
}
133
164
0 commit comments