diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-09 03:09:20 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-11-09 03:09:20 +0300 |
commit | 5cd8380e46d88d2afc314b11d8e3b3dee0335948 (patch) | |
tree | 61ef7a60b73cf3a985c2bcf2e2c4f694a30e8dc6 /scripts | |
parent | 9f9d994f13388fb3ce117ed01c2cd0c05c98d055 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/rubocop-parse | 44 |
1 files changed, 36 insertions, 8 deletions
diff --git a/scripts/rubocop-parse b/scripts/rubocop-parse index 0a234df81cd..c99d66e99ad 100755 --- a/scripts/rubocop-parse +++ b/scripts/rubocop-parse @@ -39,7 +39,34 @@ module Helper def ast(source, file: '', version: nil) version ||= ruby_version - puts RuboCop::AST::ProcessedSource.new(source, version).ast.to_s + + ast = RuboCop::AST::ProcessedSource.new(source, version).ast + return ast if ast + + warn "Syntax error in `#{source}`." + end + + def pattern(string) + RuboCop::NodePattern.new(string) + end + + def help! + puts <<~HELP + + Use `ast(source_string, version: nil)` method to parse code and return its AST. + Use `pattern(string)` to compile RuboCop's node patterns. + + See https://docs.rubocop.org/rubocop-ast/node_pattern.html. + + Examples: + node = ast('puts :hello') + + pat = pattern('`(sym :hello)') + pat.match(node) # => true + + HELP + + nil end def ruby_version @@ -56,11 +83,12 @@ def start_irb include Helper # rubocop:disable Style/MixinUsage - puts "Ruby version: #{ruby_version}" - puts - puts "Use `ast(source_string, version: nil)` method to parse code and output AST. For example:" - puts " ast('puts :hello')" - puts + puts <<~BANNER + Ruby version: #{ruby_version} + + Type `help!` for instructions and examples. + + BANNER IRB.start end @@ -103,12 +131,12 @@ elsif options.interactive start_irb end elsif options.eval - Helper.ast(options.eval) + puts Helper.ast(options.eval) elsif files.any? files.each do |file| if File.file?(file) source = File.read(file) - Helper.ast(source, file: file) + puts Helper.ast(source, file: file) else warn "Skipping non-file #{file.inspect}" end |