Class: A11y::Lint::CallNode
- Inherits:
-
Object
- Object
- A11y::Lint::CallNode
- Defined in:
- lib/a11y/lint/call_node.rb
Overview
Wraps a Prism::CallNode with a rule-friendly query API.
Instance Attribute Summary collapse
-
#prism_node ⇒ Object
readonly
Returns the value of attribute prism_node.
Instance Method Summary collapse
- #block? ⇒ Boolean
-
#find(name) ⇒ Object
Finds a receiverless call by method name in this node’s subtree (including self).
- #first_positional_arg_empty_string? ⇒ Boolean
-
#initialize(prism_node) ⇒ CallNode
constructor
A new instance of CallNode.
-
#keyword?(*keys) ⇒ Boolean
Checks for a keyword argument by name.
-
#keyword_non_empty?(key) ⇒ Boolean
True when the keyword is present AND its value is a non-empty string literal OR any non-string expression (dynamic — can’t be statically proven empty, so treat as providing content).
- #method_name ⇒ Object
- #positional_args ⇒ Object
Constructor Details
#initialize(prism_node) ⇒ CallNode
Returns a new instance of CallNode.
11 12 13 |
# File 'lib/a11y/lint/call_node.rb', line 11 def initialize(prism_node) @prism_node = prism_node end |
Instance Attribute Details
#prism_node ⇒ Object (readonly)
Returns the value of attribute prism_node.
9 10 11 |
# File 'lib/a11y/lint/call_node.rb', line 9 def prism_node @prism_node end |
Instance Method Details
#block? ⇒ Boolean
63 64 65 |
# File 'lib/a11y/lint/call_node.rb', line 63 def block? !@prism_node.block.nil? end |
#find(name) ⇒ Object
Finds a receiverless call by method name in this node’s subtree (including self). Returns a CallNode or nil.
69 70 71 72 |
# File 'lib/a11y/lint/call_node.rb', line 69 def find(name) found = search_for_call(@prism_node, name) found ? self.class.new(found) : nil end |
#first_positional_arg_empty_string? ⇒ Boolean
58 59 60 61 |
# File 'lib/a11y/lint/call_node.rb', line 58 def first_positional_arg_empty_string? first = positional_args.first first.is_a?(Prism::StringNode) && first.unescaped.empty? end |
#keyword?(*keys) ⇒ Boolean
Checks for a keyword argument by name.
keyword?(:alt) => alt: or "alt" =>
keyword?(:aria, :label) => aria: { label: ... }
keyword?(:"aria-label") => "aria-label" =>
23 24 25 26 27 28 29 30 31 |
# File 'lib/a11y/lint/call_node.rb', line 23 def keyword?(*keys) return false unless (kw_hash = find_keyword_hash) if keys.length == 1 flat_keyword?(kw_hash, keys[0]) else nested_keyword?(kw_hash, keys[0], keys[1]) end end |
#keyword_non_empty?(key) ⇒ Boolean
True when the keyword is present AND its value is a non-empty string literal OR any non-string expression (dynamic — can’t be statically proven empty, so treat as providing content). False when the key is absent or the value is an empty string literal.
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/a11y/lint/call_node.rb', line 38 def keyword_non_empty?(key) return false unless (kw_hash = find_keyword_hash) assoc = kw_hash.elements.find { |a| key_name(a) == key.to_s } return false unless assoc value = assoc.value return !value.unescaped.empty? if value.is_a?(Prism::StringNode) true end |
#method_name ⇒ Object
15 16 17 |
# File 'lib/a11y/lint/call_node.rb', line 15 def method_name @prism_node.name.to_s end |
#positional_args ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/a11y/lint/call_node.rb', line 50 def positional_args return [] unless @prism_node.arguments @prism_node.arguments.arguments.reject do |a| a.is_a?(Prism::KeywordHashNode) end end |