|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | +@_spi(Rules) import SwiftFormat |
| 15 | +import SwiftParser |
| 16 | +import SwiftSyntax |
| 17 | + |
| 18 | +/// Collects information about rules in the formatter code base. |
| 19 | +final class PrettyPrintCollector { |
| 20 | + |
| 21 | + /// A list of all the format-only pretty-print categories found in the code base. |
| 22 | + var allPrettyPrinterCategories = Set<String>() |
| 23 | + |
| 24 | + /// Populates the internal collections with rules in the given directory. |
| 25 | + /// |
| 26 | + /// - Parameter url: The file system URL that should be scanned for rules. |
| 27 | + func collect(from url: URL) throws { |
| 28 | + // For each file in the Rules directory, find types that either conform to SyntaxLintRule or |
| 29 | + // inherit from SyntaxFormatRule. |
| 30 | + let fm = FileManager.default |
| 31 | + guard let rulesEnumerator = fm.enumerator(atPath: url.path) else { |
| 32 | + fatalError("Could not list the directory \(url.path)") |
| 33 | + } |
| 34 | + |
| 35 | + for baseName in rulesEnumerator { |
| 36 | + // Ignore files that aren't Swift source files. |
| 37 | + guard let baseName = baseName as? String, baseName.hasSuffix(".swift") else { continue } |
| 38 | + |
| 39 | + let fileURL = url.appendingPathComponent(baseName) |
| 40 | + let fileInput = try String(contentsOf: fileURL) |
| 41 | + let sourceFile = Parser.parse(source: fileInput) |
| 42 | + |
| 43 | + for statement in sourceFile.statements { |
| 44 | + let pp = self.detectPrettyPrintCategories(at: statement) |
| 45 | + allPrettyPrinterCategories.formUnion(pp) |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private func detectPrettyPrintCategories(at statement: CodeBlockItemSyntax) -> [String] { |
| 51 | + guard let enumDecl = statement.item.as(EnumDeclSyntax.self) else { |
| 52 | + return [] |
| 53 | + } |
| 54 | + |
| 55 | + if enumDecl.name.text == "PrettyPrintFindingCategory" { |
| 56 | + print("HIT") |
| 57 | + } |
| 58 | + |
| 59 | + // Make sure it has an inheritance clause. |
| 60 | + guard let inheritanceClause = enumDecl.inheritanceClause else { |
| 61 | + return [] |
| 62 | + } |
| 63 | + |
| 64 | + // Scan through the inheritance clause to find one of the protocols/types we're interested in. |
| 65 | + for inheritance in inheritanceClause.inheritedTypes { |
| 66 | + guard let identifier = inheritance.type.as(IdentifierTypeSyntax.self) else { |
| 67 | + continue |
| 68 | + } |
| 69 | + |
| 70 | + if identifier.name.text != "FindingCategorizing" { |
| 71 | + // Keep looking at the other inheritances. |
| 72 | + continue |
| 73 | + } |
| 74 | + |
| 75 | + // Now that we know it's a pretty printing category, collect the `description` method and extract the name. |
| 76 | + for member in enumDecl.memberBlock.members { |
| 77 | + guard let varDecl = member.decl.as(VariableDeclSyntax.self) else { continue } |
| 78 | + guard |
| 79 | + let descriptionDecl = varDecl.bindings |
| 80 | + .first(where: { |
| 81 | + $0.pattern.as(IdentifierPatternSyntax.self)?.identifier.text == "description" |
| 82 | + }) |
| 83 | + else { continue } |
| 84 | + let pp = PrettyPrintCategoryVisitor(viewMode: .sourceAccurate) |
| 85 | + _ = pp.walk(descriptionDecl) |
| 86 | + return pp.prettyPrintCategories |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return [] |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +final class PrettyPrintCategoryVisitor: SyntaxVisitor { |
| 95 | + |
| 96 | + var prettyPrintCategories: [String] = [] |
| 97 | + |
| 98 | + override func visit(_ node: StringSegmentSyntax) -> SyntaxVisitorContinueKind { |
| 99 | + prettyPrintCategories.append(node.content.text) |
| 100 | + return .skipChildren |
| 101 | + } |
| 102 | +} |
0 commit comments