2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative '../../code_reuse_helpers'
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module CodeReuse
|
2021-01-03 14:25:43 +05:30
|
|
|
# Cop that denies the use of ActiveRecord methods outside of models.
|
2018-12-05 23:21:45 +05:30
|
|
|
class ActiveRecord < RuboCop::Cop::Cop
|
|
|
|
include CodeReuseHelpers
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
MSG = 'This method can only be used inside an ActiveRecord model: ' \
|
2019-12-04 20:38:33 +05:30
|
|
|
'https://gitlab.com/gitlab-org/gitlab-foss/issues/49653'
|
2018-12-05 23:21:45 +05:30
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
# Various methods from ActiveRecord::Querying that are denied. We
|
2018-12-05 23:21:45 +05:30
|
|
|
# exclude some generic ones such as `any?` and `first`, as these may
|
|
|
|
# lead to too many false positives, since `Array` also supports these
|
|
|
|
# methods.
|
|
|
|
#
|
2021-01-03 14:25:43 +05:30
|
|
|
# The keys of this Hash are the denied method names. The values are
|
|
|
|
# booleans that indicate if the method should only be denied if any
|
2018-12-05 23:21:45 +05:30
|
|
|
# arguments are provided.
|
|
|
|
NOT_ALLOWED = {
|
|
|
|
average: true,
|
|
|
|
calculate: true,
|
|
|
|
count_by_sql: true,
|
|
|
|
create_with: true,
|
|
|
|
distinct: false,
|
|
|
|
eager_load: true,
|
|
|
|
exists?: true,
|
|
|
|
find_by: true,
|
|
|
|
find_by!: true,
|
|
|
|
find_by_sql: true,
|
|
|
|
find_each: true,
|
|
|
|
find_in_batches: true,
|
|
|
|
find_or_create_by: true,
|
|
|
|
find_or_create_by!: true,
|
|
|
|
find_or_initialize_by: true,
|
|
|
|
first!: false,
|
|
|
|
first_or_create: true,
|
|
|
|
first_or_create!: true,
|
|
|
|
first_or_initialize: true,
|
|
|
|
from: true,
|
|
|
|
group: true,
|
|
|
|
having: true,
|
|
|
|
ids: false,
|
|
|
|
includes: true,
|
|
|
|
joins: true,
|
|
|
|
limit: true,
|
|
|
|
lock: false,
|
|
|
|
many?: false,
|
|
|
|
offset: true,
|
|
|
|
order: true,
|
|
|
|
pluck: true,
|
|
|
|
preload: true,
|
|
|
|
readonly: false,
|
|
|
|
references: true,
|
|
|
|
reorder: true,
|
|
|
|
rewhere: true,
|
|
|
|
take: false,
|
|
|
|
take!: false,
|
|
|
|
unscope: false,
|
|
|
|
where: false,
|
|
|
|
with: true
|
|
|
|
}.freeze
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
# Directories that allow the use of the denied methods. These
|
2018-12-05 23:21:45 +05:30
|
|
|
# directories are checked relative to both . and ee/
|
2021-01-03 14:25:43 +05:30
|
|
|
ALLOWED_DIRECTORIES = %w[
|
2018-12-05 23:21:45 +05:30
|
|
|
app/models
|
|
|
|
config
|
|
|
|
danger
|
|
|
|
db
|
|
|
|
lib/backup
|
|
|
|
lib/banzai
|
|
|
|
lib/gitlab/background_migration
|
|
|
|
lib/gitlab/cycle_analytics
|
|
|
|
lib/gitlab/database
|
|
|
|
lib/gitlab/import_export
|
|
|
|
lib/gitlab/project_authorizations
|
|
|
|
lib/gitlab/sql
|
|
|
|
lib/system_check
|
|
|
|
lib/tasks
|
|
|
|
qa
|
|
|
|
rubocop
|
|
|
|
spec
|
|
|
|
].freeze
|
|
|
|
|
|
|
|
def on_send(node)
|
2021-01-03 14:25:43 +05:30
|
|
|
return if in_allowed_directory?(node)
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
receiver = node.children[0]
|
|
|
|
send_name = node.children[1]
|
|
|
|
first_arg = node.children[2]
|
|
|
|
|
|
|
|
if receiver && NOT_ALLOWED.key?(send_name)
|
|
|
|
# If the rule requires an argument to be given, but none are
|
|
|
|
# provided, we won't register an offense. This prevents us from
|
|
|
|
# adding offenses for `project.group`, while still covering
|
|
|
|
# `Project.group(:name)`.
|
|
|
|
return if NOT_ALLOWED[send_name] && !first_arg
|
|
|
|
|
|
|
|
add_offense(node, location: :selector)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
# Returns true if the node resides in one of the allowed
|
2018-12-05 23:21:45 +05:30
|
|
|
# directories.
|
2021-01-03 14:25:43 +05:30
|
|
|
def in_allowed_directory?(node)
|
2018-12-05 23:21:45 +05:30
|
|
|
path = file_path_for_node(node)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
ALLOWED_DIRECTORIES.any? do |directory|
|
2018-12-05 23:21:45 +05:30
|
|
|
path.start_with?(
|
|
|
|
File.join(rails_root, directory),
|
|
|
|
File.join(rails_root, 'ee', directory)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# We can not auto correct code like this, as it requires manual
|
2021-01-03 14:25:43 +05:30
|
|
|
# refactoring. Instead, we'll just allow the surrounding scope.
|
2018-12-05 23:21:45 +05:30
|
|
|
#
|
|
|
|
# Despite this method's presence, you should not use it. This method
|
2021-01-03 14:25:43 +05:30
|
|
|
# exists to make it possible to allow large chunks of offenses we
|
2018-12-05 23:21:45 +05:30
|
|
|
# can't fix in the short term. If you are writing new code, follow the
|
2021-01-03 14:25:43 +05:30
|
|
|
# code reuse guidelines, instead of allowing any new offenses.
|
2018-12-05 23:21:45 +05:30
|
|
|
def autocorrect(node)
|
|
|
|
scope = surrounding_scope_of(node)
|
|
|
|
indent = indentation_of(scope)
|
|
|
|
|
|
|
|
lambda do |corrector|
|
|
|
|
# This prevents us from inserting the same enable/disable comment
|
|
|
|
# for a method or block that has multiple offenses.
|
2021-01-03 14:25:43 +05:30
|
|
|
next if allowed_scopes.include?(scope)
|
2018-12-05 23:21:45 +05:30
|
|
|
|
|
|
|
corrector.insert_before(
|
|
|
|
scope.source_range,
|
|
|
|
"# rubocop: disable #{cop_name}\n#{indent}"
|
|
|
|
)
|
|
|
|
|
|
|
|
corrector.insert_after(
|
|
|
|
scope.source_range,
|
|
|
|
"\n#{indent}# rubocop: enable #{cop_name}"
|
|
|
|
)
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
allowed_scopes << scope
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def indentation_of(node)
|
|
|
|
' ' * node.loc.expression.source_line[/\A */].length
|
|
|
|
end
|
|
|
|
|
|
|
|
def surrounding_scope_of(node)
|
|
|
|
%i[def defs block begin].each do |type|
|
|
|
|
if (found = node.each_ancestor(type).first)
|
|
|
|
return found
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-03 14:25:43 +05:30
|
|
|
def allowed_scopes
|
|
|
|
@allowed_scopes ||= Set.new
|
2018-12-05 23:21:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|