summaryrefslogtreecommitdiff
path: root/rubocop/cop/custom_error_class.rb
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2017-12-22 15:17:28 +0000
committerRobert Speicher <robert@gitlab.com>2017-12-22 15:17:28 +0000
commit217d4a19f2af8e219971a3c616c69cc422f17d0c (patch)
treecc5a0f06ba04448dbf025dd95a6e39686343d256 /rubocop/cop/custom_error_class.rb
parent299e04e318728d4ee5857379f8fb350c944a0b4e (diff)
parenta2d39b80109f006ff63752cfaed5e458f9443d1d (diff)
downloadgitlab-ce-217d4a19f2af8e219971a3c616c69cc422f17d0c.tar.gz
Merge branch 'rc/use-gitlab-styles' into 'master'
Use gitlab-styles which provides shared RuboCop config and cops Closes #37711 See merge request gitlab-org/gitlab-ce!14337
Diffstat (limited to 'rubocop/cop/custom_error_class.rb')
-rw-r--r--rubocop/cop/custom_error_class.rb64
1 files changed, 0 insertions, 64 deletions
diff --git a/rubocop/cop/custom_error_class.rb b/rubocop/cop/custom_error_class.rb
deleted file mode 100644
index 38d93acfe88..00000000000
--- a/rubocop/cop/custom_error_class.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-module RuboCop
- module Cop
- # This cop makes sure that custom error classes, when empty, are declared
- # with Class.new.
- #
- # @example
- # # bad
- # class FooError < StandardError
- # end
- #
- # # okish
- # class FooError < StandardError; end
- #
- # # good
- # FooError = Class.new(StandardError)
- class CustomErrorClass < RuboCop::Cop::Cop
- MSG = 'Use `Class.new(SuperClass)` to define an empty custom error class.'.freeze
-
- def on_class(node)
- _klass, parent, body = node.children
-
- return if body
-
- parent_klass = class_name_from_node(parent)
-
- return unless parent_klass && parent_klass.to_s.end_with?('Error')
-
- add_offense(node, :expression)
- end
-
- def autocorrect(node)
- klass, parent, _body = node.children
- replacement = "#{class_name_from_node(klass)} = Class.new(#{class_name_from_node(parent)})"
-
- lambda do |corrector|
- corrector.replace(node.source_range, replacement)
- end
- end
-
- private
-
- # The nested constant `Foo::Bar::Baz` looks like:
- #
- # s(:const,
- # s(:const,
- # s(:const, nil, :Foo), :Bar), :Baz)
- #
- # So recurse through that to get the name as written in the source.
- #
- def class_name_from_node(node, suffix = nil)
- return unless node&.type == :const
-
- name = node.children[1].to_s
- name = "#{name}::#{suffix}" if suffix
-
- if node.children[0]
- class_name_from_node(node.children[0], name)
- else
- name
- end
- end
- end
- end
-end