diff options
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/line_break_around_conditional_block.rb | 6 | ||||
-rw-r--r-- | rubocop/cop/ruby_interpolation_in_translation.rb | 29 | ||||
-rw-r--r-- | rubocop/rubocop.rb | 1 |
3 files changed, 36 insertions, 0 deletions
diff --git a/rubocop/cop/line_break_around_conditional_block.rb b/rubocop/cop/line_break_around_conditional_block.rb index 011f2bcf8bf..59fe6e5d98c 100644 --- a/rubocop/cop/line_break_around_conditional_block.rb +++ b/rubocop/cop/line_break_around_conditional_block.rb @@ -48,6 +48,8 @@ module RuboCop MSG = 'Add a line break around conditional blocks' def on_if(node) + # This cop causes errors in haml files, so let's skip those + return if in_haml?(node) return if node.single_line? return unless node.if? || node.unless? @@ -116,6 +118,10 @@ module RuboCop def end_line?(line) line =~ /^\s*(end|})/ end + + def in_haml?(node) + node.location.expression.source_buffer.name.end_with?('.haml.rb') + end end end end diff --git a/rubocop/cop/ruby_interpolation_in_translation.rb b/rubocop/cop/ruby_interpolation_in_translation.rb new file mode 100644 index 00000000000..b9411fcfd6c --- /dev/null +++ b/rubocop/cop/ruby_interpolation_in_translation.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + class RubyInterpolationInTranslation < RuboCop::Cop::Cop + MSG = "Don't use ruby interpolation \#{} inside translated strings, instead use \%{}" + + TRANSLATION_METHODS = ':_ :s_ :N_ :n_' + RUBY_INTERPOLATION_REGEX = /.*\#\{.*\}/ + + def_node_matcher :translation_method?, <<~PATTERN + (send nil? {#{TRANSLATION_METHODS}} $dstr ...) + PATTERN + + def_node_matcher :plural_translation_method?, <<~PATTERN + (send nil? :n_ str $dstr ...) + PATTERN + + def on_send(node) + interpolation = translation_method?(node) || plural_translation_method?(node) + return unless interpolation + + interpolation.descendants.each do |possible_violation| + add_offense(possible_violation, message: MSG) if possible_violation.type != :str + end + end + end + end +end diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 88c9bbf24f4..eaf421a7235 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -28,3 +28,4 @@ require_relative 'cop/rspec/env_assignment' require_relative 'cop/rspec/factories_in_migration_specs' require_relative 'cop/sidekiq_options_queue' require_relative 'cop/destroy_all' +require_relative 'cop/ruby_interpolation_in_translation' |