@@ -52,13 +52,17 @@ public class Context {
5252 /// Contains the rules have been disabled by comments for certain line numbers.
5353 public let ruleMask : RuleMask
5454
55+ /// Contains all the available rules' names associated to their types' object identifiers.
56+ public let ruleNameCache : [ ObjectIdentifier : String ]
57+
5558 /// Creates a new Context with the provided configuration, diagnostic engine, and file URL.
5659 public init (
5760 configuration: Configuration ,
5861 diagnosticEngine: DiagnosticEngine ? ,
5962 fileURL: URL ,
6063 sourceFileSyntax: SourceFileSyntax ,
61- source: String ? = nil
64+ source: String ? = nil ,
65+ ruleNameCache: [ ObjectIdentifier : String ]
6266 ) {
6367 self . configuration = configuration
6468 self . diagnosticEngine = diagnosticEngine
@@ -71,12 +75,22 @@ public class Context {
7175 syntaxNode: Syntax ( sourceFileSyntax) ,
7276 sourceLocationConverter: sourceLocationConverter
7377 )
78+ self . ruleNameCache = ruleNameCache
7479 }
7580
7681 /// Given a rule's name and the node it is examining, determine if the rule is disabled at this
7782 /// location or not.
78- public func isRuleEnabled( _ ruleName : String , node: Syntax ) -> Bool {
83+ public func isRuleEnabled< R : Rule > ( _ rule : R . Type , node: Syntax ) -> Bool {
7984 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
8094 switch ruleMask. ruleState ( ruleName, at: loc) {
8195 case . default:
8296 return configuration. rules [ ruleName] ?? false
0 commit comments