diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-09-20 02:18:09 +0300 |
commit | 6ed4ec3e0b1340f96b7c043ef51d1b33bbe85fde (patch) | |
tree | dc4d20fe6064752c0bd323187252c77e0a89144b /scripts/rubocop-parse | |
parent | 9868dae7fc0655bd7ce4a6887d4e6d487690eeed (diff) |
Add latest changes from gitlab-org/gitlab@15-4-stable-eev15.4.0-rc42
Diffstat (limited to 'scripts/rubocop-parse')
-rwxr-xr-x | scripts/rubocop-parse | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/scripts/rubocop-parse b/scripts/rubocop-parse new file mode 100755 index 00000000000..4c82be5934b --- /dev/null +++ b/scripts/rubocop-parse @@ -0,0 +1,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 |