Class: RuboCop::Cop::Yoshiki::Style::NestedHtmlOptionKeys

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/yoshiki/style/nested_html_option_keys.rb,
lib/rubocop/cop/yoshiki/style/nested_html_option_keys/rewriter.rb

Overview

Nests hyphenated data-* / aria-* keys so every key is a 1.9 identifier. Rails and Haml flatten the nested hashes back to the same HTML (data-game-live-target, aria-label).

This is not a HashSyntax replacement. HashSyntax still owns : vs =>. Nesting first is what lets HashSyntax stay in ruby19 form instead of mixed rockets or quoted-symbol keys.

Keys whose hyphen segments are not identifiers (1p-ignore) stay. Real HTML attributes (http-equiv) stay. Stimulus controller names are not renamed — hyphens split in place.

Examples:

# bad
{ data: { controller: 'chat', 'chat-target' => 'input' } }
{ 'aria-label' => placeholder }

# good
{ data: { controller: 'chat', chat: { target: 'input' } } }
{ aria: { label: placeholder } }

Defined Under Namespace

Classes: Forest, Printer, Rewriter, Tree

Constant Summary collapse

MSG =
'Nest hyphenated HTML option keys as symbol hashes.'.freeze
IDENT =
/\A[a-z_][a-z0-9_]*\z/

Instance Method Summary collapse

Instance Method Details

#on_hash(node) ⇒ Object



32
33
34
# File 'lib/rubocop/cop/yoshiki/style/nested_html_option_keys.rb', line 32

def on_hash node
  (@hashes ||= []) << node
end

#on_investigation_endObject



36
37
38
39
40
41
# File 'lib/rubocop/cop/yoshiki/style/nested_html_option_keys.rb', line 36

def on_investigation_end
  flagged.each { register(it) }
  @hashes = []

  super
end