Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rubocop-parse « scripts - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4c82be5934b2e904689ad1be196ce13ac6dc52a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env ruby
# frozen_string_literal: true

# Emit AST from parsed Ruby code by RuboCop.
#
# This is an alternative to `ruby-parser` shipped with `parser` gem.
#
# Usage:
#   rubocop-parse -e 'puts "hello"'
#   (send nil :puts
#     (str "hello"))
#
#   rubocop-parse -e 'puts "hello"' -v 3.0
#   (send nil :puts
#     (str "hello"))
#
#   rubocop-parse app/models/project.rb
#   (begin
#     (send nil :require
#       (str "carrierwave/orm/activerecord"))
#     (class
#       (const nil :Project)
#       (const nil :ApplicationRecord)
#       (begin
#         (send nil :include
#    ...

require_relative '../config/bundler_setup'

require 'rubocop'
require 'optparse'

def print_ast(file, source, version)
  version ||= RuboCop::ConfigStore.new.for_file(file).target_ruby_version
  puts RuboCop::AST::ProcessedSource.new(source, version).ast.to_s
end

options = Struct.new(:eval, :ruby_version, :print_help, keyword_init: true).new

parser = OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [-e code] [FILE...]"

  opts.on('-e FRAGMENT', '--eval FRAGMENT', 'Process a fragment of Ruby code') do |code|
    options.eval = code
  end

  opts.on('-v RUBY_VERSION', '--ruby-version RUBY_VERSION',
          'Parse as Ruby would. Defaults to RuboCop TargetRubyVersion setting.') do |ruby_version|
    options.ruby_version = Float(ruby_version)
  end

  opts.on('-h', '--help') do
    options.print_help = true
  end
end

args = parser.parse!

if options.print_help
  puts parser
  exit
end

print_ast('', options.eval, options.ruby_version) if options.eval

args.each do |arg|
  if File.file?(arg)
    source = File.read(arg)
    print_ast(arg, source, options.ruby_version)
  else
    warn "Skipping non-file #{arg.inspect}"
  end
end