lib/rubocop/cop/rspec/described_class.rb in rubocop-rspec-2.10.0 vs lib/rubocop/cop/rspec/described_class.rb in rubocop-rspec-2.11.0

- old
+ new

@@ -76,13 +76,12 @@ def_node_matcher :described_constant, <<-PATTERN (block (send _ :describe $(const ...) ...) (args) $_) PATTERN # @!method contains_described_class?(node) - def_node_search :contains_described_class?, <<-PATTERN - (send nil? :described_class) - PATTERN + def_node_search :contains_described_class?, + '(send nil? :described_class)' def on_block(node) # In case the explicit style is used, we need to remember what's # being described. @described_class, body = described_constant(node) @@ -127,33 +126,30 @@ src: DESCRIBED_CLASS) end end def scope_change?(node) - scope_changing_syntax?(node) || + scope_changing_syntax?(node) || common_instance_exec_closure?(node) || skippable_block?(node) end def skippable_block?(node) - node.block_type? && !rspec_block?(node) && skip_blocks? + node.block_type? && !rspec_block?(node) && cop_config['SkipBlocks'] end - def skip_blocks? - cop_config['SkipBlocks'] - end - def offensive?(node) if style == :described_class offensive_described_class?(node) else node.send_type? && node.method?(:described_class) end end def offensive_described_class?(node) return unless node.const_type? + # E.g. `described_class::CONSTANT` return if contains_described_class?(node) nearest_described_class, = node.each_ancestor(:block) .map { |ancestor| described_constant(ancestor) }.find(&:itself) @@ -170,18 +166,17 @@ # @param namespace [Array<Symbol>] # @param const [Array<Symbol>] # @return [Array<Symbol>] # @example # # nil represents base constant - # collapse_namespace([], :C) # => [:C] - # collapse_namespace([:A, :B], [:C) # => [:A, :B, :C] - # collapse_namespace([:A, :B], [:B, :C) # => [:A, :B, :C] - # collapse_namespace([:A, :B], [nil, :C) # => [nil, :C] - # collapse_namespace([:A, :B], [nil, :B, :C) # => [nil, :B, :C] + # collapse_namespace([], [:C]) # => [:C] + # collapse_namespace([:A, :B], [:C]) # => [:A, :B, :C] + # collapse_namespace([:A, :B], [:B, :C]) # => [:A, :B, :C] + # collapse_namespace([:A, :B], [nil, :C]) # => [nil, :C] + # collapse_namespace([:A, :B], [nil, :B, :C]) # => [nil, :B, :C] def collapse_namespace(namespace, const) - return const if namespace.empty? - return const if const.first.nil? + return const if namespace.empty? || const.first.nil? start = [0, (namespace.length - const.length)].max max = namespace.length intersection = (start..max).find do |shift| namespace[shift, max - shift] == const[0, max - shift] @@ -194,12 +189,10 @@ # @example # const_name(s(:const, nil, :C)) # => [:C] # const_name(s(:const, s(:const, nil, :M), :C)) # => [:M, :C] # const_name(s(:const, s(:cbase), :C)) # => [nil, :C] def const_name(node) - # rubocop:disable InternalAffairs/NodeDestructuring - namespace, name = *node - # rubocop:enable InternalAffairs/NodeDestructuring + namespace, name = *node # rubocop:disable InternalAffairs/NodeDestructuring if !namespace [name] elsif namespace.const_type? [*const_name(namespace), name] elsif %i[lvar cbase send].include?(namespace.type)