Class: A11y::Lint::PhlexRunner::BlockTextScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/a11y/lint/phlex_runner/block_text_scanner.rb

Overview

Decides whether a Phlex tag block produces accessible text. Stateless: only reads the given Prism block node and pre-collected child tags.

Constant Summary collapse

TEXT_CALLS =

Phlex auto-emits the return value of these calls into the document: ‘plain` / `text` are built-in; the rest are registered as value helpers by phlex-rails via `register_value_helper`.

%w[
  plain text
  t translate l localize
  pluralize truncate
  number_to_currency number_to_human number_to_human_size
  number_to_percentage number_to_phone
  number_with_delimiter number_with_precision
  highlight excerpt
].to_set.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block, children) ⇒ BlockTextScanner

Returns a new instance of BlockTextScanner.



53
54
55
56
# File 'lib/a11y/lint/phlex_runner/block_text_scanner.rb', line 53

def initialize(block, children)
  @block = block
  @children = children
end

Class Method Details

.non_blank_string_literal?(node) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/a11y/lint/phlex_runner/block_text_scanner.rb', line 36

def self.non_blank_string_literal?(node)
  case node
  when Prism::StringNode
    !node.unescaped.strip.empty?
  when Prism::InterpolatedStringNode
    node.parts.any? do |part|
      if part.is_a?(Prism::StringNode)
        !part.unescaped.strip.empty?
      else
        true
      end
    end
  else
    false
  end
end

.scan(block, children:) ⇒ Object



25
26
27
# File 'lib/a11y/lint/phlex_runner/block_text_scanner.rb', line 25

def self.scan(block, children:)
  new(block, children).scan
end

.text_emitting?(node) ⇒ Boolean

Public recognizer for a single Prism node — used by PhlexRunner to inspect a tag’s first positional argument, where Phlex emits the value as text content (‘a(“Click”, href: “/x”)`).

Returns:

  • (Boolean)


32
33
34
# File 'lib/a11y/lint/phlex_runner/block_text_scanner.rb', line 32

def self.text_emitting?(node)
  new(nil, []).text_emitting?(node)
end

Instance Method Details

#scanObject



58
59
60
61
62
# File 'lib/a11y/lint/phlex_runner/block_text_scanner.rb', line 58

def scan
  return false unless @block.is_a?(Prism::BlockNode)

  scan_for_text(@block) || @children.any?(&:text_content?)
end

#text_emitting?(node) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/a11y/lint/phlex_runner/block_text_scanner.rb', line 64

def text_emitting?(node)
  auto_emitted_text?(node)
end