From 0ce6785851510ccb49f0d1edc0220aca46f815f5 Mon Sep 17 00:00:00 2001 From: Jacopo Date: Tue, 3 Oct 2017 10:35:01 +0200 Subject: Replaces `tag: true` into `:tag` in the specs Replaces all the explicit include metadata syntax in the specs (tag: true) into the implicit one (:tag). Added a cop to prevent future errors and handle autocorrection. --- rubocop/cop/rspec/verbose_include_metadata.rb | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 rubocop/cop/rspec/verbose_include_metadata.rb (limited to 'rubocop/cop') diff --git a/rubocop/cop/rspec/verbose_include_metadata.rb b/rubocop/cop/rspec/verbose_include_metadata.rb new file mode 100644 index 00000000000..58390622d60 --- /dev/null +++ b/rubocop/cop/rspec/verbose_include_metadata.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +require 'rubocop-rspec' + +module RuboCop + module Cop + module RSpec + # Checks for verbose include metadata used in the specs. + # + # @example + # # bad + # describe MyClass, js: true do + # end + # + # # good + # describe MyClass, :js do + # end + class VerboseIncludeMetadata < Cop + MSG = 'Use `%s` instead of `%s`.' + + SELECTORS = %i[describe context feature example_group it specify example scenario its].freeze + + def_node_matcher :include_metadata, <<-PATTERN + (send {(const nil :RSpec) nil} {#{SELECTORS.map(&:inspect).join(' ')}} + !const + ... + (hash $...)) + PATTERN + + def_node_matcher :invalid_metadata?, <<-PATTERN + (pair + (sym $...) + (true)) + PATTERN + + def on_send(node) + invalid_metadata_matches(node) do |match| + add_offense(node, :expression, format(MSG, good(match), bad(match))) + end + end + + def autocorrect(node) + lambda do |corrector| + invalid_metadata_matches(node) do |match| + corrector.replace(match.loc.expression, good(match)) + end + end + end + + private + + def invalid_metadata_matches(node) + include_metadata(node) do |matches| + matches.select(&method(:invalid_metadata?)).each do |match| + yield match + end + end + end + + def bad(match) + "#{metadata_key(match)}: true" + end + + def good(match) + ":#{metadata_key(match)}" + end + + def metadata_key(match) + match.children[0].source + end + end + end + end +end -- cgit v1.2.3 From a560291f143399b2c8d340fb0a02615dc72552c3 Mon Sep 17 00:00:00 2001 From: Sean McGivern Date: Tue, 10 Oct 2017 10:36:10 +0100 Subject: Also warn on timestamp in datetime migration cop The types `timestamp` and `datetime` are aliases: https://github.com/rails/rails/blob/v4.2.10/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L362-L364 --- rubocop/cop/migration/datetime.rb | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'rubocop/cop') diff --git a/rubocop/cop/migration/datetime.rb b/rubocop/cop/migration/datetime.rb index 651935dd53e..9cba3c35b26 100644 --- a/rubocop/cop/migration/datetime.rb +++ b/rubocop/cop/migration/datetime.rb @@ -7,14 +7,18 @@ module RuboCop class Datetime < RuboCop::Cop::Cop include MigrationHelpers - MSG = 'Do not use the `datetime` data type, use `datetime_with_timezone` instead'.freeze + MSG = 'Do not use the `%s` data type, use `datetime_with_timezone` instead'.freeze # Check methods in table creation. def on_def(node) return unless in_migration?(node) node.each_descendant(:send) do |send_node| - add_offense(send_node, :selector) if method_name(send_node) == :datetime + method_name = node.children[1] + + if method_name == :datetime || method_name == :timestamp + add_offense(send_node, :selector, format(MSG, method_name)) + end end end @@ -23,12 +27,14 @@ module RuboCop return unless in_migration?(node) node.each_descendant do |descendant| - add_offense(node, :expression) if descendant.type == :sym && descendant.children.last == :datetime - end - end + next unless descendant.type == :sym - def method_name(node) - node.children[1] + last_argument = descendant.children.last + + if last_argument == :datetime || last_argument == :timestamp + add_offense(node, :expression, format(MSG, last_argument)) + end + end end end end -- cgit v1.2.3 From 8a31b0743753f080e0ba07c8d6d0da863a5c4726 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Coutable?= Date: Wed, 11 Oct 2017 13:25:40 +0200 Subject: Add a new RSpec::EnvAssignment cop to prevent assigning to ENV in specs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rémy Coutable --- rubocop/cop/rspec/env_assignment.rb | 58 +++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 rubocop/cop/rspec/env_assignment.rb (limited to 'rubocop/cop') diff --git a/rubocop/cop/rspec/env_assignment.rb b/rubocop/cop/rspec/env_assignment.rb new file mode 100644 index 00000000000..257454af0e1 --- /dev/null +++ b/rubocop/cop/rspec/env_assignment.rb @@ -0,0 +1,58 @@ +require 'rubocop-rspec' +require_relative '../../spec_helpers' + +module RuboCop + module Cop + module RSpec + # This cop checks for ENV assignment in specs + # + # @example + # + # # bad + # before do + # ENV['FOO'] = 'bar' + # end + # + # # good + # before do + # stub_env('FOO', 'bar') + # end + class EnvAssignment < Cop + include SpecHelpers + + MESSAGE = "Don't assign to ENV, use `stub_env` instead.".freeze + + def_node_search :env_assignment?, <<~PATTERN + (send (const nil? :ENV) :[]= ...) + PATTERN + + # Following is what node.children looks like on a match + # [s(:const, nil, :ENV), :[]=, s(:str, "key"), s(:str, "value")] + def on_send(node) + return unless in_spec?(node) + return unless env_assignment?(node) + + add_offense(node, :expression, MESSAGE) + end + + def autocorrect(node) + lambda do |corrector| + corrector.replace(node.loc.expression, stub_env(env_key(node), env_value(node))) + end + end + + def env_key(node) + node.children[2].source + end + + def env_value(node) + node.children[3].source + end + + def stub_env(key, value) + "stub_env(#{key}, #{value})" + end + end + end + end +end -- cgit v1.2.3