Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested cop: time/date for const/prop defaults #103

Open
rvanmelle opened this issue Apr 15, 2022 · 1 comment
Open

Suggested cop: time/date for const/prop defaults #103

rvanmelle opened this issue Apr 15, 2022 · 1 comment

Comments

@rvanmelle
Copy link

We were recently affected by a bug where had stale times/dates for struct defaults --- which is not surprising, just easy to overlook. We ended up writing the cop below which could be generally useful.

I'd be happy to work at creating a PR, but I thought it would be great to get general feedback first. My concerns:

  • this cop currently isn't smart enough to know whether the prop or const declaration is within a T::Struct definition, so it is likely prone to false positives; it works in our code base fine but I imagine it might need to be a bit more contextual for general usage
  • this is an easy cop for autocorrect, but I'm still learning rubocop 😅
  class StructDefaultTime < Cop
        MSG = "Struct property defaults are evaluated only once. Use a factory for time and date defaults"
        DATE_OR_TIME_METHODS = %i[since from_now after ago until before yesterday tomorrow now today].to_set.freeze
        STRUCT_DECLARATIONS = %i[const prop].to_set.freeze

        def on_send(node)
          struct_default(node) do |_, default_node|
            nested_date_or_time(default_node) do
              add_offense(node)
            end
          end
        end

        private

        def_node_matcher :struct_default, <<~PATTERN
          (send nil? $STRUCT_DECLARATIONS
            (sym _)
            (const nil? _)
            (hash
              (pair
                (sym :default) $(...))))
        PATTERN

        def_node_matcher :date_or_time, <<~PATTERN
          (send _ $DATE_OR_TIME_METHODS)
        PATTERN

        def nested_date_or_time(node, &callback)
          return if node.nil? || node.block_type?

          node.each_child_node do |child|
            nested_date_or_time(child, &callback)
          end

          date_or_time(node, &callback)
        end
      end

The rough test suite looks something like:

 class StructDefaultTimeTest < ActiveSupport::TestCase
        include RubocopTestHelper

        def setup
          @cop = RuboCop::Cop::Bourgeois::StructDefaultTime.new
        end

        test "ok for non date/time props and constants with or without defaults" do
          assert_no_offenses("const :foo, Integer, default: 3")
          assert_no_offenses("prop :bar, String, default: 'foo'")
          assert_no_offenses("const :baz, Time")
        end

        test "ok for factory date and time props and constants" do
          assert_no_offenses("const :baz, Time, factory: -> { Time.zone.now }")
        end

        test "offense when using current or relative date/time methods" do
          assert_offense("const :foo, Date, default: Time.zone.today")
          assert_offense("const :foo, Time, default: Time.zone.now")
          assert_offense("const :foo, Date, default: 3.days.from_now")
          assert_offense("const :foo, Date, default: Time.zone.now.yesterday")
          assert_offense("const :foo, Time, default: 10.minutes.ago")
          assert_offense("const :foo, Date, default: 5.days.before(Date.today)")

          assert_offense("prop :foo, Date, default: Time.zone.today")
          assert_offense("prop :foo, Time, default: Time.zone.now")
          assert_offense("prop :foo, Date, default: 3.days.from_now")
          assert_offense("prop :foo, Date, default: Time.zone.now.yesterday")
          assert_offense("prop :foo, Time, default: 10.minutes.ago")
          assert_offense("prop :foo, Date, default: 5.days.before(Date.today)")
        end
      end
@KaanOzkan
Copy link
Contributor

Thank you 🙂 . This is my first time hearing about factory but the cop suggestion seems reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants