rubocop-i18n 1.3.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05f242d66df11b40d7d4e4e33bb98ae8be347062
4
- data.tar.gz: b9e07ef3ce86a5c34f409620a2ce3eae4917341b
3
+ metadata.gz: c8ec74372e48d2888a1e145600899f36250f1d41
4
+ data.tar.gz: c45019b849416c7d3e67d5a0f0677f4cff193fef
5
5
  SHA512:
6
- metadata.gz: c4afff3c8a1d356f37de79fd59c34a2ed4ad8e977692d888c265791ca180779c8145012b70c8b542fc251c68f1325a7370c729d9180b31c233f40fea623fcd82
7
- data.tar.gz: da4a2892bb9a3450475afc776d435eae64f0be2766b3b5f061d824adc39dd9512e458555b018992f6222c4452b9445b82c52969985bb6447999e26ed8f1c555f
6
+ metadata.gz: f229adc2ced75db0cc05dae0bb4d0cb3d1d6bf28f6d832ee9897d3a6d6a383a4be15bffb40b4805fb8ef28334197d0017f0341fa58e986d2ee2ae1906588e303
7
+ data.tar.gz: 5720548d777aaa871358d9f8fcc3107e54574e7fbcbf5032f29d7442a0055624f7bc3a24c2d6ca0a6f4c9575ec5480f07700ae2bfd1cc6924852cbb705206f7a
@@ -1,3 +1,8 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
  require:
3
3
  - rubocop/cop/internal_affairs
4
+
5
+ Metrics/BlockLength:
6
+ Exclude:
7
+ # Exclude the spec directory because the rspec DSL results in long blocks
8
+ - 'spec/**/*'
@@ -1,6 +1,14 @@
1
1
  # Change Log
2
2
 
3
- ## master (unreleased)
3
+ ### master (Unreleased)
4
+
5
+ * None
6
+
7
+ ### 2.0.0
8
+
9
+ * Add rails-i18n support and documentation in README. Thanks @kbacha!
10
+
11
+ ### 1.3.1, 1.3.0, 1.2.0
4
12
 
5
13
  * Updated DecorateString to look for sentences using a regular expression that should be decorated. This limits the number of strings that it finds to things that look like a sentence.
6
14
  * Code restructure (no API changes)
data/README.md CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  A set of cops for detecting strings that need i18n decoration in your project.
4
4
 
5
+ Supports the following framework styles:
6
+
7
+ - gettext
8
+ - rails-i18n
9
+
5
10
  ## Installation
6
11
 
7
12
  Add this line to your application's Gemfile:
@@ -25,14 +30,28 @@ In your `rubocop.yml`:
25
30
  require:
26
31
  - rubocop-i18n
27
32
  ...
28
- GetText/DecorateString:
33
+ # You *must* choose GetText or Rails-i18n style checking
34
+ # If you want GetText-style checking
35
+ GetText:
29
36
  Enabled: true
30
- GetText/DecorateFunctionMessage:
31
- Enabled: true
32
- GetText/DecorateStringFormattingUsingInterpolation
33
- Enabled: true
34
- GetText/DecorateStringFormattingUsingPercent
37
+ RailsI18n:
38
+ Enabled: false
39
+ # If you want rails-i18n-style checking
40
+ RailsI18n:
35
41
  Enabled: true
42
+ GetText:
43
+ Enabled: false
44
+ # If you want custom control of all the cops
45
+ GetText/DecorateString:
46
+ Enabled: false
47
+ GetText/DecorateFunctionMessage:
48
+ Enabled: false
49
+ GetText/DecorateStringFormattingUsingInterpolation:
50
+ Enabled: false
51
+ GetText/DecorateStringFormattingUsingPercent:
52
+ Enabled: false
53
+ RailsI18n/DecorateString:
54
+ Enabled: false
36
55
  ```
37
56
 
38
57
  ## Cops
@@ -233,11 +252,36 @@ raise(_("Warning is %s") % ['bad'])
233
252
  raise(_("Warning is %{value}") % { value: 'bad' })
234
253
  ```
235
254
 
255
+ ### RailsI18n/DecorateString
256
+
257
+ This cop looks for decorated rails-i18n methods.
258
+
259
+ ##### Error message thrown
260
+
261
+ ```
262
+ decorator is missing around sentence
263
+ ```
264
+
265
+ ##### Bad
266
+
267
+ ``` ruby
268
+ raise("Warning is %s" % ['bad'])
269
+ ```
270
+
271
+ ##### Good
272
+
273
+ ``` ruby
274
+ raise(t("Warning is %{value}") % { value: 'good' })
275
+ raise(translate("Warning is %{value}") % { value: 'good' })
276
+ raise(I18n.t("Warning is %{value}") % { value: 'good' })
277
+ ```
278
+
236
279
  ## How to ignore rules in code
237
280
 
238
281
  It may be necessary to ignore a cop for a particular piece of code. We follow standard rubocop idioms.
239
282
  ``` ruby
240
283
  raise("We don't want this translated.") # rubocop:disable GetText/DecorateString
284
+ raise("We don't want this translated.") # rubocop:disable RailsI18n/DecorateString
241
285
  raise("We don't want this translated") # rubocop:disable GetText/DecorateFunctionMessage
242
286
  raise(_("We don't want this translated #{crazy}") # rubocop:disable GetText/DecorateStringFormattingUsingInterpolation)
243
287
  raise(_("We don't want this translated %s") % ['crazy'] # rubocop:disable GetText/DecorateStringFormattingUsingPercent)
@@ -4,3 +4,6 @@ require 'rubocop/cop/i18n/gettext/decorate_string'
4
4
  require 'rubocop/cop/i18n/gettext/decorate_function_message'
5
5
  require 'rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation'
6
6
  require 'rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent'
7
+
8
+ require 'rubocop/cop/i18n/rails_i18n'
9
+ require 'rubocop/cop/i18n/rails_i18n/decorate_string'
@@ -20,7 +20,7 @@ module RuboCop
20
20
  #
21
21
  # _("Result is good.")
22
22
  class DecorateString < Cop
23
- STRING_REGEXP = /^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/
23
+ STRING_REGEXP = /^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze
24
24
 
25
25
  def on_dstr(node)
26
26
  check_for_parent_decorator(node) if dstr_contains_sentence?(node)
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module I18n
6
+ # The Rails I18n module contains cops used to lint and enforce the use of strings
7
+ # in rails applications that want to use the I18n gem.
8
+ module RailsI18n
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module I18n
6
+ module RailsI18n
7
+ # This cop is looks for strings that appear to be sentences but are not decorated.
8
+ # Sentences are determined by the STRING_REGEXP. (Upper case character, at least one space,
9
+ # and sentence punctuation at the end)
10
+ #
11
+ # @example
12
+ #
13
+ # # bad
14
+ #
15
+ # "Result is bad."
16
+ #
17
+ # @example
18
+ #
19
+ # # good
20
+ #
21
+ # t("result_is_good")
22
+ # I18n.t("result_is_good")
23
+ #
24
+ class DecorateString < Cop
25
+ STRING_REGEXP = /^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze
26
+ SUPPORTED_DECORATORS = %w[
27
+ t
28
+ t!
29
+ translate
30
+ translate!
31
+ ].freeze
32
+
33
+ def on_dstr(node)
34
+ check_for_parent_decorator(node) if dstr_contains_sentence?(node)
35
+ end
36
+
37
+ def on_str(node)
38
+ return unless sentence?(node)
39
+
40
+ parent = node.parent
41
+ if parent.respond_to?(:type)
42
+ return if parent.regexp_type? || parent.dstr_type?
43
+ end
44
+
45
+ check_for_parent_decorator(node)
46
+ end
47
+
48
+ private
49
+
50
+ def sentence?(node)
51
+ child = node.children[0]
52
+ if child.is_a?(String)
53
+ if child.valid_encoding?
54
+ child.encode(Encoding::UTF_8).chomp =~ STRING_REGEXP
55
+ else
56
+ false
57
+ end
58
+ elsif child.respond_to?(:type) && child.str_type?
59
+ sentence?(child)
60
+ else
61
+ false
62
+ end
63
+ end
64
+
65
+ def dstr_contains_sentence?(node)
66
+ node.children.any? { |child| sentence?(child) }
67
+ end
68
+
69
+ def check_for_parent_decorator(node)
70
+ return if parent_is_translator?(node.parent)
71
+ return if parent_is_indexer?(node.parent)
72
+ return if ignoring_raised_parent?(node.parent)
73
+
74
+ add_offense(node, message: 'decorator is missing around sentence')
75
+ end
76
+
77
+ def ignoring_raised_parent?(parent)
78
+ return false unless cop_config['IgnoreExceptions']
79
+
80
+ return true if parent.respond_to?(:method_name) && %i[raise fail].include?(parent.method_name)
81
+
82
+ # Commonly exceptions are initialized manually.
83
+ return ignoring_raised_parent?(parent.parent) if parent.respond_to?(:method_name) && parent.method_name == :new
84
+
85
+ false
86
+ end
87
+
88
+ def parent_is_indexer?(parent)
89
+ parent.respond_to?(:method_name) && parent.method_name == :[]
90
+ end
91
+
92
+ def parent_is_translator?(parent)
93
+ if parent.respond_to?(:type) && parent.send_type?
94
+ method_name = parent.loc.selector.source
95
+ if supported_decorator?(method_name)
96
+ # Implicit receiver is assumed correct.
97
+ return true if parent.receiver.nil?
98
+ return true if parent.receiver.children == [nil, :I18n]
99
+ end
100
+ end
101
+ false
102
+ end
103
+
104
+ def supported_decorator?(decorator_name)
105
+ SUPPORTED_DECORATORS.include?(decorator_name)
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
@@ -4,12 +4,12 @@ rubocop_version = '~> 0.51'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'rubocop-i18n'
7
- spec.version = '1.3.1'
7
+ spec.version = '2.0.0'
8
8
  spec.authors = ['Puppet', 'Brandon High', 'TP Honey', 'Helen Campbell']
9
9
  spec.email = ['team-modules@puppet.com', 'brandon.high@puppet.com', 'tp@puppet.com', 'helen@puppet.com']
10
10
 
11
11
  spec.summary = 'RuboCop rules for i18n'
12
- spec.description = 'RuboCop rules for detecting and autocorrecting undecorated strings for i18n'
12
+ spec.description = 'RuboCop rules for detecting and autocorrecting undecorated strings for i18n (gettext and rails-i18n)'
13
13
  spec.homepage = 'https://github.com/puppetlabs/rubocop-i18n'
14
14
  spec.license = 'Apache-2'
15
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Puppet
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2018-09-21 00:00:00.000000000 Z
14
+ date: 2018-11-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: bundler
@@ -112,7 +112,7 @@ dependencies:
112
112
  - !ruby/object:Gem::Version
113
113
  version: '0.51'
114
114
  description: RuboCop rules for detecting and autocorrecting undecorated strings for
115
- i18n
115
+ i18n (gettext and rails-i18n)
116
116
  email:
117
117
  - team-modules@puppet.com
118
118
  - brandon.high@puppet.com
@@ -141,6 +141,8 @@ files:
141
141
  - lib/rubocop/cop/i18n/gettext/decorate_string.rb
142
142
  - lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation.rb
143
143
  - lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent.rb
144
+ - lib/rubocop/cop/i18n/rails_i18n.rb
145
+ - lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb
144
146
  - lib/rubocop/rspec/cop_helper.rb
145
147
  - rubocop-i18n.gemspec
146
148
  homepage: https://github.com/puppetlabs/rubocop-i18n
@@ -163,7 +165,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
163
165
  version: '0'
164
166
  requirements: []
165
167
  rubyforge_project:
166
- rubygems_version: 2.6.14.1
168
+ rubygems_version: 2.6.6
167
169
  signing_key:
168
170
  specification_version: 4
169
171
  summary: RuboCop rules for i18n