forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use SwiftSyntax visitor to parse commands (realm#3872)
* Cache SwiftSyntax syntax trees * Use SwiftSyntax visitor to parse commands * Update changelog entry * Cache commands
- Loading branch information
Showing
5 changed files
with
91 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import SwiftSyntax | ||
|
||
// MARK: - CommandVisitor | ||
|
||
/// Visits the source syntax tree to collect all SwiftLint-style comment commands. | ||
final class CommandVisitor: SyntaxVisitor { | ||
private(set) var commands: [Command] = [] | ||
let locationConverter: SourceLocationConverter | ||
|
||
init(locationConverter: SourceLocationConverter) { | ||
self.locationConverter = locationConverter | ||
super.init() | ||
} | ||
|
||
override func visitPost(_ node: TokenSyntax) { | ||
let leadingCommands = node.leadingTrivia.commands(offset: node.position, | ||
locationConverter: locationConverter) | ||
let trailingCommands = node.trailingTrivia.commands(offset: node.endPositionBeforeTrailingTrivia, | ||
locationConverter: locationConverter) | ||
self.commands.append(contentsOf: leadingCommands + trailingCommands) | ||
} | ||
} | ||
|
||
// MARK: - Private Helpers | ||
|
||
private extension Trivia { | ||
func commands(offset: AbsolutePosition, locationConverter: SourceLocationConverter) -> [Command] { | ||
var triviaOffset = SourceLength.zero | ||
var results: [Command] = [] | ||
for trivia in self { | ||
triviaOffset += trivia.sourceLength | ||
switch trivia { | ||
case .lineComment(let comment), .blockComment(let comment): | ||
if | ||
let lower = comment.range(of: "swiftlint:")?.lowerBound, | ||
case let actionString = String(comment[lower...]), | ||
case let end = locationConverter.location(for: offset + triviaOffset), | ||
let line = end.line, | ||
let column = end.column, | ||
let command = Command(actionString: actionString, line: line, character: column) | ||
{ | ||
results.append(command) | ||
} | ||
default: | ||
break | ||
} | ||
} | ||
|
||
return results | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters