Class: RubyParser
- Inherits:
-
Object
- Object
- RubyParser
- Defined in:
- lib/puppet-check/ruby_parser.rb
Overview
executes diagnostics on ruby files
Class Method Summary collapse
-
.librarian(files, style, rc_args) ⇒ Object
checks librarian puppet (Puppetfile/Modulefile) and misc ruby (Rakefile/Gemfile).
-
.ruby(files, style, rc_args) ⇒ Object
checks ruby (.rb).
-
.template(files) ⇒ Object
checks ruby template (.erb).
Class Method Details
.librarian(files, style, rc_args) ⇒ Object
checks librarian puppet (Puppetfile/Modulefile) and misc ruby (Rakefile/Gemfile)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/puppet-check/ruby_parser.rb', line 67 def self.librarian(files, style, rc_args) # efficient var assignment prior to iterator if style require 'json' require 'rubocop' rubocop_cli = RuboCop::CLI.new # RuboCop is grumpy about non-snake_case filenames so disable the FileName check rc_args.include?('--except') ? rc_args[rc_args.index('--except') + 1] = "#{rc_args[rc_args.index('--except') + 1]},Naming/FileName" : rc_args.push('--except', 'Naming/FileName') end files.each do |file| # check librarian puppet syntax RubyVM::InstructionSequence.compile_file(file) rescue SyntaxError, LoadError, ArgumentError => err PuppetCheck.files[:errors][file] = err.to_s.gsub("#{file}:", '').split("\n") # check librarian puppet style else if style warnings = Utils.capture_stdout { rubocop_cli.run(rc_args + ['--enable-pending-cops', '--plugin', 'rubocop-performance', '--format', 'json', file]) } offenses = JSON.parse(warnings)['files'][0]['offenses'].map { |warning| "#{warning['location']['line']}:#{warning['location']['column']} #{warning['message']}" } # collect style warnings next PuppetCheck.files[:warnings][file] = offenses unless offenses.empty? end PuppetCheck.files[:clean].push(file.to_s) end end |
.ruby(files, style, rc_args) ⇒ Object
checks ruby (.rb)
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 |
# File 'lib/puppet-check/ruby_parser.rb', line 7 def self.ruby(files, style, rc_args) # prepare rubocop object for style checks if style require 'json' require 'rubocop' require 'reek' rubocop_cli = RuboCop::CLI.new end files.each do |file| # check ruby syntax RubyVM::InstructionSequence.compile_file(file) rescue ScriptError, StandardError => err PuppetCheck.files[:errors][file] = err.to_s.gsub("#{file}:", '').split("\n") else # check ruby style if style # add rubocop rspec plugin if the file is a spec rc_args.push('--plugin', 'rubocop-rspec') if file =~ /_spec\.rb$/ # check RuboCop and parse warnings' JSON output rubocop_warnings = Utils.capture_stdout { rubocop_cli.run(rc_args + ['--enable-pending-cops', '--plugin', 'rubocop-performance', '--format', 'json', file]) } rubocop_offenses = JSON.parse(rubocop_warnings)['files'][0]['offenses'].map { |warning| "#{warning['location']['line']}:#{warning['location']['column']} #{warning['message']}" } # check Reek using examiner api examiner = Reek::Examiner.new(Pathname.new(file)) reek_offenses = examiner.smells.map { |warning| "#{warning.lines.join(',')}: #{warning.context} #{warning.message}" } # assign warnings from combined offenses warnings = rubocop_offenses + reek_offenses # return warnings next PuppetCheck.files[:warnings][file] = warnings unless warnings.empty? end PuppetCheck.files[:clean].push(file.to_s) end end |
.template(files) ⇒ Object
checks ruby template (.erb)
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/puppet-check/ruby_parser.rb', line 47 def self.template(files) require 'erb' files.each do |file| # check ruby template syntax begin warnings = Utils.capture_stderr { ERB.new(File.read(file), trim_mode: '-').result(Object.new.instance_eval { binding }) } rescue NameError, TypeError # empty out warnings since it would contain an error if this pass triggers warnings = '' rescue ScriptError => err next PuppetCheck.files[:errors][file] = err.to_s.gsub('(erb):', '').split("\n") end # return warnings from the check if there were any next PuppetCheck.files[:warnings][file] = warnings.to_s.gsub('warning: ', '').delete("\n").split('(erb):').compact unless warnings == '' PuppetCheck.files[:clean].push(file.to_s) end end |