|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RuboCop |
| 4 | + module Cop |
| 5 | + module Committee |
| 6 | + # Check for unused schema coverage setup without any report call. |
| 7 | + # |
| 8 | + # @example |
| 9 | + # # bad |
| 10 | + # before do |
| 11 | + # @schema_coverage = Committee::Test::SchemaCoverage.new(schema) |
| 12 | + # @committee_options[:schema_coverage] = @schema_coverage |
| 13 | + # end |
| 14 | + # |
| 15 | + # # good |
| 16 | + # before do |
| 17 | + # @schema_coverage = Committee::Test::SchemaCoverage.new(schema) |
| 18 | + # @committee_options[:schema_coverage] = @schema_coverage |
| 19 | + # end |
| 20 | + # |
| 21 | + # after(:all) do |
| 22 | + # @schema_coverage.report |
| 23 | + # end |
| 24 | + # |
| 25 | + class UnusedSchemaCoverage < Base |
| 26 | + MSG = "Call `report` or `report_flatten` when schema coverage is set up." |
| 27 | + |
| 28 | + EXAMPLE_GROUP_METHODS = %i[describe context feature].freeze |
| 29 | + |
| 30 | + # @!method schema_coverage_assignment(node) |
| 31 | + def_node_search :schema_coverage_assignment, <<~PATTERN |
| 32 | + (ivasgn :@schema_coverage (send (const (const (const nil? :Committee) :Test) :SchemaCoverage) :new ...)) |
| 33 | + PATTERN |
| 34 | + |
| 35 | + # @!method schema_coverage_report?(node) |
| 36 | + def_node_search :schema_coverage_report?, <<~PATTERN |
| 37 | + (send (ivar :@schema_coverage) {:report :report_flatten}) |
| 38 | + PATTERN |
| 39 | + |
| 40 | + def on_block(node) |
| 41 | + return unless example_group?(node) |
| 42 | + |
| 43 | + assignment = schema_coverage_assignment(node).first |
| 44 | + return unless assignment |
| 45 | + return if schema_coverage_report?(node) |
| 46 | + |
| 47 | + add_offense(assignment) |
| 48 | + end |
| 49 | + alias on_numblock on_block |
| 50 | + |
| 51 | + private |
| 52 | + |
| 53 | + def example_group?(node) |
| 54 | + send_node = node.send_node |
| 55 | + return false unless send_node&.send_type? |
| 56 | + |
| 57 | + EXAMPLE_GROUP_METHODS.include?(send_node.method_name) |
| 58 | + end |
| 59 | + end |
| 60 | + end |
| 61 | + end |
| 62 | +end |
0 commit comments