Sha256: 371903a4b0291a6fcfc2911ec2680e456ee5425b28a3911001c19da68fca2cc8

Contents?: true

Size: 925 Bytes

Versions: 7

Compression:

Stored size: 925 Bytes

Contents

# frozen_string_literal: true

module RubyNext
  module Language
    module Rewriters
      class ShorthandHash < Base
        NAME = "shorthand-hash"
        SYNTAX_PROBE = "data = {x}"
        MIN_SUPPORTED_VERSION = Gem::Version.new("3.0.0")

        def on_ipair(node)
          context.track! self

          ident, = *node.children

          key = key_from_ident(ident)

          replace(
            node.loc.expression,
            "#{key}: #{key}"
          )

          node.updated(
            :pair,
            [
              s(:sym, key),
              ident
            ]
          )
        end

        private

        def key_from_ident(node)
          case node.type
          when :send
            node.children[1]
          when :lvar
            node.children[0]
          else
            raise ArgumentError, "Unsupport ipair node: #{node}"
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ruby-next-core-0.11.0 lib/ruby-next/language/rewriters/shorthand_hash.rb
ruby-next-core-0.10.5 lib/ruby-next/language/rewriters/shorthand_hash.rb
ruby-next-core-0.10.4 lib/ruby-next/language/rewriters/shorthand_hash.rb
ruby-next-core-0.10.3 lib/ruby-next/language/rewriters/shorthand_hash.rb
ruby-next-core-0.10.2 lib/ruby-next/language/rewriters/shorthand_hash.rb
ruby-next-core-0.10.1 lib/ruby-next/language/rewriters/shorthand_hash.rb
ruby-next-core-0.10.0 lib/ruby-next/language/rewriters/shorthand_hash.rb