@@ -52,13 +52,17 @@ public class Context {
52
52
/// Contains the rules have been disabled by comments for certain line numbers.
53
53
public let ruleMask : RuleMask
54
54
55
+ /// Contains all the available rules' names associated to their types' object identifiers.
56
+ public let ruleNameCache : [ ObjectIdentifier : String ]
57
+
55
58
/// Creates a new Context with the provided configuration, diagnostic engine, and file URL.
56
59
public init (
57
60
configuration: Configuration ,
58
61
diagnosticEngine: DiagnosticEngine ? ,
59
62
fileURL: URL ,
60
63
sourceFileSyntax: SourceFileSyntax ,
61
- source: String ? = nil
64
+ source: String ? = nil ,
65
+ ruleNameCache: [ ObjectIdentifier : String ]
62
66
) {
63
67
self . configuration = configuration
64
68
self . diagnosticEngine = diagnosticEngine
@@ -71,12 +75,22 @@ public class Context {
71
75
syntaxNode: Syntax ( sourceFileSyntax) ,
72
76
sourceLocationConverter: sourceLocationConverter
73
77
)
78
+ self . ruleNameCache = ruleNameCache
74
79
}
75
80
76
81
/// Given a rule's name and the node it is examining, determine if the rule is disabled at this
77
82
/// location or not.
78
- public func isRuleEnabled( _ ruleName : String , node: Syntax ) -> Bool {
83
+ public func isRuleEnabled< R : Rule > ( _ rule : R . Type , node: Syntax ) -> Bool {
79
84
let loc = node. startLocation ( converter: self . sourceLocationConverter)
85
+
86
+ assert (
87
+ ruleNameCache [ ObjectIdentifier ( rule) ] != nil ,
88
+ """
89
+ Missing cached rule name for ' \( rule) '! \
90
+ Ensure `generate-pipelines` has been run and `ruleNameCache` was injected.
91
+ """ )
92
+
93
+ let ruleName = ruleNameCache [ ObjectIdentifier ( rule) ] ?? R . ruleName
80
94
switch ruleMask. ruleState ( ruleName, at: loc) {
81
95
case . default:
82
96
return configuration. rules [ ruleName] ?? false
0 commit comments