Skip to content

Commit

Permalink
Merge pull request #59 from Shopify/detect_bad_events
Browse files Browse the repository at this point in the history
Detect bad events in the rubocop cop
  • Loading branch information
Lisa Ugray authored Jan 10, 2023
2 parents 2cc4e5a + 4889aa3 commit 28d7596
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
33 changes: 24 additions & 9 deletions lib/smart_todo_cop.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "smart_todo/parser/metadata_parser"
require "smart_todo"

module RuboCop
module Cop
Expand All @@ -10,29 +10,44 @@ module SmartTodo
#
# @see https://rubocop.readthedocs.io/en/latest/extensions/#loading-extensions
class SmartTodoCop < Cop
MSG = "Don't write regular TODO comments. Write SmartTodo compatible syntax comments." \
"For more info please look at https://github.com/Shopify/smart_todo/wiki/Syntax"
HELP = "For more info please look at https://github.com/Shopify/smart_todo/wiki/Syntax"
MSG = "Don't write regular TODO comments. Write SmartTodo compatible syntax comments. #{HELP}"

# @param processed_source [RuboCop::ProcessedSource]
# @return [void]
def investigate(processed_source)
processed_source.comments.each do |comment|
next unless /^#\sTODO/ =~ comment.text
next if smart_todo?(comment.text)

add_offense(comment)
metadata = metadata(comment.text)
if !smart_todo?(metadata)
add_offense(comment)
elsif (methods = invalid_event_methods(metadata.events)).any?
add_offense(comment, message: "Invalid event method(s): #{methods.join(", ")}. #{HELP}")
end
end
end

private

# @param comment [String]
# @return [true, false]
def smart_todo?(comment)
metadata = ::SmartTodo::Parser::MetadataParser.parse(comment.gsub(/^#/, ""))
# @return [SmartTodo::Parser::Visitor]
def metadata(comment)
::SmartTodo::Parser::MetadataParser.parse(comment.gsub(/^#/, ""))
end

# @param metadata [SmartTodo::Parser::Visitor]
# @return [true, false]
def smart_todo?(metadata)
metadata.events.any? &&
metadata.events.all? { |event| event.is_a?(::SmartTodo::Parser::MethodNode) } &&
metadata.assignees.any?
end

# @param metadata [Array<SmartTodo::Parser::MethodNode>]
# @return [Array<String>]
def invalid_event_methods(events)
events.map(&:method_name).reject { |method| ::SmartTodo::Events.respond_to?(method) }
end
end
end
end
Expand Down
13 changes: 11 additions & 2 deletions test/smart_todo/smart_todo_cop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def hello
RUBY
end

def test_add_offense_when_todo_event_is_not_a_valid_method
expect_offense(<<~RUBY)
# TODO(on: data('2019-08-04'), to: '[email protected]')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invalid event method(s): data. For more info please look at https://github.com/Shopify/smart_todo/wiki/Syntax
def hello
end
RUBY
end

def test_does_not_add_offense_when_todo_is_a_smart_todo
expect_no_offense(<<~RUBY)
# TODO(on: date('2019-08-04'), to: '[email protected]')
Expand All @@ -62,8 +71,8 @@ def hello
private

def expected_message
"Don't write regular TODO comments. Write SmartTodo compatible syntax comments." \
"For more info please look at https://github.com/shopify/smart_todo"
"Don't write regular TODO comments. Write SmartTodo compatible syntax comments. " \
"For more info please look at https://github.com/Shopify/smart_todo/wiki/Syntax"
end

def expect_offense(source)
Expand Down

0 comments on commit 28d7596

Please sign in to comment.