2020-06-25 20:10:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
require "rubocops/extend/formula"
|
|
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
|
module Cop
|
|
|
|
|
module FormulaAudit
|
2020-08-28 20:47:52 -04:00
|
|
|
# This cop audits deprecate! date
|
|
|
|
|
class DeprecateDate < FormulaCop
|
2020-06-25 20:10:37 -04:00
|
|
|
def audit_formula(_node, _class_node, _parent_class_node, body_node)
|
|
|
|
|
deprecate_node = find_node_method_by_name(body_node, :deprecate!)
|
|
|
|
|
|
2020-08-28 20:47:52 -04:00
|
|
|
return if deprecate_node.nil?
|
2020-06-25 20:10:37 -04:00
|
|
|
|
2020-08-28 20:47:52 -04:00
|
|
|
deprecate_date(deprecate_node) do |date_node|
|
2020-06-25 20:10:37 -04:00
|
|
|
Date.iso8601(string_content(date_node))
|
|
|
|
|
rescue ArgumentError
|
|
|
|
|
fixed_date_string = Date.parse(string_content(date_node)).iso8601
|
|
|
|
|
offending_node(date_node)
|
|
|
|
|
problem "Use `#{fixed_date_string}` to comply with ISO 8601"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def autocorrect(node)
|
|
|
|
|
lambda do |corrector|
|
|
|
|
|
fixed_fixed_date_string = Date.parse(string_content(node)).iso8601
|
|
|
|
|
corrector.replace(node.source_range, "\"#{fixed_fixed_date_string}\"")
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-08-28 20:47:52 -04:00
|
|
|
|
|
|
|
|
def_node_search :deprecate_date, <<~EOS
|
|
|
|
|
(pair (sym :date) $str)
|
|
|
|
|
EOS
|
2020-06-25 20:10:37 -04:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|