# frozen_string_literal: truemoduleRuboCopmoduleCop# FIXMEclassTeamDEFAULT_OPTIONS={auto_correct: false,debug: false}.freezeInvestigation=Struct.new(:offenses,:errors)attr_reader:errors,:warnings,:updated_source_filealiasupdated_source_file?updated_source_filedefinitialize(cop_classes,config,options=nil)@cop_classes=cop_classes@config=config@options=options||DEFAULT_OPTIONS@errors=[]@warnings=[]validate_configenddefautocorrect?@options[:auto_correct]enddefdebug?@options[:debug]enddefinspect_file(processed_source)# If we got any syntax errors, return only the syntax offenses.unlessprocessed_source.valid_syntax?returnLint::Syntax.offenses_from_processed_source(processed_source,@config,@options)endoffenses(processed_source)enddefcopsonly=@options.fetch(:only,[])safe=@options.fetch(:safe,false)@cops||=@cop_classes.enabled(@config,only,safe).mapdo|cop_class|cop_class.new(@config,@options)endenddefforces@forces||=forces_for(cops)enddefforces_for(cops)Force.all.each_with_object([])do|force_class,forces|joining_cops=cops.select{|cop|cop.join_force?(force_class)}nextifjoining_cops.empty?forces<<force_class.new(joining_cops)endenddefautocorrect(buffer,cops)@updated_source_file=falsereturnunlessautocorrect?new_source=autocorrect_all_cops(buffer,cops)returnifnew_source==buffer.sourceif@options[:stdin]# holds source read in from stdin, when --stdin option is used@options[:stdin]=new_sourceelsefilename=buffer.nameFile.open(filename,'w'){|f|f.write(new_source)}end@updated_source_file=trueendprivatedefoffenses(processed_source)# The autocorrection process may have to be repeated multiple times# until there are no corrections left to perform# To speed things up, run auto-correcting cops by themselves, and only# run the other cops when no corrections are leftautocorrect_cops,other_cops=cops.partition(&:autocorrect?)autocorrect=investigate(autocorrect_cops,processed_source)do|offenses|# We corrected some errors. Another round of inspection will be# done, and any other offenses will be caught then, so we don't# need to continue.returnoffensesifautocorrect(processed_source.buffer,autocorrect_cops)endother=investigate(other_cops,processed_source)errors=autocorrect.errors.merge(other.errors)process_commissioner_errors(processed_source.path,errors)autocorrect.offenses.concat(other.offenses)enddefinvestigate(cops,processed_source)returnInvestigation.new([],{})ifcops.empty?commissioner=Commissioner.new(cops,forces_for(cops))offenses=commissioner.investigate(processed_source)yieldoffensesifblock_given?Investigation.new(offenses,commissioner.errors)enddefautocorrect_all_cops(buffer,cops)corrector=Corrector.new(buffer)collate_corrections(corrector,cops)if!corrector.corrections.empty?corrector.rewriteelsebuffer.sourceendenddefcollate_corrections(corrector,cops)skips=Set.newcops.eachdo|cop|nextifcop.corrections.empty?nextifskips.include?(cop.class)corrector.corrections.concat(cop.corrections)skips.merge(cop.class.autocorrect_incompatible_with)endenddefvalidate_configcops.eachdo|cop|cop.validate_configifcop.respond_to?(:validate_config)endenddefprocess_commissioner_errors(file,file_errors)file_errors.eachdo|cop,errors|errors.eachdo|cop_error|e=cop_error.errorline=":#{cop_error.line}"ifcop_error.linecolumn=":#{cop_error.column}"ifcop_error.columnlocation="#{file}#{line}#{column}"ife.is_a?(Warning)handle_warning(e,location)elsehandle_error(e,location,cop)endendendenddefhandle_warning(error,location)message=Rainbow("#{error.message} (from file: #{location})").yellow@warnings<<messagewarnmessageputserror.backtraceifdebug?enddefhandle_error(error,location,cop)message=Rainbow("An error occurred while #{cop.name}"\" cop was inspecting #{location}.").red@errors<<messagewarnmessageifdebug?putserror.message,error.backtraceelsewarn'To see the complete backtrace run rubocop -d.'endendendendend