Skip to content

Commit

Permalink
Enable Performance/MapCompact cop
Browse files Browse the repository at this point in the history
Follow up to #42053.
  • Loading branch information
kamipo committed Apr 23, 2021
1 parent c4b944a commit bbbc861
Show file tree
Hide file tree
Showing 20 changed files with 49 additions and 40 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Expand Up @@ -253,6 +253,9 @@ Performance/BindCall:
Performance/FlatMap:
Enabled: true

Performance/MapCompact:
Enabled: true

Performance/RedundantMerge:
Enabled: true

Expand Down
16 changes: 13 additions & 3 deletions Gemfile.lock
Expand Up @@ -221,6 +221,9 @@ GEM
tzinfo
event_emitter (0.2.6)
eventmachine (1.2.7)
eventmachine (1.2.7-java)
eventmachine (1.2.7-x64-mingw32)
eventmachine (1.2.7-x86-mingw32)
execjs (2.7.0)
faraday (1.3.0)
faraday-net_http (~> 1.0)
Expand Down Expand Up @@ -288,12 +291,14 @@ GEM
hiredis (0.6.3)
hiredis (0.6.3-java)
http_parser.rb (0.6.0)
http_parser.rb (0.6.0-java)
httpclient (2.8.3)
i18n (1.8.10)
concurrent-ruby (~> 1.0)
image_processing (1.12.1)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.17, < 3)
jar-dependencies (0.4.1)
jdbc-mysql (5.1.47)
jdbc-postgres (42.2.14)
jdbc-sqlite3 (3.28.0)
Expand All @@ -305,6 +310,7 @@ GEM
mustache
nokogiri
libxml-ruby (3.2.1)
libxml-ruby (3.2.1-x64-mingw32)
listen (3.5.1)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
Expand Down Expand Up @@ -361,7 +367,11 @@ GEM
ast (~> 2.4.1)
path_expander (1.1.0)
pg (1.2.3)
pg (1.2.3-x64-mingw32)
pg (1.2.3-x86-mingw32)
psych (3.3.1)
psych (3.3.1-java)
jar-dependencies (>= 0.1.7)
public_suffix (4.0.6)
puma (5.2.2)
nio4r (~> 2.0)
Expand Down Expand Up @@ -417,7 +427,7 @@ GEM
retriable (3.1.2)
rexml (3.2.5)
rouge (3.26.0)
rubocop (1.12.1)
rubocop (1.13.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
rainbow (>= 2.2.2, < 4.0)
Expand All @@ -430,8 +440,8 @@ GEM
parser (>= 2.7.1.5)
rubocop-packaging (0.5.1)
rubocop (>= 0.89, < 2.0)
rubocop-performance (1.10.2)
rubocop (>= 0.90.0, < 2.0)
rubocop-performance (1.11.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.9.1)
activesupport (>= 4.2.0)
Expand Down
Expand Up @@ -130,7 +130,7 @@ def visualizer(paths, title = "FSM")
states = "function tt() { return #{to_json}; }"

fun_routes = paths.sample(3).map do |ast|
ast.map { |n|
ast.filter_map { |n|
case n
when Nodes::Symbol
case n.left
Expand All @@ -143,7 +143,7 @@ def visualizer(paths, title = "FSM")
else
nil
end
}.compact.join
}.join
end

stylesheets = [fsm_css]
Expand Down
2 changes: 1 addition & 1 deletion actiontext/lib/action_text/encryption.rb
Expand Up @@ -31,7 +31,7 @@ def has_encrypted_rich_texts?

def encryptable_rich_texts
@encryptable_rich_texts ||= self.class.rich_text_association_names
.collect { |attribute_name| send(attribute_name) }.compact
.filter_map { |attribute_name| send(attribute_name) }
.find_all { |record| record.is_a?(ActionText::EncryptedRichText) }
end
end
Expand Down
4 changes: 2 additions & 2 deletions actionview/lib/action_view/helpers/form_options_helper.rb
Expand Up @@ -793,9 +793,9 @@ def extract_selected_and_disabled(selected)

def extract_values_from_collection(collection, value_method, selected)
if selected.is_a?(Proc)
collection.map do |element|
collection.filter_map do |element|
element.public_send(value_method) if selected.call(element)
end.compact
end
else
selected
end
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/attribute_methods.rb
Expand Up @@ -399,7 +399,7 @@ def attribute_method_matchers_cache

def attribute_method_matchers_matching(method_name)
attribute_method_matchers_cache.compute_if_absent(method_name) do
attribute_method_matchers.map { |matcher| matcher.match(method_name) }.compact
attribute_method_matchers.filter_map { |matcher| matcher.match(method_name) }
end
end

Expand Down
8 changes: 4 additions & 4 deletions activemodel/lib/active_model/validations/confirmation.rb
Expand Up @@ -19,13 +19,13 @@ def validate_each(record, attribute, value)

private
def setup!(klass)
klass.attr_reader(*attributes.map do |attribute|
klass.attr_reader(*attributes.filter_map do |attribute|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation")
end.compact)
end)

klass.attr_writer(*attributes.map do |attribute|
klass.attr_writer(*attributes.filter_map do |attribute|
:"#{attribute}_confirmation" unless klass.method_defined?(:"#{attribute}_confirmation=")
end.compact)
end)
end

def confirmation_value_equal?(record, attribute, value, confirmed)
Expand Down
Expand Up @@ -46,9 +46,9 @@ def target_classes

def likely_reflections
parent_classes = parent.target_classes
parent_classes.map do |parent_klass|
parent_classes.filter_map do |parent_klass|
parent_klass._reflect_on_association(@association)
end.compact
end
end

def root?
Expand Down
Expand Up @@ -498,10 +498,10 @@ def build_fixture_sql(fixtures, table_name)
end

def build_fixture_statements(fixture_set)
fixture_set.map do |table_name, fixtures|
fixture_set.filter_map do |table_name, fixtures|
next if fixtures.empty?
build_fixture_sql(fixtures, table_name)
end.compact
end
end

def build_truncate_statement(table_name)
Expand Down
Expand Up @@ -787,14 +787,13 @@ def configure_connection
end

# Gather up all of the SET variables...
variable_assignments = variables.map do |k, v|
variable_assignments = variables.filter_map do |k, v|
if defaults.include?(v)
"@@SESSION.#{k} = DEFAULT" # Sets the value to the global or compile default
elsif !v.nil?
"@@SESSION.#{k} = #{quote(v)}"
end
# or else nil; compact to clear nils out
end.compact.join(", ")
end.join(", ")

# ...and send them all in one query
execute("SET #{encoding} #{sql_mode_assignment} #{variable_assignments}", "SCHEMA")
Expand Down
Expand Up @@ -917,9 +917,7 @@ def add_pg_decoders
WHERE t.typname IN (%s)
SQL
coders = execute_and_clear(query, "SCHEMA", []) do |result|
result
.map { |row| construct_coder(row, coders_by_name[row["typname"]]) }
.compact
result.filter_map { |row| construct_coder(row, coders_by_name[row["typname"]]) }
end

map = PG::TypeMapByOid.new
Expand Down
Expand Up @@ -6,7 +6,7 @@ module SQLite3
module SchemaStatements # :nodoc:
# Returns an array of indexes for the given table.
def indexes(table_name)
exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row|
exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").filter_map do |row|
# Indexes SQLite creates implicitly for internal use start with "sqlite_".
# See https://www.sqlite.org/fileformat2.html#intschema
next if row["name"].start_with?("sqlite_")
Expand Down Expand Up @@ -49,7 +49,7 @@ def indexes(table_name)
where: where,
orders: orders
)
end.compact
end
end

def add_foreign_key(from_table, to_table, **options)
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -796,11 +796,11 @@ def inspect
# We check defined?(@attributes) not to issue warnings if the object is
# allocated but not initialized.
inspection = if defined?(@attributes) && @attributes
self.class.attribute_names.collect do |name|
self.class.attribute_names.filter_map do |name|
if _has_attribute?(name)
"#{name}: #{attribute_for_inspect(name)}"
end
end.compact.join(", ")
end.join(", ")
else
"not initialized"
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/insert_all.rb
Expand Up @@ -196,11 +196,11 @@ def updatable_columns
end

def touch_model_timestamps_unless(&block)
model.timestamp_attributes_for_update_in_model.map do |column_name|
model.timestamp_attributes_for_update_in_model.filter_map do |column_name|
if touch_timestamp_attribute?(column_name)
"#{column_name}=(CASE WHEN (#{updatable_columns.map(&block).join(" AND ")}) THEN #{model.quoted_table_name}.#{column_name} ELSE CURRENT_TIMESTAMP END),"
end
end.compact.join
end.join
end

def raw_update_sql
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -1159,13 +1159,13 @@ def migrations # :nodoc:
def migrations_status # :nodoc:
db_list = schema_migration.normalized_versions

file_list = migration_files.map do |file|
file_list = migration_files.filter_map do |file|
version, name, scope = parse_migration_filename(file)
raise IllegalMigrationNameError.new(file) unless version
version = schema_migration.normalize_migration_number(version)
status = db_list.delete(version) ? "up" : "down"
[status, version, (name + scope).humanize]
end.compact
end

db_list.map! do |version|
["up", version, "********** NO FILE **********"]
Expand Down
Expand Up @@ -94,7 +94,7 @@ def prepare_command_options
sslcapath: "--ssl-capath",
sslcipher: "--ssl-cipher",
sslkey: "--ssl-key"
}.map { |opt, arg| "#{arg}=#{configuration_hash[opt]}" if configuration_hash[opt] }.compact
}.filter_map { |opt, arg| "#{arg}=#{configuration_hash[opt]}" if configuration_hash[opt] }

args
end
Expand Down
3 changes: 1 addition & 2 deletions activerecord/lib/active_record/timestamp.rb
Expand Up @@ -148,8 +148,7 @@ def current_time_from_proper_timezone

def max_updated_column_timestamp
timestamp_attributes_for_update_in_model
.map { |attr| self[attr]&.to_time }
.compact
.filter_map { |attr| self[attr]&.to_time }
.max
end

Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/core_ext/object/to_query.rb
Expand Up @@ -75,11 +75,11 @@ class Hash
#
# This method is also aliased as +to_param+.
def to_query(namespace = nil)
query = collect do |key, value|
query = filter_map do |key, value|
unless (value.is_a?(Hash) || value.is_a?(Array)) && value.empty?
value.to_query(namespace ? "#{namespace}[#{key}]" : key)
end
end.compact
end

query.sort! unless namespace.to_s.include?("[]")
query.join("&")
Expand Down
Expand Up @@ -45,13 +45,13 @@ def attributes_string
def attributes_hash
return {} if attributes_names.empty?

attributes_names.map do |name|
attributes_names.filter_map do |name|
if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?)
["#{name}", "'secret'"]
elsif !virtual?(name)
["#{name}", "@#{singular_table_name}.#{name}"]
end
end.compact.sort.to_h
end.sort.to_h
end

def boolean?(name)
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/tasks/zeitwerk.rake
Expand Up @@ -54,11 +54,11 @@ namespace :zeitwerk do
end

require "active_support/core_ext/object/try"
eager_load_paths = Rails.configuration.eager_load_namespaces.map do |eln|
eager_load_paths = Rails.configuration.eager_load_namespaces.filter_map do |eln|
# Quick regression fix for 6.0.3 to support namespaces that do not have
# eager load paths, like the recently added i18n. I'll rewrite this task.
eln.try(:config).try(:eager_load_paths)
end.compact.flatten
end.flatten

not_checked = ActiveSupport::Dependencies.autoload_paths - eager_load_paths
not_checked.select! { |dir| Dir.exist?(dir) }
Expand Down

0 comments on commit bbbc861

Please sign in to comment.