forked from swiftlang/swift-format
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuleMask.swift
284 lines (252 loc) · 11.2 KB
/
RuleMask.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Foundation
import SwiftSyntax
/// This class takes the raw source text and scans through it searching for comments that instruct
/// the formatter to change the status of rules for the following node. The comments may include no
/// rule names to affect all rules, a single rule name to affect that rule, or a comma delimited
/// list of rule names to affect a number of rules. Ignore is the only supported operation.
///
/// 1. | // swift-format-ignore
/// 2. | let a = 123
/// Ignores all rules for line 2.
///
/// 1. | // swift-format-ignore-file
/// 2. | let a = 123
/// 3. | class Foo { }
/// Ignores all rules for an entire file (lines 2-3).
///
/// 1. | // swift-format-ignore: RuleName
/// 2. | let a = 123
/// Ignores `RuleName` for line 2.
///
/// 1. | // swift-format-ignore: RuleName, OtherRuleName
/// 2. | let a = 123
/// Ignores `RuleName` and `OtherRuleName` for line 2.
///
/// 1. | // swift-format-ignore-file: RuleName
/// 2. | let a = 123
/// 3. | class Foo { }
/// Ignores `RuleName` for the entire file (lines 2-3).
///
/// 1. | // swift-format-ignore-file: RuleName, OtherRuleName
/// 2. | let a = 123
/// 3. | class Foo { }
/// Ignores `RuleName` and `OtherRuleName` for the entire file (lines 2-3).
///
/// The rules themselves reference RuleMask to see if it is disabled for the line it is currently
/// examining.
@_spi(Testing)
public class RuleMask {
/// Stores the source ranges in which all rules are ignored.
private var allRulesIgnoredRanges: [SourceRange] = []
/// Map of rule names to list ranges in the source where the rule is ignored.
private var ruleMap: [String: [SourceRange]] = [:]
/// Used to compute line numbers of syntax nodes.
private let sourceLocationConverter: SourceLocationConverter
/// Creates a `RuleMask` that can specify whether a given rule's status is explicitly modified at
/// a location obtained from the `SourceLocationConverter`.
///
/// Ranges in the source where rules' statuses are modified are pre-computed during init so that
/// lookups later don't require parsing the source.
public init(syntaxNode: Syntax, sourceLocationConverter: SourceLocationConverter) {
self.sourceLocationConverter = sourceLocationConverter
computeIgnoredRanges(in: syntaxNode)
}
/// Computes the ranges in the given node where the status of rules are explicitly modified.
private func computeIgnoredRanges(in node: Syntax) {
let visitor = RuleStatusCollectionVisitor(sourceLocationConverter: sourceLocationConverter)
visitor.walk(node)
allRulesIgnoredRanges = visitor.allRulesIgnoredRanges
ruleMap = visitor.ruleMap
}
/// Returns the `RuleState` for the given rule at the provided location.
public func ruleState(_ rule: String, at location: SourceLocation) -> RuleState {
if allRulesIgnoredRanges.contains(where: { $0.contains(location) }) {
return .disabled
}
if let ignoredRanges = ruleMap[rule] {
return ignoredRanges.contains { $0.contains(location) } ? .disabled : .default
}
return .default
}
}
extension SourceRange {
/// Returns whether the range includes the given location.
fileprivate func contains(_ location: SourceLocation) -> Bool {
return start.offset <= location.offset && end.offset >= location.offset
}
}
/// Represents the kind of ignore directive encountered in the source.
enum IgnoreDirective: CustomStringConvertible {
typealias RegexExpression = Regex<(Substring, ruleNames: Substring?)>
/// A node-level directive that disables rules for the following node and its children.
case node
/// A file-level directive that disables rules for the entire file.
case file
var description: String {
switch self {
case .node:
return "swift-format-ignore"
case .file:
return "swift-format-ignore-file"
}
}
/// Regex pattern to match an ignore directive comment.
/// - Captures rule names when `:` is present.
///
/// Note: We are using a string-based regex instead of a regex literal (`#/regex/#`)
/// because Windows did not have full support for regex literals until Swift 5.10.
fileprivate func makeRegex() -> RegexExpression {
let pattern = #"^\s*\/\/\s*"# + description + #"(?:\s*:\s*(?<ruleNames>.+))?$"#
return try! Regex(pattern).matchingSemantics(.unicodeScalar)
}
}
/// A syntax visitor that finds `SourceRange`s of nodes that have rule status modifying comment
/// directives. The changes requested in each comment is parsed and collected into a map to support
/// status lookup per rule name.
///
/// The rule status comment directives implementation intentionally supports exactly the same nodes
/// as `TokenStreamCreator` to disable pretty printing. This ensures ignore comments for pretty
/// printing and for rules are as consistent as possible.
fileprivate class RuleStatusCollectionVisitor: SyntaxVisitor {
/// Describes the possible matches for ignore directives, in comments.
enum RuleStatusDirectiveMatch {
/// There is a directive that applies to all rules.
case all
/// There is a directive that applies to a number of rules. The names of the rules are provided
/// in `ruleNames`.
case subset(ruleNames: [String])
}
/// Cached regex object for ignoring rules at the node.
private static let ignoreRegex: IgnoreDirective.RegexExpression = IgnoreDirective.node.makeRegex()
/// Cached regex object for ignoring rules at the file.
private static let ignoreFileRegex: IgnoreDirective.RegexExpression = IgnoreDirective.file.makeRegex()
/// Computes source locations and ranges for syntax nodes in a source file.
private let sourceLocationConverter: SourceLocationConverter
/// Stores the source ranges in which all rules are ignored.
var allRulesIgnoredRanges: [SourceRange] = []
/// Map of rule names to list ranges in the source where the rule is ignored.
var ruleMap: [String: [SourceRange]] = [:]
init(sourceLocationConverter: SourceLocationConverter) {
self.sourceLocationConverter = sourceLocationConverter
super.init(viewMode: .sourceAccurate)
}
// MARK: - Syntax Visitation Methods
override func visit(_ node: SourceFileSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
let sourceRange = node.sourceRange(
converter: sourceLocationConverter,
afterLeadingTrivia: false,
afterTrailingTrivia: true
)
return appendRuleStatus(from: firstToken, of: sourceRange, using: Self.ignoreFileRegex)
}
override func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
let sourceRange = node.sourceRange(converter: sourceLocationConverter)
return appendRuleStatus(from: firstToken, of: sourceRange, using: Self.ignoreRegex)
}
override func visit(_ node: MemberBlockItemSyntax) -> SyntaxVisitorContinueKind {
guard let firstToken = node.firstToken(viewMode: .sourceAccurate) else {
return .visitChildren
}
let sourceRange = node.sourceRange(converter: sourceLocationConverter)
return appendRuleStatus(from: firstToken, of: sourceRange, using: Self.ignoreRegex)
}
// MARK: - Helper Methods
/// Searches for comments on the given token that explicitly modify the status of rules and adds
/// them to the appropriate collection of those changes.
///
/// - Parameters:
/// - token: A token that may have comments that modify the status of rules.
/// - sourceRange: The range covering the node to which `token` belongs. If an ignore directive
/// is found among the comments, this entire range is used to ignore the specified rules.
/// - regex: The regular expression used to detect ignore directives.
private func appendRuleStatus(
from token: TokenSyntax,
of sourceRange: SourceRange,
using regex: IgnoreDirective.RegexExpression
) -> SyntaxVisitorContinueKind {
let isFirstInFile = token.previousToken(viewMode: .sourceAccurate) == nil
let comments = loneLineComments(in: token.leadingTrivia, isFirstToken: isFirstInFile)
for comment in comments {
guard let matchResult = ruleStatusDirectiveMatch(in: comment, using: regex) else { continue }
switch matchResult {
case .all:
allRulesIgnoredRanges.append(sourceRange)
// All rules are ignored for the entire node and its children. Any ignore comments in the
// node's children are irrelevant because all rules are suppressed by this node.
return .skipChildren
case .subset(let ruleNames):
ruleNames.forEach { ruleMap[$0, default: []].append(sourceRange) }
break
}
}
return .visitChildren
}
/// Checks if a comment containing the given text matches a rule status directive. When it does
/// match, its contents (e.g. list of rule names) are returned.
private func ruleStatusDirectiveMatch(
in text: String,
using regex: IgnoreDirective.RegexExpression
) -> RuleStatusDirectiveMatch? {
guard let match = text.firstMatch(of: regex) else {
return nil
}
guard let matchedRuleNames = match.output.ruleNames else {
return .all
}
let rules = matchedRuleNames.split(separator: ",")
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { $0.count > 0 }
return .subset(ruleNames: rules)
}
/// Returns the list of line comments in the given trivia that are on a line by themselves
/// (excluding leading whitespace).
///
/// - Parameters:
/// - trivia: The trivia collection to scan for comments.
/// - isFirstToken: True if the trivia came from the first token in the file.
/// - Returns: The list of lone line comments from the trivia.
private func loneLineComments(in trivia: Trivia, isFirstToken: Bool) -> [String] {
var currentComment: String? = nil
var lineComments = [String]()
for piece in trivia.reversed() {
switch piece {
case .lineComment(let text):
currentComment = text
case .spaces, .tabs:
break // Intentionally do nothing.
case .carriageReturnLineFeeds, .carriageReturns, .newlines:
if let text = currentComment {
lineComments.insert(text, at: 0)
currentComment = nil
}
default:
// If anything other than spaces intervened between the line comment and a newline, then the
// comment isn't on a line by itself, so reset our state.
currentComment = nil
}
}
// For the first token in the file, there may not be a newline preceding the first line comment,
// so check for that here.
if isFirstToken, let text = currentComment {
lineComments.insert(text, at: 0)
}
return lineComments
}
}