Class: RubyNext::Language::Rewriters::OnelinePatternParensless

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby-next/language/rewriters/3.1/oneline_pattern_parensless.rb

Overview

Allow omitting parentheses around patterns in ‘=>` and `in`

Constant Summary collapse

NAME =
"pattern-matching-oneline-parensless"
SYNTAX_PROBE =
"[1, 2] => a, b"
MIN_SUPPORTED_VERSION =
Gem::Version.new("3.1.0")

Instance Attribute Summary

Attributes inherited from Base

#locals

Instance Method Summary collapse

Methods inherited from Base

ast?, #initialize, #s

Methods inherited from Abstract

ast?, #initialize, text?, unsupported_syntax?, unsupported_version?

Constructor Details

This class inherits a constructor from RubyNext::Language::Rewriters::Base

Instance Method Details

#on_match_pattern(node) ⇒ Object Also known as: on_match_pattern_p



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby-next/language/rewriters/3.1/oneline_pattern_parensless.rb', line 14

def on_match_pattern(node)
  _, pattern = *node.children

  # When no parens, children boundaries are the same as the whole pattern
  if (
    pattern.type == :array_pattern ||
    pattern.type == :hash_pattern
  ) &&
      pattern.children.any? &&
      pattern.loc.column == pattern.children.first.loc.column &&
      pattern.loc.last_column == pattern.children.last.loc.last_column

    context.track! self

    left_p, right_p = (pattern.type == :array_pattern) ? %w([ ]) : %w[{ }]

    insert_before(pattern.loc.expression, left_p)
    insert_after(pattern.loc.expression, right_p)
  else
    super(node)
  end
end