From a5876e0b8441bb841bbbdbefb70c24adfa0bbfa8 Mon Sep 17 00:00:00 2001 From: Lisa Ugray Date: Tue, 10 Jan 2023 10:48:29 -0500 Subject: [PATCH 1/3] Fix tests --- test/smart_todo/smart_todo_cop_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/smart_todo/smart_todo_cop_test.rb b/test/smart_todo/smart_todo_cop_test.rb index fd96a56..b425fa0 100644 --- a/test/smart_todo/smart_todo_cop_test.rb +++ b/test/smart_todo/smart_todo_cop_test.rb @@ -63,7 +63,7 @@ def hello 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" + "For more info please look at https://github.com/Shopify/smart_todo/wiki/Syntax" end def expect_offense(source) From 6609a8a6c468f45adbaa8755124979a50d3df3c1 Mon Sep 17 00:00:00 2001 From: Lisa Ugray Date: Tue, 10 Jan 2023 10:49:06 -0500 Subject: [PATCH 2/3] Add a space after period in message --- lib/smart_todo_cop.rb | 2 +- test/smart_todo/smart_todo_cop_test.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/smart_todo_cop.rb b/lib/smart_todo_cop.rb index a80b5f1..d2a786e 100644 --- a/lib/smart_todo_cop.rb +++ b/lib/smart_todo_cop.rb @@ -10,7 +10,7 @@ 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." \ + 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" # @param processed_source [RuboCop::ProcessedSource] diff --git a/test/smart_todo/smart_todo_cop_test.rb b/test/smart_todo/smart_todo_cop_test.rb index b425fa0..8e26860 100644 --- a/test/smart_todo/smart_todo_cop_test.rb +++ b/test/smart_todo/smart_todo_cop_test.rb @@ -62,7 +62,7 @@ def hello private def expected_message - "Don't write regular TODO comments. Write SmartTodo compatible syntax comments." \ + "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 From 4889aa3a7f8247124a05f253e6f290215f182009 Mon Sep 17 00:00:00 2001 From: Lisa Ugray Date: Tue, 10 Jan 2023 10:30:37 -0500 Subject: [PATCH 3/3] Detect bad events in the rubocop cop --- lib/smart_todo_cop.rb | 33 +++++++++++++++++++------- test/smart_todo/smart_todo_cop_test.rb | 9 +++++++ 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/lib/smart_todo_cop.rb b/lib/smart_todo_cop.rb index d2a786e..0ec05c5 100644 --- a/lib/smart_todo_cop.rb +++ b/lib/smart_todo_cop.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "smart_todo/parser/metadata_parser" +require "smart_todo" module RuboCop module Cop @@ -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] + # @return [Array] + def invalid_event_methods(events) + events.map(&:method_name).reject { |method| ::SmartTodo::Events.respond_to?(method) } + end end end end diff --git a/test/smart_todo/smart_todo_cop_test.rb b/test/smart_todo/smart_todo_cop_test.rb index 8e26860..01a5893 100644 --- a/test/smart_todo/smart_todo_cop_test.rb +++ b/test/smart_todo/smart_todo_cop_test.rb @@ -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: 'john@example.com') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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: 'john@example.com')