Class: Rubydex::Linter::CustomRule Abstract
- Defined in:
- lib/rubydex/linter/custom_rule.rb
Overview
Base class for semantic lint rules, which collect their diagnostics by walking a resolved graph.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
: LinterConfig.
-
#diagnostics ⇒ Object
readonly
: Array.
-
#graph ⇒ Object
readonly
: Graph.
Instance Method Summary collapse
-
#child_classes(base_name) ⇒ Object
Returns every class inheriting from
base_name, excluding the base class itself. -
#diagnostic_location(definition) ⇒ Object
Anchors a diagnostic on a definition's name token, falling back to its full range when no name location exists.
-
#initialize(graph, config:) ⇒ CustomRule
constructor
: (Graph, config: LinterConfig) -> void.
-
#lint ⇒ Object
abstract
: () -> void.
-
#required_method(namespace, method_name) ⇒ Object
: (Namespace, String) -> Rubydex::Method.
-
#required_namespace(name) ⇒ Object
: (String) -> Namespace.
-
#rule_name ⇒ Object
: () -> String.
Methods inherited from Rule
default_severity, rule_name, severity
Constructor Details
#initialize(graph, config:) ⇒ CustomRule
: (Graph, config: LinterConfig) -> void
18 19 20 21 22 23 |
# File 'lib/rubydex/linter/custom_rule.rb', line 18 def initialize(graph, config:) super() @graph = graph @config = config @diagnostics = [] #: Array[Diagnostic] end |
Instance Attribute Details
#config ⇒ Object (readonly)
: LinterConfig
12 13 14 |
# File 'lib/rubydex/linter/custom_rule.rb', line 12 def config @config end |
#diagnostics ⇒ Object (readonly)
: Array
15 16 17 |
# File 'lib/rubydex/linter/custom_rule.rb', line 15 def diagnostics @diagnostics end |
#graph ⇒ Object (readonly)
: Graph
9 10 11 |
# File 'lib/rubydex/linter/custom_rule.rb', line 9 def graph @graph end |
Instance Method Details
#child_classes(base_name) ⇒ Object
Returns every class inheriting from base_name, excluding the base class itself.
: (String) -> Enumerable
39 40 41 42 43 44 45 46 |
# File 'lib/rubydex/linter/custom_rule.rb', line 39 def child_classes(base_name) required_namespace(base_name).descendants.lazy.filter_map do |child_declaration| next unless child_declaration.is_a?(Rubydex::Class) next if child_declaration.name == base_name child_declaration end end |
#diagnostic_location(definition) ⇒ Object
Anchors a diagnostic on a definition's name token, falling back to its full range when no name location exists. : (Definition) -> Location
33 34 35 |
# File 'lib/rubydex/linter/custom_rule.rb', line 33 def diagnostic_location(definition) definition.name_location || definition.location end |
#lint ⇒ Object
: () -> void
27 28 29 |
# File 'lib/rubydex/linter/custom_rule.rb', line 27 def lint raise NotImplementedError, "Subclasses must implement the lint method" end |
#required_method(namespace, method_name) ⇒ Object
: (Namespace, String) -> Rubydex::Method
57 58 59 60 61 62 |
# File 'lib/rubydex/linter/custom_rule.rb', line 57 def required_method(namespace, method_name) method = namespace.find_member(method_name) return method if method.is_a?(Rubydex::Method) raise MissingGraphDependencyError.new(rule_name, "`#{namespace.name}##{method_name}`") end |
#required_namespace(name) ⇒ Object
: (String) -> Namespace
49 50 51 52 53 54 |
# File 'lib/rubydex/linter/custom_rule.rb', line 49 def required_namespace(name) declaration = graph[name] return declaration if declaration.is_a?(Namespace) raise MissingGraphDependencyError.new(rule_name, "`#{name}`") end |
#rule_name ⇒ Object
: () -> String
65 66 67 |
# File 'lib/rubydex/linter/custom_rule.rb', line 65 def rule_name self.class.rule_name end |