code
stringlengths
26
124k
docstring
stringlengths
23
125k
func_name
stringlengths
1
98
language
stringclasses
1 value
repo
stringlengths
5
53
path
stringlengths
7
151
url
stringlengths
50
211
license
stringclasses
7 values
def deserialize_offenses(offenses) source_buffer = Parser::Source::Buffer.new(@filename) source_buffer.source = File.read(@filename, encoding: Encoding::UTF_8) offenses.map! do |o| location = Parser::Source::Range.new(source_buffer, o['location']['begin_pos'], o['location']['end_pos']) Cop::Offense.new(o['severity'], location, o['message'], o['cop_name'], o['status'].to_sym) end end
Restore an offense object loaded from a JSON file.
deserialize_offenses
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cached_data.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cached_data.rb
Apache-2.0
def run(args = ARGV) @options, paths = Options.new.parse(args) @env = Environment.new(@options, @config_store, paths) if @options[:init] run_command(:init) else act_on_options validate_options_vs_config apply_default_formatter execute_runners end rescue ConfigNotFoundError, IncorrectCopNameError, OptionArgumentError => e warn e.message STATUS_ERROR rescue RuboCop::Error => e warn Rainbow("Error: #{e.message}").red STATUS_ERROR rescue Finished STATUS_SUCCESS rescue OptionParser::InvalidOption => e warn e.message warn 'For usage information, use --help' STATUS_ERROR rescue StandardError, SyntaxError, LoadError => e warn e.message warn e.backtrace STATUS_ERROR end
@api public Entry point for the application logic. Here we do the command line arguments processing and inspect the target files. @param args [Array<String>] command line arguments @return [Integer] UNIX exit code rubocop:disable Metrics/MethodLength, Metrics/AbcSize
run
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cli.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cli.rb
Apache-2.0
def handle_switch(cop_names, names, disabled, extras, comment) cop_names.each do |name| if disabled names[name] += 1 elsif (names[name]).positive? names[name] -= 1 else extras[comment] << name end end end
Collect cops that have been disabled or enabled by name in a directive comment so that `Lint/RedundantCopEnableDirective` can register offenses correctly.
handle_switch
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/comment_config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/comment_config.rb
Apache-2.0
def internal? base_config_path = File.expand_path(File.join(ConfigLoader::RUBOCOP_HOME, 'config')) File.expand_path(loaded_path).start_with?(base_config_path) end
True if this is a config file that is shipped with RuboCop
internal?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
Apache-2.0
def for_cop(cop) @for_cop[cop.respond_to?(:cop_name) ? cop.cop_name : cop] end
@return [Config] for the given cop / cop name. Note: the 'Enabled' attribute is calculated according to the department's and 'AllCops' configuration; other attributes are not inherited.
for_cop
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
Apache-2.0
def for_badge(badge) cop_config = for_cop(badge.to_s) fetch(badge.department.to_s) { return cop_config } .merge(cop_config) end
@return [Config] for the given cop merged with that of its department (if any) Note: the 'Enabled' attribute is same as that returned by `for_cop`
for_badge
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
Apache-2.0
def for_department(department_name) @for_department ||= Hash.new do |h, dept| h[dept] = self[dept] || {} end @for_department[department_name.to_s] end
@return [Config] for the given department name. Note: the 'Enabled' attribute will be present only if specified at the department's level
for_department
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
Apache-2.0
def possibly_include_hidden? return @possibly_include_hidden if defined?(@possibly_include_hidden) @possibly_include_hidden = patterns_to_include.any? do |s| s.is_a?(Regexp) || s.start_with?('.') || s.include?('/.') end end
Returns true if there's a chance that an Include pattern matches hidden files, false if that's definitely not possible.
possibly_include_hidden?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
Apache-2.0
def base_dir_for_path_parameters @base_dir_for_path_parameters ||= if File.basename(loaded_path).start_with?('.rubocop') && loaded_path != File.join(Dir.home, ConfigLoader::DOTFILE) File.expand_path(File.dirname(loaded_path)) else Dir.pwd end end
Paths specified in configuration files starting with .rubocop are relative to the directory where that file is. Paths in other config files are relative to the current directory. This is so that paths in config/default.yml, for example, are not relative to RuboCop's config directory since that wouldn't work.
base_dir_for_path_parameters
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config.rb
Apache-2.0
def merge(base_hash, derived_hash) resolver.merge(base_hash, derived_hash) end
Return a recursive merge of two hashes. That is, a normal hash merge, with the addition that any value that is a hash, and occurs in both arguments, will also be merged. And so on.
merge
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
Apache-2.0
def configuration_file_for(target_dir) find_project_dotfile(target_dir) || find_user_dotfile || find_user_xdg_config || DEFAULT_FILE end
Returns the path of .rubocop.yml searching upwards in the directory structure starting at the given directory where the inspected file is. If no .rubocop.yml is found there, the user's home directory is checked. If there's no .rubocop.yml there either, the path to the default file is returned.
configuration_file_for
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
Apache-2.0
def project_root @project_root ||= find_project_root end
Returns the path rubocop inferred as the root of the project. No file searches will go past this directory.
project_root
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
Apache-2.0
def merge_with_default(config, config_file, unset_nil: true) resolver.merge_with_default(config, config_file, unset_nil: unset_nil) end
Merges the given configuration with the default one.
merge_with_default
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
Apache-2.0
def read_file(absolute_path) IO.read(absolute_path, encoding: Encoding::UTF_8) rescue Errno::ENOENT raise ConfigNotFoundError, "Configuration file not found: #{absolute_path}" end
Read the specified file, or exit with a friendly, concise message on stderr. Care is taken to use the standard OS exit code for a "file not found" error.
read_file
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader.rb
Apache-2.0
def merge_with_default(config, config_file, unset_nil:) default_configuration = ConfigLoader.default_configuration disabled_by_default = config.for_all_cops['DisabledByDefault'] enabled_by_default = config.for_all_cops['EnabledByDefault'] if disabled_by_default || enabled_by_default default_configuration = transform(default_configuration) do |params| params.merge('Enabled' => !disabled_by_default) end end config = handle_disabled_by_default(config, default_configuration) if disabled_by_default opts = { inherit_mode: config['inherit_mode'] || {}, unset_nil: unset_nil } Config.new(merge(default_configuration, config, **opts), config_file) end
Merges the given configuration with the default one. If AllCops:DisabledByDefault is true, it changes the Enabled params so that only cops from user configuration are enabled. If AllCops::EnabledByDefault is true, it changes the Enabled params so that only cops explicitly disabled in user configuration are disabled.
merge_with_default
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader_resolver.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader_resolver.rb
Apache-2.0
def merge(base_hash, derived_hash, **opts) result = base_hash.merge(derived_hash) keys_appearing_in_both = base_hash.keys & derived_hash.keys keys_appearing_in_both.each do |key| if opts[:unset_nil] && derived_hash[key].nil? result.delete(key) elsif base_hash[key].is_a?(Hash) result[key] = merge(base_hash[key], derived_hash[key], **opts) elsif should_union?(base_hash, key, opts[:inherit_mode]) result[key] = base_hash[key] | derived_hash[key] elsif opts[:debug] warn_on_duplicate_setting(base_hash, derived_hash, key, **opts) end end result end
Return a recursive merge of two hashes. That is, a normal hash merge, with the addition that any value that is a hash, and occurs in both arguments, will also be merged. And so on. rubocop:disable Metrics/AbcSize
merge
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader_resolver.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader_resolver.rb
Apache-2.0
def override_department_setting_for_cops(base_hash, derived_hash) derived_hash.each_key do |key| next unless key =~ %r{(.*)/.*} department = Regexp.last_match(1) next unless disabled?(derived_hash, department) || disabled?(base_hash, department) # The `override_department` setting for the `Enabled` parameter is an # internal setting that's not documented in the manual. It will cause a # cop to be enabled later, when logic surrounding enabled/disabled it # run, even though its department is disabled. derived_hash[key]['Enabled'] = 'override_department' if derived_hash[key]['Enabled'] end end
rubocop:enable Metrics/AbcSize An `Enabled: true` setting in user configuration for a cop overrides an `Enabled: false` setting for its department.
override_department_setting_for_cops
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader_resolver.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_loader_resolver.rb
Apache-2.0
def options # If there's no existing TODO file, generate one return DEFAULT_OPTIONS unless todo_exists? match = generation_command.match(COMMAND_REGEX) return DEFAULT_OPTIONS unless match options = match[1].split(' ') Options.new.parse(options).first end
Get options from the comment in the TODO file, and parse them as options
options
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_regeneration.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_regeneration.rb
Apache-2.0
def for(file_or_dir) dir = if File.directory?(file_or_dir) file_or_dir else File.dirname(file_or_dir) end for_dir(dir) end
If type (file/dir) is known beforehand, prefer using #for_file or #for_dir for improved performance
for
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_store.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_store.rb
Apache-2.0
def msg_not_boolean(parent, key, value) "#{Rainbow('').reset}" \ "Property #{Rainbow(key).yellow} of cop #{Rainbow(parent).yellow}" \ " is supposed to be a boolean and #{Rainbow(value).yellow} is not." end
FIXME: Handling colors in exception messages like this is ugly.
msg_not_boolean
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_validator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/config_validator.rb
Apache-2.0
def initialize(departments: []) @departments = departments.map(&:to_sym).sort! @cops = RuboCop::Cop::Registry.global @config = RuboCop::ConfigLoader.default_configuration end
This class will only generate documentation for cops that belong to one of the departments given in the `departments` array. E.g. if we only wanted documentation for Lint cops: CopsDocumentationGenerator.new(departments: ['Lint']).call
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cops_documentation_generator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cops_documentation_generator.rb
Apache-2.0
def configurable_values(pars, name) case name when /^Enforced/ supported_style_name = RuboCop::Cop::Util.to_supported_styles(name) format_table_value(pars[supported_style_name]) when 'IndentationWidth' 'Integer' when 'Database' format_table_value(pars['SupportedDatabases']) else case pars[name] when String 'String' when Integer 'Integer' when Float 'Float' when true, false 'Boolean' when Array 'Array' else '' end end end
rubocop:disable Metrics/CyclomaticComplexity,Metrics/MethodLength
configurable_values
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cops_documentation_generator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cops_documentation_generator.rb
Apache-2.0
def to_table(header, content) table = [ '|===', "| #{header.join(' | ')}\n\n" ].join("\n") marked_contents = content.map do |plain_content| plain_content.map { |c| "| #{c}" }.join("\n") end table << marked_contents.join("\n\n") table << "\n|===\n" end
rubocop:enable Metrics/CyclomaticComplexity,Metrics/MethodLength
to_table
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cops_documentation_generator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cops_documentation_generator.rb
Apache-2.0
def cops match = comment.text.match(CommentConfig::COMMENT_DIRECTIVE_REGEXP) return unless match cops_string = match.captures[1] cops_string.split(/,\s*/).uniq.sort end
Return all the cops specified in the directive
cops
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/directive_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/directive_comment.rb
Apache-2.0
def match?(cop_names) cops == cop_names.uniq.sort end
Checks if this directive contains all the given cop names
match?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/directive_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/directive_comment.rb
Apache-2.0
def frozen_string_literal? frozen_string_literal == true end
Does the magic comment enable the frozen string literal feature. Test whether the frozen string literal value is `true`. Cannot just return `frozen_string_literal` since an invalid magic comment like `# frozen_string_literal: yes` is possible and the truthy value `'yes'` does not actually enable the feature @return [Boolean]
frozen_string_literal?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
Apache-2.0
def frozen_string_literal return unless (setting = extract_frozen_string_literal) case setting when 'true' then true when 'false' then false else setting end end
Expose the `frozen_string_literal` value coerced to a boolean if possible. @return [Boolean] if value is `true` or `false` @return [nil] if frozen_string_literal comment isn't found @return [String] if comment is found but isn't true or false
frozen_string_literal
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
Apache-2.0
def extract(pattern) @comment[pattern, 1] end
Match the entire comment string with a pattern and take the first capture. @param pattern [Regexp] @return [String] if pattern matched @return [nil] otherwise
extract
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
Apache-2.0
def match(keyword) pattern = /\A#{keyword}\s*#{self.class::OPERATOR}\s*(#{TOKEN})\z/ tokens.each do |token| next unless (value = token[pattern, 1]) return value.downcase end nil end
Find a token starting with the provided keyword and extract its value. @param keyword [String] @return [String] extracted value if it is found @return [nil] otherwise
match
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
Apache-2.0
def encoding match('fileencoding') if tokens.size > 1 end
For some reason the fileencoding keyword only works if there is at least one other token included in the string. For example # works # vim: foo=bar, fileencoding=ascii-8bit # does nothing # vim: foo=bar, fileencoding=ascii-8bit
encoding
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/magic_comment.rb
Apache-2.0
def add_boolean_flags(opts) option(opts, '-F', '--fail-fast') option(opts, '-d', '--debug') option(opts, '-D', '--[no-]display-cop-names') option(opts, '-E', '--extra-details') option(opts, '-S', '--display-style-guide') option(opts, '-a', '--auto-correct') do @options[:safe_auto_correct] = true end option(opts, '--safe-auto-correct') do warn '--safe-auto-correct is deprecated; use --auto-correct' @options[:safe_auto_correct] = @options[:auto_correct] = true end option(opts, '-A', '--auto-correct-all') do @options[:auto_correct] = true end option(opts, '--disable-pending-cops') option(opts, '--enable-pending-cops') option(opts, '--ignore-disable-comments') option(opts, '--safe') option(opts, '--[no-]color') option(opts, '-v', '--version') option(opts, '-V', '--verbose-version') option(opts, '-P', '--parallel') end
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
add_boolean_flags
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
Apache-2.0
def add_aliases(opts) option(opts, '-l', '--lint') do @options[:only] ||= [] @options[:only] << 'Lint' end option(opts, '-x', '--fix-layout') do @options[:only] ||= [] @options[:only] << 'Layout' @options[:auto_correct] = true end end
rubocop:enable Metrics/MethodLength, Metrics/AbcSize
add_aliases
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
Apache-2.0
def option(opts, *args) long_opt_symbol = long_opt_symbol(args) args += Array(OptionsHelp::TEXT[long_opt_symbol]) opts.on(*args) do |arg| @options[long_opt_symbol] = arg yield arg if block_given? end end
Sets a value in the @options hash, based on the given long option and its value, in addition to calling the block if a block is given.
option
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
Apache-2.0
def long_opt_symbol(args) long_opt = args.find { |arg| arg.start_with?('--') } long_opt[2..-1].sub('[no-]', '').sub(/ .*/, '') .tr('-', '_').gsub(/[\[\]]/, '').to_sym end
Finds the option in `args` starting with -- and converts it to a symbol, e.g. [..., '--auto-correct', ...] to :auto_correct.
long_opt_symbol
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
Apache-2.0
def validate_cop_list(names) return unless names cop_names = Cop::Registry.global.names departments = Cop::Registry.global.departments.map(&:to_s) names.each do |name| next if cop_names.include?(name) next if departments.include?(name) next if %w[Syntax Lint/Syntax].include?(name) raise IncorrectCopNameError, format_message_from(name, cop_names) end end
Cop name validation must be done later than option parsing, so it's not called from within Options.
validate_cop_list
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/options.rb
Apache-2.0
def maybe_hidden_file?(path) separator_index = path.rindex(File::SEPARATOR) return false unless separator_index dot_index = path.index('.', separator_index + 1) dot_index == separator_index + 1 end
Loose check to reduce memory allocations
maybe_hidden_file?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/path_util.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/path_util.rb
Apache-2.0
def rubocop_checksum ResultCache.source_checksum ||= begin digest = Digest::SHA1.new rubocop_extra_features .select { |path| File.file?(path) } .sort! .each do |path| content = File.open(path, 'rb', &:read) digest << Zlib.crc32(content).to_s # mtime not reliable end digest << RuboCop::Version::STRING << RuboCop::AST::Version::STRING digest.hexdigest end end
The checksum of the rubocop program running the inspection.
rubocop_checksum
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
Apache-2.0
def relevant_options_digest(options) options = options.reject { |key, _| NON_CHANGING.include?(key) } options.to_s.gsub(/[^a-z]+/i, '_') end
Return a hash of the options given at invocation, minus the ones that have no effect on which offenses and disabled line ranges are found, and thus don't affect caching.
relevant_options_digest
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
Apache-2.0
def team_checksum(team) @checksum_by_team ||= {}.compare_by_identity @checksum_by_team[team] ||= team.external_dependency_checksum end
The external dependency checksums are cached per RuboCop team so that the checksums don't need to be recomputed for each file.
team_checksum
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
Apache-2.0
def context_checksum(team, options) Digest::SHA1.hexdigest([ team_checksum(team), relevant_options_digest(options) ].join) end
We combine team and options into a single "context" checksum to avoid making file names that are too long for some filesystems to handle. This context is for anything that's not (1) the RuboCop executable checksum or (2) the inspected file checksum.
context_checksum
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/result_cache.rb
Apache-2.0
def warm_cache(target_files) puts 'Running parallel inspection' if @options[:debug] Parallel.each(target_files, &method(:file_offenses)) end
Warms up the RuboCop cache by forking a suitable number of rubocop instances that each inspects its allotted group of files.
warm_cache
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/runner.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/runner.rb
Apache-2.0
def check_for_infinite_loop(processed_source, offenses_by_iteration) checksum = processed_source.checksum if (loop_start_index = @processed_sources.index(checksum)) raise InfiniteCorrectionLoop.new( processed_source.path, offenses_by_iteration, loop_start: loop_start_index ) end @processed_sources << checksum end
Check whether a run created source identical to a previous run, which means that we definitely have an infinite loop.
check_for_infinite_loop
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/runner.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/runner.rb
Apache-2.0
def standby_team(config) @team_by_config ||= {}.compare_by_identity @team_by_config[config] ||= Cop::Team.mobilize(mobilized_cop_classes(config), config, @options) end
A Cop::Team instance is stateful and may change when inspecting. The "standby" team for a given config is an initialized but otherwise dormant team that can be used for config- and option- level caching in ResultCache.
standby_team
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/runner.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/runner.rb
Apache-2.0
def find(args, mode) return target_files_in_dir if args.empty? files = [] args.uniq.each do |arg| files += if File.directory?(arg) target_files_in_dir(arg.chomp(File::SEPARATOR)) else process_explicit_path(arg, mode) end end files.map { |f| File.expand_path(f) }.uniq end
Generate a list of target files by expanding globbing patterns (if any). If args is empty, recursively find all Ruby source files under the current directory @return [Array] array of file paths
find
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/target_finder.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/target_finder.rb
Apache-2.0
def target_files_in_dir(base_dir = Dir.pwd) # Support Windows: Backslashes from command-line -> forward slashes base_dir = base_dir.gsub(File::ALT_SEPARATOR, File::SEPARATOR) if File::ALT_SEPARATOR all_files = find_files(base_dir, File::FNM_DOTMATCH) # use file.include? for performance optimization hidden_files = all_files.select { |file| file.include?(HIDDEN_PATH_SUBSTRING) } base_dir_config = @config_store.for(base_dir) target_files = all_files.select do |file| to_inspect?(file, hidden_files, base_dir_config) end target_files.sort_by!(&order) end
Finds all Ruby source files under the current or other supplied directory. A Ruby source file is defined as a file with the `.rb` extension or a file with no extension that has a ruby shebang line as its first line. It is possible to specify includes and excludes using the config file, so you can include other Ruby files like Rakefiles and gemspecs. @param base_dir Root directory under which to search for ruby source files @return [Array] Array of filenames
target_files_in_dir
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/target_finder.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/target_finder.rb
Apache-2.0
def find_files(base_dir, flags) # get all wanted directories first to improve speed of finding all files exclude_pattern = combined_exclude_glob_patterns(base_dir) dir_flags = flags | File::FNM_PATHNAME | File::FNM_EXTGLOB patterns = wanted_dir_patterns(base_dir, exclude_pattern, dir_flags) patterns.map! { |dir| File.join(dir, '*') } # We need this special case to avoid creating the pattern # /**/* which searches the entire file system. patterns = [File.join(dir, '**/*')] if patterns.empty? Dir.glob(patterns, flags).select { |path| FileTest.file?(path) } end
Search for files recursively starting at the given base directory using the given flags that determine how the match is made. Excluded files will be removed later by the caller, but as an optimization find_files removes the top level directories that are excluded in configuration in the normal way (dir/**/*).
find_files
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/target_finder.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/target_finder.rb
Apache-2.0
def run_line_length_cop puts Rainbow(PHASE_1).yellow @options[:only] = ['Layout/LineLength'] execute_runner @options.delete(:only) @config_store = ConfigStore.new # Save the todo configuration of the LineLength cop. IO.read(AUTO_GENERATED_FILE) .lines .drop_while { |line| line.start_with?('#') } .join end
Do an initial run with only Layout/LineLength so that cops that depend on Layout/LineLength:Max get the correct value for that parameter.
run_line_length_cop
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cli/command/auto_genenerate_config.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cli/command/auto_genenerate_config.rb
Apache-2.0
def range_by_lines(range) begin_of_first_line = range.begin_pos - range.column last_line = range.source_buffer.source_line(range.last_line) last_line_offset = last_line.length - range.last_column end_of_last_line = range.end_pos + last_line_offset Parser::Source::Range.new(range.source_buffer, begin_of_first_line, end_of_last_line) end
Expand the given range to include all of any lines it covers. Does not include newline at end of the last line.
range_by_lines
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/autocorrect_logic.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/autocorrect_logic.rb
Apache-2.0
def add_global_offense(message = nil, severity: nil) severity = find_severity(nil, severity) message = find_message(nil, message) @current_offenses << Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported) end
Adds an offense that has no particular location. No correction can be applied to global offenses
add_global_offense
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/base.rb
Apache-2.0
def add_offense(node_or_range, message: nil, severity: nil, &block) range = range_from_node_or_range(node_or_range) return unless current_offense_locations.add?(range) range_to_pass = callback_argument(range) severity = find_severity(range_to_pass, severity) message = find_message(range_to_pass, message) status, corrector = enabled_line?(range.line) ? correct(range, &block) : :disabled @current_offenses << Offense.new(severity, range, message, name, status, corrector) end
Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense. If message is not specified, the method `message` will be called.
add_offense
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/base.rb
Apache-2.0
def offenses raise 'The offenses are not directly available; ' \ 'they are returned as the result of the investigation' end
## Reserved for Cop::Cop @deprecated Make potential errors with previous API more obvious
offenses
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/base.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/base.rb
Apache-2.0
def with_cop_error_handling(cop, node = nil) yield rescue StandardError => e raise e if @options[:raise_error] err = ErrorWithAnalyzedFileLocation.new(cause: e, node: node, cop: cop) @errors << err end
Allow blind rescues here, since we're absorbing and packaging or re-raising exceptions that can be raised from within the individual cops' `#investigate` methods.
with_cop_error_handling
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/commissioner.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/commissioner.rb
Apache-2.0
def on_new_investigation investigate(processed_source) if respond_to?(:investigate) super end
Called before all on_... have been called
on_new_investigation
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/cop.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/cop.rb
Apache-2.0
def on_investigation_end investigate_post_walk(processed_source) if respond_to?(:investigate_post_walk) super end
Called after all on_... have been called
on_investigation_end
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/cop.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/cop.rb
Apache-2.0
def parse(source, path = nil) ProcessedSource.new(source, target_ruby_version, path) end
@deprecated Open issue if there's a valid use case to include this in Base
parse
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/cop.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/cop.rb
Apache-2.0
def initialize(source) source = self.class.source_buffer(source) super( source, different_replacements: :raise, swallowed_insertions: :raise, crossing_deletions: :accept ) # Don't print warnings to stderr if corrections conflict with each other diagnostics.consumer = ->(diagnostic) {} end
@param source [Parser::Source::Buffer, or anything leading to one via `(processed_source.)buffer`] corrector = Corrector.new(cop)
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
Apache-2.0
def remove_preceding(node_or_range, size) range = to_range(node_or_range) to_remove = range.with( begin_pos: range.begin_pos - size, end_pos: range.begin_pos ) remove(to_remove) end
Removes `size` characters prior to the source range. @param [Parser::Source::Range, Rubocop::AST::Node] range or node @param [Integer] size
remove_preceding
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
Apache-2.0
def remove_leading(node_or_range, size) range = to_range(node_or_range) to_remove = range.with(end_pos: range.begin_pos + size) remove(to_remove) end
Removes `size` characters from the beginning of the given range. If `size` is greater than the size of `range`, the removed region can overrun the end of `range`. @param [Parser::Source::Range, Rubocop::AST::Node] range or node @param [Integer] size
remove_leading
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
Apache-2.0
def remove_trailing(node_or_range, size) range = to_range(node_or_range) to_remove = range.with(begin_pos: range.end_pos - size) remove(to_remove) end
Removes `size` characters from the end of the given range. If `size` is greater than the size of `range`, the removed region can overrun the beginning of `range`. @param [Parser::Source::Range, Rubocop::AST::Node] range or node @param [Integer] size
remove_trailing
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/corrector.rb
Apache-2.0
def initialize(config, cop_name, cop_config, options) @config = config @cop_name = cop_name @cop_config = cop_config || {} @options = options end
@param config [RuboCop::Config] Check configs for all cops @note Message Annotator specifically checks the following config options for_all_cops :StyleGuideBaseURL [String] URL for styleguide :DisplayStyleGuide [Boolean] Include styleguide and reference URLs :ExtraDetails [Boolean] Include cop details :DisplayCopNames [Boolean] Include cop name @param [String] cop_name for specific cop name @param [Hash] cop_config configs for specific cop, from config#for_cop @option cop_config [String] :StyleGuide Extension of base styleguide URL @option cop_config [String] :Reference Full reference URL @option cop_config [String] :Details @param [Hash, nil] options optional @option options [Boolean] :display_style_guide Include style guide and reference URLs @option options [Boolean] :extra_details Include cop specific details @option options [Boolean] :debug Include debug output @option options [Boolean] :display_cop_names Include cop name
initialize
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/message_annotator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/message_annotator.rb
Apache-2.0
def annotate(message) message = "#{cop_name}: #{message}" if display_cop_names? message += " #{details}" if extra_details? && details if display_style_guide? links = urls.join(', ') message = "#{message} (#{links})" end message end
Returns the annotated message, based on params passed into initializer @return [String] annotated message
annotate
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/message_annotator.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/message_annotator.rb
Apache-2.0
def correctable? @status != :unsupported end
@api public @!attribute [r] correctable? @return [Boolean] whether this offense can be automatically corrected via autocorrect or a todo.
correctable?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def corrected? @status == :corrected || @status == :corrected_with_todo end
@api public @!attribute [r] corrected? @return [Boolean] whether this offense is automatically corrected via autocorrect or a todo.
corrected?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def corrected_with_todo? @status == :corrected_with_todo end
@api public @!attribute [r] corrected_with_todo? @return [Boolean] whether this offense is automatically disabled via a todo.
corrected_with_todo?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def disabled? @status == :disabled || @status == :todo end
@api public @!attribute [r] disabled? @return [Boolean] whether this offense was locally disabled with a disable or todo where it occurred.
disabled?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def highlighted_area Parser::Source::Range.new(source_line, column, column + column_length) end
@api public @return [Parser::Source::Range] the range of the code that is highlighted
highlighted_area
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def to_s format('%<severity>s:%3<line>d:%3<column>d: %<message>s', severity: severity.code, line: line, column: real_column, message: message) end
@api private This is just for debugging purpose.
to_s
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def real_column column + 1 end
@api private Internally we use column number that start at 0, but when outputting column numbers, we want them to start at 1. One reason is that editors, such as Emacs, expect this.
real_column
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/offense.rb
Apache-2.0
def departments clear_enrollment_queue @departments.keys end
@return [Array<Symbol>] list of departments for current cops.
departments
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/registry.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/registry.rb
Apache-2.0
def with_department(department) clear_enrollment_queue with(@departments.fetch(department, [])) end
@return [Registry] Cops for that specific department.
with_department
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/registry.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/registry.rb
Apache-2.0
def without_department(department) clear_enrollment_queue without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end
@return [Registry] Cops not for a specific department.
without_department
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/registry.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/registry.rb
Apache-2.0
def first_part_of_call_chain(node) while node case node.type when :send node = node.receiver when :block node = node.send_node else break end end node end
Returns, for example, a bare `if` node if the given node is an `if` with calls chained to the end of it.
first_part_of_call_chain
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/util.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/util.rb
Apache-2.0
def double_quotes_required?(string) # Double quotes are required for strings which either: # - Contain single quotes # - Contain non-printable characters, which must use an escape # Regex matches IF there is a ' or there is a \\ in the string that is # not preceded/followed by another \\ (e.g. "\\x34") but not "\\\\". /'|(?<! \\) \\{2}* \\ (?![\\"])/x.match?(string) end
If converting a string to Ruby string literal source code, must double quotes be used?
double_quotes_required?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/util.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/util.rb
Apache-2.0
def inspect_variables_in_scope(scope_node) variable_table.push_scope(scope_node) process_children(scope_node) variable_table.pop_scope end
This is called for each scope recursively.
inspect_variables_in_scope
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
Apache-2.0
def node_handler_method_name(node) case node.type when VARIABLE_ASSIGNMENT_TYPE :process_variable_assignment when REGEXP_NAMED_CAPTURE_TYPE :process_regexp_named_captures when MULTIPLE_ASSIGNMENT_TYPE :process_variable_multiple_assignment when VARIABLE_REFERENCE_TYPE :process_variable_referencing when RESCUE_TYPE :process_rescue when ZERO_ARITY_SUPER_TYPE :process_zero_arity_super when SEND_TYPE :process_send when *ARGUMENT_DECLARATION_TYPES :process_variable_declaration when *OPERATOR_ASSIGNMENT_TYPES :process_variable_operator_assignment when *LOOP_TYPES :process_loop when *SCOPE_TYPES :process_scope end end
rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
node_handler_method_name
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
Apache-2.0
def process_variable_declaration(node) variable_name = node.children.first # restarg and kwrestarg would have no name: # # def initialize(*) # end return unless variable_name variable_table.declare_variable(variable_name, node) end
rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity
process_variable_declaration
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
Apache-2.0
def mark_assignments_as_referenced_in_loop(node) referenced_variable_names_in_loop, assignment_nodes_in_loop = find_variables_in_loop(node) referenced_variable_names_in_loop.each do |name| variable = variable_table.find_variable(name) # Non related references which are caught in the above scan # would be skipped here. next unless variable variable.assignments.each do |assignment| next if assignment_nodes_in_loop.none? do |assignment_node| assignment_node.equal?(assignment.node) end assignment.reference!(node) end end end
Mark all assignments which are referenced in the same loop as referenced by ignoring AST order since they would be referenced in next iteration.
mark_assignments_as_referenced_in_loop
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
Apache-2.0
def scanned_node?(node) scanned_nodes.any? do |scanned_node| scanned_node.equal?(node) end end
Use Node#equal? for accurate check.
scanned_node?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/variable_force.rb
Apache-2.0
def precede?(node1, node2) node2.loc.line - node1.loc.line <= 1 end
The args node1 & node2 may represent a RuboCop::AST::Node or a Parser::Source::Comment. Both respond to #loc.
precede?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/bundler/gem_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/bundler/gem_comment.rb
Apache-2.0
def version_specified_gem?(node) # arguments[0] is the gem name node.arguments[1]&.str_type? end
Besides the gem name, all other *positional* arguments to `gem` are version specifiers, as long as it has one we know there's at least one version specifier.
version_specified_gem?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/bundler/gem_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/bundler/gem_comment.rb
Apache-2.0
def unexpected_indent_offset configured_indentation_width - expected_indent_offset end
An offset that is not expected, but correct if the configuration is changed.
unexpected_indent_offset
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/access_modifier_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/access_modifier_indentation.rb
Apache-2.0
def on_class(class_node) previous = -1 walk_over_nested_class_definition(class_node) do |node, category| index = expected_order.index(category) if index < previous message = format(MSG, category: category, previous: expected_order[previous]) add_offense(node, message: message) do |corrector| autocorrect(corrector, node) end end previous = index end end
Validates code style on class declaration. Add offense when find a node out of expected order.
on_class
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
Apache-2.0
def autocorrect(corrector, node) node_classification = classify(node) previous = node.left_siblings.find do |sibling| classification = classify(sibling) !ignore?(classification) && node_classification != classification end current_range = source_range_with_comment(node) previous_range = source_range_with_comment(previous) corrector.insert_before(previous_range, current_range.source) corrector.remove(current_range) end
Autocorrect by swapping between two nodes autocorrecting them
autocorrect
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
Apache-2.0
def classify(node) return node.to_s unless node.respond_to?(:type) case node.type when :block classify(node.send_node) when :send find_category(node) else humanize_node(node) end.to_s end
Classifies a node to match with something in the {expected_order} @param node to be analysed @return String when the node type is a `:block` then {classify} recursively with the first children @return String when the node type is a `:send` then {find_category} by method name @return String otherwise trying to {humanize_node} of the current node
classify
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
Apache-2.0
def find_category(node) name = node.method_name.to_s category, = categories.find { |_, names| names.include?(name) } key = category || name visibility_key = "#{node_visibility(node)}_#{key}" expected_order.include?(visibility_key) ? visibility_key : key end
Categorize a node according to the {expected_order} Try to match {categories} values against the node's method_name given also its visibility. @param node to be analysed. @return [String] with the key category or the `method_name` as string
find_category
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/class_structure.rb
Apache-2.0
def autocorrect_preceding_comments(comment) comments = processed_source.comments index = comments.index(comment) comments[0..index] .reverse_each .each_cons(2) .take_while { |below, above| should_correct?(above, below) } .map { |_, above| autocorrect_one(above) } end
Corrects all comment lines that occur immediately before the given comment and have the same indentation. This is to avoid a long chain of correcting, saving the file, parsing and inspecting again, and then correcting one more line, and so on.
autocorrect_preceding_comments
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/comment_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/comment_indentation.rb
Apache-2.0
def last_magic_comment(source) source .comments .take_while { |comment| comment.loc.line < source.ast.loc.line } .reverse .find { |comment| MagicComment.parse(comment.text).any? } end
Find the last magic comment in the source file. Take all comments that precede the first line of code, select the magic comments, and return the last magic comment in the file. @return [Parser::Source::Comment] if magic comments exist before code @return [nil] otherwise
last_magic_comment
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb
Apache-2.0
def on_begin(node) node.children.each_cons(2) do |prev, n| nodes = [prev, n] check_defs(nodes) if nodes.all?(&method(:def_node?)) end end
We operate on `begin` nodes, instead of using `OnMethodDef`, so that we can walk over pairs of consecutive nodes and efficiently access a node's predecessor; #prev_node ends up doing a linear scan over siblings, so we don't want to call it on each def.
on_begin
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/empty_line_between_defs.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/empty_line_between_defs.rb
Apache-2.0
def unimportant_missing_cr?(index, last_line, line) style == :crlf && index == last_line - 1 && !/\n$/.match?(line) end
If there is no LF on the last line, we don't care if there's no CR.
unimportant_missing_cr?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/end_of_line.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/end_of_line.rb
Apache-2.0
def ignored_ranges(ast) return [] unless ast @ignored_ranges ||= on_node(:pair, ast).map do |pair| next if pair.parent.single_line? key, value = *pair key.source_range.end_pos...value.source_range.begin_pos end.compact end
Returns an array of ranges that should not be reported. It's the extra spaces between the keys and values in a multiline hash, since those are handled by the Layout/HashAlignment cop.
ignored_ranges
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/extra_spacing.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/extra_spacing.rb
Apache-2.0
def column_of(range) source = range.source.strip if source.include?("\n") previous_code_line(range.line + source.count("\n") + 1) =~ /\S/ else display_column(range) end end
Returns the column of the given range. For single line ranges, this is simple. For ranges with line breaks, we look a the last code line.
column_of
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_argument_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_argument_indentation.rb
Apache-2.0
def previous_code_line(line_number) line = '' while line.blank? || comment_lines.include?(line_number) line_number -= 1 line = processed_source.lines[line_number - 1] end line end
Takes the line number of a given code line and returns a string containing the previous line that's not a comment line or a blank line.
previous_code_line
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_argument_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_argument_indentation.rb
Apache-2.0
def base_description(left_parenthesis) if style == :align_brackets 'the position of the opening bracket' elsif left_parenthesis && style == :special_inside_parentheses 'the first position after the preceding left parenthesis' else 'the start of the line where the left square bracket is' end end
Returns the description of what the correct indentation is based on.
base_description
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_array_element_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_array_element_indentation.rb
Apache-2.0
def base_description(left_parenthesis) if style == :align_braces 'the position of the opening brace' elsif left_parenthesis && style == :special_inside_parentheses 'the first position after the preceding left parenthesis' else 'the start of the line where the left curly brace is' end end
Returns the description of what the correct indentation is based on.
base_description
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_hash_element_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_hash_element_indentation.rb
Apache-2.0
def base_description(_) if style == brace_alignment_style 'the position of the opening parenthesis' else 'the start of the line where the left parenthesis is' end end
Returns the description of what the correct indentation is based on.
base_description
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_parameter_indentation.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/first_parameter_indentation.rb
Apache-2.0
def autocorrect(corrector, node) fix_closing_parenthesis(node, corrector) remove_internal_trailing_comma(node, corrector) if internal_trailing_comma?(node) fix_external_trailing_comma(node, corrector) if external_trailing_comma?(node) end
Autocorrection note: Commas are a bit tricky to handle when the method call is embedded in another expression. Here's an example: [ first_array_value, foo(<<-SQL, 123, 456, SELECT * FROM db SQL ), third_array_value, ] The "internal" trailing comma is after `456`. The "external" trailing comma is after `)`. To autocorrect, we remove the latter, and move the former up: [ first_array_value, foo(<<-SQL, 123, 456), SELECT * FROM db SQL third_array_value, ]
autocorrect
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
Apache-2.0
def internal_trailing_comma_offset_from_last_arg(node) source_after_last_arg = range_between( node.children.last.source_range.end_pos, node.loc.end.begin_pos ).source first_comma_offset = source_after_last_arg.index(',') first_new_line_offset = source_after_last_arg.index("\n") return if first_comma_offset.nil? return if first_new_line_offset.nil? return if first_comma_offset > first_new_line_offset first_comma_offset + 1 end
Returns nil if no trailing internal comma.
internal_trailing_comma_offset_from_last_arg
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
Apache-2.0
def external_trailing_comma_offset_from_loc_end(node) end_pos = node.source_range.end_pos offset = 0 limit = 20 offset += 1 while offset < limit && space?(end_pos + offset) char = processed_source.buffer.source[end_pos + offset] return unless char == ',' offset + 1 # Add one to include the comma. end
Returns nil if no trailing external comma.
external_trailing_comma_offset_from_loc_end
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb
Apache-2.0
def bare_access_modifier?(node) node.send_type? && node.bare_access_modifier? end
Not all nodes define `bare_access_modifier?` (for example, `RuboCop::AST::DefNode` does not), so we must check `send_type?` first to avoid a NoMethodError.
bare_access_modifier?
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/indentation_consistency.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/indentation_consistency.rb
Apache-2.0
def base_column_for_normal_style(node) first_child = node.children.first return unless first_child && bare_access_modifier?(first_child) # If, as is most common, the access modifier is indented deeper than # the module (`access_modifier_indent > module_indent`) then the # indentation of the access modifier determines the correct # indentation. # # Otherwise, in the rare event that the access modifier is outdented # to the level of the module (see `AccessModifierIndentation` cop) we # return nil so that `check_alignment` will derive the correct # indentation from the first child that is not an access modifier. module_indent = display_column(node.parent.source_range) access_modifier_indent = display_column(first_child.source_range) access_modifier_indent if access_modifier_indent > module_indent end
Returns an integer representing the correct indentation, or nil to indicate that the correct indentation is that of the first child that is not an access modifier.
base_column_for_normal_style
ruby
collabnix/kubelabs
.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/indentation_consistency.rb
https://github.com/collabnix/kubelabs/blob/master/.bundles_cache/ruby/2.6.0/gems/rubocop-0.93.1/lib/rubocop/cop/layout/indentation_consistency.rb
Apache-2.0