rubocop-rails 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +20 -0
  3. data/README.md +73 -0
  4. data/bin/setup +7 -0
  5. data/config/default.yml +466 -0
  6. data/lib/rubocop-rails.rb +12 -0
  7. data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
  8. data/lib/rubocop/cop/rails/action_filter.rb +117 -0
  9. data/lib/rubocop/cop/rails/active_record_aliases.rb +48 -0
  10. data/lib/rubocop/cop/rails/active_record_override.rb +82 -0
  11. data/lib/rubocop/cop/rails/active_support_aliases.rb +69 -0
  12. data/lib/rubocop/cop/rails/application_job.rb +40 -0
  13. data/lib/rubocop/cop/rails/application_record.rb +40 -0
  14. data/lib/rubocop/cop/rails/assert_not.rb +44 -0
  15. data/lib/rubocop/cop/rails/belongs_to.rb +102 -0
  16. data/lib/rubocop/cop/rails/blank.rb +164 -0
  17. data/lib/rubocop/cop/rails/bulk_change_table.rb +289 -0
  18. data/lib/rubocop/cop/rails/create_table_with_timestamps.rb +91 -0
  19. data/lib/rubocop/cop/rails/date.rb +161 -0
  20. data/lib/rubocop/cop/rails/delegate.rb +132 -0
  21. data/lib/rubocop/cop/rails/delegate_allow_blank.rb +37 -0
  22. data/lib/rubocop/cop/rails/dynamic_find_by.rb +91 -0
  23. data/lib/rubocop/cop/rails/enum_uniqueness.rb +45 -0
  24. data/lib/rubocop/cop/rails/environment_comparison.rb +68 -0
  25. data/lib/rubocop/cop/rails/exit.rb +67 -0
  26. data/lib/rubocop/cop/rails/file_path.rb +108 -0
  27. data/lib/rubocop/cop/rails/find_by.rb +55 -0
  28. data/lib/rubocop/cop/rails/find_each.rb +51 -0
  29. data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +25 -0
  30. data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +106 -0
  31. data/lib/rubocop/cop/rails/helper_instance_variable.rb +39 -0
  32. data/lib/rubocop/cop/rails/http_positional_arguments.rb +117 -0
  33. data/lib/rubocop/cop/rails/http_status.rb +160 -0
  34. data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +94 -0
  35. data/lib/rubocop/cop/rails/inverse_of.rb +246 -0
  36. data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +175 -0
  37. data/lib/rubocop/cop/rails/link_to_blank.rb +98 -0
  38. data/lib/rubocop/cop/rails/not_null_column.rb +67 -0
  39. data/lib/rubocop/cop/rails/output.rb +49 -0
  40. data/lib/rubocop/cop/rails/output_safety.rb +99 -0
  41. data/lib/rubocop/cop/rails/pluralization_grammar.rb +107 -0
  42. data/lib/rubocop/cop/rails/presence.rb +124 -0
  43. data/lib/rubocop/cop/rails/present.rb +153 -0
  44. data/lib/rubocop/cop/rails/read_write_attribute.rb +74 -0
  45. data/lib/rubocop/cop/rails/redundant_allow_nil.rb +111 -0
  46. data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +136 -0
  47. data/lib/rubocop/cop/rails/reflection_class_name.rb +37 -0
  48. data/lib/rubocop/cop/rails/refute_methods.rb +76 -0
  49. data/lib/rubocop/cop/rails/relative_date_constant.rb +93 -0
  50. data/lib/rubocop/cop/rails/request_referer.rb +56 -0
  51. data/lib/rubocop/cop/rails/reversible_migration.rb +286 -0
  52. data/lib/rubocop/cop/rails/safe_navigation.rb +87 -0
  53. data/lib/rubocop/cop/rails/save_bang.rb +316 -0
  54. data/lib/rubocop/cop/rails/scope_args.rb +29 -0
  55. data/lib/rubocop/cop/rails/skips_model_validations.rb +87 -0
  56. data/lib/rubocop/cop/rails/time_zone.rb +238 -0
  57. data/lib/rubocop/cop/rails/uniq_before_pluck.rb +105 -0
  58. data/lib/rubocop/cop/rails/unknown_env.rb +63 -0
  59. data/lib/rubocop/cop/rails/validation.rb +109 -0
  60. data/lib/rubocop/cop/rails_cops.rb +64 -0
  61. data/lib/rubocop/rails.rb +12 -0
  62. data/lib/rubocop/rails/inject.rb +18 -0
  63. data/lib/rubocop/rails/version.rb +10 -0
  64. metadata +143 -0
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks that environments called with `Rails.env` predicates
7
+ # exist.
8
+ #
9
+ # @example
10
+ # # bad
11
+ # Rails.env.proudction?
12
+ #
13
+ # # good
14
+ # Rails.env.production?
15
+ class UnknownEnv < Cop
16
+ include NameSimilarity
17
+
18
+ MSG = 'Unknown environment `%<name>s`.'
19
+ MSG_SIMILAR = 'Unknown environment `%<name>s`. ' \
20
+ 'Did you mean `%<similar>s`?'
21
+
22
+ def_node_matcher :unknown_environment?, <<-PATTERN
23
+ (send
24
+ (send
25
+ {(const nil? :Rails) (const (cbase) :Rails)}
26
+ :env)
27
+ $#unknown_env_name?)
28
+ PATTERN
29
+
30
+ def on_send(node)
31
+ unknown_environment?(node) do |name|
32
+ add_offense(node, location: :selector, message: message(name))
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def collect_variable_like_names(_scope)
39
+ environments.map { |env| env + '?' }
40
+ end
41
+
42
+ def message(name)
43
+ similar = find_similar_name(name.to_s, [])
44
+ if similar
45
+ format(MSG_SIMILAR, name: name, similar: similar)
46
+ else
47
+ format(MSG, name: name)
48
+ end
49
+ end
50
+
51
+ def unknown_env_name?(name)
52
+ name = name.to_s
53
+ name.end_with?('?') &&
54
+ !environments.include?(name[0..-2])
55
+ end
56
+
57
+ def environments
58
+ cop_config['Environments']
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop checks for the use of old-style attribute validation macros.
7
+ #
8
+ # @example
9
+ # # bad
10
+ # validates_acceptance_of :foo
11
+ # validates_confirmation_of :foo
12
+ # validates_exclusion_of :foo
13
+ # validates_format_of :foo
14
+ # validates_inclusion_of :foo
15
+ # validates_length_of :foo
16
+ # validates_numericality_of :foo
17
+ # validates_presence_of :foo
18
+ # validates_absence_of :foo
19
+ # validates_size_of :foo
20
+ # validates_uniqueness_of :foo
21
+ #
22
+ # # good
23
+ # validates :foo, acceptance: true
24
+ # validates :foo, confirmation: true
25
+ # validates :foo, exclusion: true
26
+ # validates :foo, format: true
27
+ # validates :foo, inclusion: true
28
+ # validates :foo, length: true
29
+ # validates :foo, numericality: true
30
+ # validates :foo, presence: true
31
+ # validates :foo, absence: true
32
+ # validates :foo, size: true
33
+ # validates :foo, uniqueness: true
34
+ #
35
+ class Validation < Cop
36
+ MSG = 'Prefer the new style validations `%<prefer>s` over ' \
37
+ '`%<current>s`.'
38
+
39
+ TYPES = %w[
40
+ acceptance
41
+ confirmation
42
+ exclusion
43
+ format
44
+ inclusion
45
+ length
46
+ numericality
47
+ presence
48
+ absence
49
+ size
50
+ uniqueness
51
+ ].freeze
52
+
53
+ DENYLIST = TYPES.map { |p| "validates_#{p}_of".to_sym }.freeze
54
+ ALLOWLIST = TYPES.map { |p| "validates :column, #{p}: value" }.freeze
55
+
56
+ def on_send(node)
57
+ return unless !node.receiver && DENYLIST.include?(node.method_name)
58
+
59
+ add_offense(node, location: :selector)
60
+ end
61
+
62
+ def autocorrect(node)
63
+ last_argument = node.arguments.last
64
+ return if !last_argument.literal? && !last_argument.splat_type?
65
+
66
+ lambda do |corrector|
67
+ corrector.replace(node.loc.selector, 'validates')
68
+ correct_validate_type(corrector, node)
69
+ end
70
+ end
71
+
72
+ private
73
+
74
+ def message(node)
75
+ format(MSG, prefer: preferred_method(node.method_name),
76
+ current: node.method_name)
77
+ end
78
+
79
+ def preferred_method(method)
80
+ ALLOWLIST[DENYLIST.index(method.to_sym)]
81
+ end
82
+
83
+ def correct_validate_type(corrector, node)
84
+ last_argument = node.arguments.last
85
+ validate_type = node.method_name.to_s.split('_')[1]
86
+
87
+ if last_argument.hash_type?
88
+ corrector.replace(
89
+ last_argument.loc.expression,
90
+ "#{validate_type}: #{braced_options(last_argument)}"
91
+ )
92
+ else
93
+ range = last_argument.source_range
94
+
95
+ corrector.insert_after(range, ", #{validate_type}: true")
96
+ end
97
+ end
98
+
99
+ def braced_options(options)
100
+ if options.braces?
101
+ options.source
102
+ else
103
+ "{ #{options.source} }"
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # RuboCop included the Rails cops directly before version 1.0.0.
5
+ # We can remove them to avoid warnings about redefining constants.
6
+ module Cop
7
+ remove_const('Rails') if const_defined?('Rails')
8
+ end
9
+ end
10
+
11
+ require_relative 'mixin/target_rails_version'
12
+
13
+ require_relative 'rails/action_filter'
14
+ require_relative 'rails/active_record_aliases'
15
+ require_relative 'rails/active_record_override'
16
+ require_relative 'rails/active_support_aliases'
17
+ require_relative 'rails/application_job'
18
+ require_relative 'rails/application_record'
19
+ require_relative 'rails/assert_not'
20
+ require_relative 'rails/belongs_to'
21
+ require_relative 'rails/blank'
22
+ require_relative 'rails/bulk_change_table'
23
+ require_relative 'rails/create_table_with_timestamps'
24
+ require_relative 'rails/date'
25
+ require_relative 'rails/delegate'
26
+ require_relative 'rails/delegate_allow_blank'
27
+ require_relative 'rails/dynamic_find_by'
28
+ require_relative 'rails/enum_uniqueness'
29
+ require_relative 'rails/environment_comparison'
30
+ require_relative 'rails/exit'
31
+ require_relative 'rails/file_path'
32
+ require_relative 'rails/find_by'
33
+ require_relative 'rails/find_each'
34
+ require_relative 'rails/has_and_belongs_to_many'
35
+ require_relative 'rails/has_many_or_has_one_dependent'
36
+ require_relative 'rails/helper_instance_variable'
37
+ require_relative 'rails/http_positional_arguments'
38
+ require_relative 'rails/http_status'
39
+ require_relative 'rails/ignored_skip_action_filter_option'
40
+ require_relative 'rails/inverse_of'
41
+ require_relative 'rails/lexically_scoped_action_filter'
42
+ require_relative 'rails/link_to_blank'
43
+ require_relative 'rails/not_null_column'
44
+ require_relative 'rails/output'
45
+ require_relative 'rails/output_safety'
46
+ require_relative 'rails/pluralization_grammar'
47
+ require_relative 'rails/presence'
48
+ require_relative 'rails/present'
49
+ require_relative 'rails/read_write_attribute'
50
+ require_relative 'rails/redundant_allow_nil'
51
+ require_relative 'rails/redundant_receiver_in_with_options'
52
+ require_relative 'rails/reflection_class_name'
53
+ require_relative 'rails/refute_methods'
54
+ require_relative 'rails/relative_date_constant'
55
+ require_relative 'rails/request_referer'
56
+ require_relative 'rails/reversible_migration'
57
+ require_relative 'rails/safe_navigation'
58
+ require_relative 'rails/save_bang'
59
+ require_relative 'rails/scope_args'
60
+ require_relative 'rails/skips_model_validations'
61
+ require_relative 'rails/time_zone'
62
+ require_relative 'rails/uniq_before_pluck'
63
+ require_relative 'rails/unknown_env'
64
+ require_relative 'rails/validation'
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ # RuboCop Rails project namespace
5
+ module Rails
6
+ PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
7
+ CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
8
+ CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
9
+
10
+ private_constant(:CONFIG_DEFAULT, :PROJECT_ROOT)
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Rails
5
+ # Because RuboCop doesn't yet support plugins, we have to monkey patch in a
6
+ # bit of our configuration.
7
+ module Inject
8
+ def self.defaults!
9
+ path = CONFIG_DEFAULT.to_s
10
+ hash = ConfigLoader.send(:load_yaml_configuration, path)
11
+ config = Config.new(hash, path)
12
+ puts "configuration from #{path}" if ConfigLoader.debug?
13
+ config = ConfigLoader.merge_with_default(config, path, unset_nil: false)
14
+ ConfigLoader.instance_variable_set(:@default_configuration, config)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Rails
5
+ # This module holds the RuboCop Rails version information.
6
+ module Version
7
+ STRING = '2.0.0'
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bozhidar Batsov
8
+ - Jonas Arvidsson
9
+ - Yuji Nakayama
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2019-05-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '2.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: rubocop
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.70.0
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 0.70.0
43
+ description: |2
44
+ Automatic Rails code style checking tool.
45
+ A RuboCop extension focused on enforcing Rails best practices and coding conventions.
46
+ email: rubocop@googlegroups.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files:
50
+ - LICENSE.txt
51
+ - README.md
52
+ files:
53
+ - LICENSE.txt
54
+ - README.md
55
+ - bin/setup
56
+ - config/default.yml
57
+ - lib/rubocop-rails.rb
58
+ - lib/rubocop/cop/mixin/target_rails_version.rb
59
+ - lib/rubocop/cop/rails/action_filter.rb
60
+ - lib/rubocop/cop/rails/active_record_aliases.rb
61
+ - lib/rubocop/cop/rails/active_record_override.rb
62
+ - lib/rubocop/cop/rails/active_support_aliases.rb
63
+ - lib/rubocop/cop/rails/application_job.rb
64
+ - lib/rubocop/cop/rails/application_record.rb
65
+ - lib/rubocop/cop/rails/assert_not.rb
66
+ - lib/rubocop/cop/rails/belongs_to.rb
67
+ - lib/rubocop/cop/rails/blank.rb
68
+ - lib/rubocop/cop/rails/bulk_change_table.rb
69
+ - lib/rubocop/cop/rails/create_table_with_timestamps.rb
70
+ - lib/rubocop/cop/rails/date.rb
71
+ - lib/rubocop/cop/rails/delegate.rb
72
+ - lib/rubocop/cop/rails/delegate_allow_blank.rb
73
+ - lib/rubocop/cop/rails/dynamic_find_by.rb
74
+ - lib/rubocop/cop/rails/enum_uniqueness.rb
75
+ - lib/rubocop/cop/rails/environment_comparison.rb
76
+ - lib/rubocop/cop/rails/exit.rb
77
+ - lib/rubocop/cop/rails/file_path.rb
78
+ - lib/rubocop/cop/rails/find_by.rb
79
+ - lib/rubocop/cop/rails/find_each.rb
80
+ - lib/rubocop/cop/rails/has_and_belongs_to_many.rb
81
+ - lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb
82
+ - lib/rubocop/cop/rails/helper_instance_variable.rb
83
+ - lib/rubocop/cop/rails/http_positional_arguments.rb
84
+ - lib/rubocop/cop/rails/http_status.rb
85
+ - lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb
86
+ - lib/rubocop/cop/rails/inverse_of.rb
87
+ - lib/rubocop/cop/rails/lexically_scoped_action_filter.rb
88
+ - lib/rubocop/cop/rails/link_to_blank.rb
89
+ - lib/rubocop/cop/rails/not_null_column.rb
90
+ - lib/rubocop/cop/rails/output.rb
91
+ - lib/rubocop/cop/rails/output_safety.rb
92
+ - lib/rubocop/cop/rails/pluralization_grammar.rb
93
+ - lib/rubocop/cop/rails/presence.rb
94
+ - lib/rubocop/cop/rails/present.rb
95
+ - lib/rubocop/cop/rails/read_write_attribute.rb
96
+ - lib/rubocop/cop/rails/redundant_allow_nil.rb
97
+ - lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb
98
+ - lib/rubocop/cop/rails/reflection_class_name.rb
99
+ - lib/rubocop/cop/rails/refute_methods.rb
100
+ - lib/rubocop/cop/rails/relative_date_constant.rb
101
+ - lib/rubocop/cop/rails/request_referer.rb
102
+ - lib/rubocop/cop/rails/reversible_migration.rb
103
+ - lib/rubocop/cop/rails/safe_navigation.rb
104
+ - lib/rubocop/cop/rails/save_bang.rb
105
+ - lib/rubocop/cop/rails/scope_args.rb
106
+ - lib/rubocop/cop/rails/skips_model_validations.rb
107
+ - lib/rubocop/cop/rails/time_zone.rb
108
+ - lib/rubocop/cop/rails/uniq_before_pluck.rb
109
+ - lib/rubocop/cop/rails/unknown_env.rb
110
+ - lib/rubocop/cop/rails/validation.rb
111
+ - lib/rubocop/cop/rails_cops.rb
112
+ - lib/rubocop/rails.rb
113
+ - lib/rubocop/rails/inject.rb
114
+ - lib/rubocop/rails/version.rb
115
+ homepage: https://github.com/rubocop-hq/rubocop-rails
116
+ licenses:
117
+ - MIT
118
+ metadata:
119
+ homepage_uri: https://github.com/rubocop-hq/rubocop-rails/
120
+ changelog_uri: https://github.com/rubocop-hq/rubocop-rails/blob/master/CHANGELOG.md
121
+ source_code_uri: https://github.com/rubocop-hq/rubocop-rails/
122
+ documentation_uri: https://rubocop.readthedocs.io/
123
+ bug_tracker_uri: https://github.com/rubocop-hq/rubocop-rails/issues
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 2.3.0
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubygems_version: 3.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Automatic Rails code style checking tool.
143
+ test_files: []