src/ripper.rb in prettier-0.17.0 vs src/ripper.rb in prettier-0.18.0

- old
+ new

@@ -1,12 +1,14 @@ #!/usr/bin/env ruby -REQUIRED_VERSION = Gem::Version.new('2.5') -if Gem::Version.new(RUBY_VERSION) < REQUIRED_VERSION +# We implement our own version checking here instead of using Gem::Version so +# that we can use the --disable-gems flag. +major, minor, * = RUBY_VERSION.split('.').map(&:to_i) +if (major < 2) || ((major == 2) && (minor < 5)) warn( - "Ruby version #{RUBY_VERSION} not supported. " \ - "Please upgrade to #{REQUIRED_VERSION} or above." + "Ruby version #{current_version} not supported. " \ + "Please upgrade to #{required_version} or above." ) exit 1 end @@ -164,10 +166,11 @@ END: [:@kw, 'END'], alias: [:@kw, 'alias'], assoc_splat: [:@op, '**'], arg_paren: :@lparen, args_add_star: [:@op, '*'], + args_forward: [:@op, '...'], begin: [:@kw, 'begin'], blockarg: [:@op, '&'], brace_block: :@lbrace, break: [:@kw, 'break'], case: [:@kw, 'case'], @@ -288,11 +291,12 @@ node = find_scanner_event(:@tstring_beg) super(*body).merge!( start: node[:start], char_start: node[:char_start], - char_end: char_pos + char_end: char_pos, + quote: node[:body] ) end end # Technically, the `not` operator is a unary operator but is reported as @@ -317,18 +321,32 @@ # special case in which if there is a `:@symbeg` event we can hook on to # then we use it, otherwise we just look at the beginning of the first # child node. %i[dyna_symbol symbol_literal].each do |event| define_method(:"on_#{event}") do |*body| - char_start = + options = if scanner_events.any? { |sevent| sevent[:type] == :@symbeg } - find_scanner_event(:@symbeg)[:char_start] + symbeg = find_scanner_event(:@symbeg) + + { + char_start: symbeg[:char_start], + char_end: char_pos, + quote: symbeg[:body][1] + } + elsif scanner_events.any? { |sevent| sevent[:type] == :@label_end } + label_end = find_scanner_event(:@label_end) + + { + char_start: char_start_for(body), + char_end: char_pos, + quote: label_end[:body][0] + } else - char_start_for(body) + { char_start: char_start_for(body), char_end: char_pos } end - super(*body).merge!(char_start: char_start, char_end: char_pos) + super(*body).merge!(options) end end def on_program(*body) super(*body).merge!(start: 1, char_start: 0, char_end: char_pos) @@ -628,10 +646,12 @@ end end end ) + # This module contains miscellaneous fixes required to get the right + # structure. prepend( Module.new do private # These are the event types that contain _actual_ string content. If @@ -656,21 +676,9 @@ @__end__ = super(lines[lineno..-1].join("\n")) end def on_program(*body) super(*body).tap { |node| node[:body][0][:body] << __end__ if __end__ } - end - - # Adds the used quote type onto string nodes. This is necessary because - # we're going to have to stick to whatever quote the user chose if there - # are escape sequences within the string. For example, if you have '\n' - # we can't switch to double quotes without changing what it means. - def on_tstring_end(quote) - last_sexp.merge!(quote: quote) - end - - def on_label_end(quote) - last_sexp.merge!(quote: quote[0]) # quote is ": or ': end # Normally access controls are reported as vcall nodes. This creates a # new node type to explicitly track those nodes instead, so that the # printer can add new lines as necessary.