rubocop-standard 1.12.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsControllerRenderPathsExist < Cop
9
+ def_node_matcher :render?, <<-PATTERN
10
+ (send nil? :render $...)
11
+ PATTERN
12
+
13
+ def_node_matcher :render_str?, <<-PATTERN
14
+ (send nil? :render $({str sym} $_) ...)
15
+ PATTERN
16
+
17
+ def_node_matcher :render_options?, <<-PATTERN
18
+ (send nil? :render (hash $...))
19
+ PATTERN
20
+
21
+ def_node_matcher :render_key?, <<-PATTERN
22
+ (pair (sym ${:action :partial :template}) $({str sym} $_))
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless cop_config["ViewPath"]
27
+
28
+ if args = render_str?(node)
29
+ node, path = args
30
+ unless resolve_template(path.to_s)
31
+ add_offense(node, location: :expression, message: "Template could not be found")
32
+ end
33
+ elsif pairs = render_options?(node)
34
+ if pair = pairs.detect { |p| render_key?(p) }
35
+ key, node, path = render_key?(pair)
36
+
37
+ case key
38
+ when :action, :template
39
+ unless resolve_template(path.to_s)
40
+ add_offense(node, location: :expression, message: "Template could not be found")
41
+ end
42
+ when :partial
43
+ unless resolve_partial(path.to_s)
44
+ add_offense(node, location: :expression, message: "Partial template could not be found")
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ def resolve_template(path)
52
+ cop_config["ViewPath"].each do |view_path|
53
+ if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
54
+ return m
55
+ end
56
+ end
57
+ nil
58
+ end
59
+
60
+ def resolve_partial(path)
61
+ parts = path.split(File::SEPARATOR)
62
+ parts << "_#{parts.pop}"
63
+ path = parts.join(File::SEPARATOR)
64
+ resolve_template(path)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsControllerRenderShorthand < Cop
9
+ MSG = "Prefer `render` template shorthand"
10
+
11
+ def_node_matcher :render_with_options?, <<-PATTERN
12
+ (send nil? :render (hash $...))
13
+ PATTERN
14
+
15
+ def_node_matcher :action_key?, <<-PATTERN
16
+ (pair (sym {:action :template}) $({str sym} _))
17
+ PATTERN
18
+
19
+ def_node_matcher :str, <<-PATTERN
20
+ ({str sym} $_)
21
+ PATTERN
22
+
23
+ def investigate(*)
24
+ @autocorrect = {}
25
+ end
26
+
27
+ def autocorrect(node)
28
+ @autocorrect[node]
29
+ end
30
+
31
+ def on_send(node)
32
+ if option_pairs = render_with_options?(node)
33
+ option_pairs.each do |pair|
34
+ if value_node = action_key?(pair)
35
+ comma = option_pairs.length > 1 ? ", " : ""
36
+ corrected_source = node.source
37
+ .sub(/#{pair.source}(,\s*)?/, "")
38
+ .sub("render ", "render \"#{str(value_node)}\"#{comma}")
39
+
40
+ @autocorrect[node] = lambda do |corrector|
41
+ corrector.replace(node.source_range, corrected_source)
42
+ end
43
+ add_offense(node, location: :expression, message: "Use `#{corrected_source}` instead")
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsRenderInline < Cop
9
+ MSG = "Avoid `render inline:`"
10
+
11
+ def_node_matcher :render_with_options?, <<-PATTERN
12
+ (send nil? :render (hash $...))
13
+ PATTERN
14
+
15
+ def_node_matcher :inline_key?, <<-PATTERN
16
+ (pair (sym :inline) $_)
17
+ PATTERN
18
+
19
+ def on_send(node)
20
+ if option_pairs = render_with_options?(node)
21
+ if option_pairs.detect { |pair| inline_key?(pair) }
22
+ add_offense(node, location: :expression)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsRenderObjectCollection < Cop
9
+ MSG = "Avoid `render object:`"
10
+
11
+ def_node_matcher :render_with_options?, <<-PATTERN
12
+ (send nil? :render (hash $...) ...)
13
+ PATTERN
14
+
15
+ def_node_matcher :partial_key?, <<-PATTERN
16
+ (pair (sym :partial) $_)
17
+ PATTERN
18
+
19
+ def_node_matcher :object_key?, <<-PATTERN
20
+ (pair (sym ${:object :collection :spacer_template}) $_)
21
+ PATTERN
22
+
23
+ def on_send(node)
24
+ if option_pairs = render_with_options?(node)
25
+ partial_pair = option_pairs.detect { |pair| partial_key?(pair) }
26
+ object_pair = option_pairs.detect { |pair| object_key?(pair) }
27
+
28
+ if partial_pair && object_pair
29
+ partial_name = partial_key?(partial_pair)
30
+ object_sym, object_node = object_key?(object_pair)
31
+
32
+ case object_sym
33
+ when :object
34
+ if partial_name.children[0].is_a?(String)
35
+ suggestion = ", instead `render partial: #{partial_name.source}, locals: { #{File.basename(partial_name.children[0], '.html.erb')}: #{object_node.source} }`"
36
+ end
37
+ add_offense(node, location: :expression, message: "Avoid `render object:`#{suggestion}")
38
+ when :collection, :spacer_template
39
+ add_offense(node, location: :expression, message: "Avoid `render collection:`")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsViewRenderLiteral < Cop
9
+ MSG = "render must be used with a string literal"
10
+
11
+ def_node_matcher :literal?, <<-PATTERN
12
+ ({str sym true false nil?} ...)
13
+ PATTERN
14
+
15
+ def_node_matcher :render?, <<-PATTERN
16
+ (send nil? :render $...)
17
+ PATTERN
18
+
19
+ def_node_matcher :render_literal?, <<-PATTERN
20
+ (send nil? :render ({str sym} $_) $...)
21
+ PATTERN
22
+
23
+ def_node_matcher :render_with_options?, <<-PATTERN
24
+ (send nil? :render (hash $...) ...)
25
+ PATTERN
26
+
27
+ def_node_matcher :ignore_key?, <<-PATTERN
28
+ (pair (sym {
29
+ :inline
30
+ }) $_)
31
+ PATTERN
32
+
33
+ def_node_matcher :partial_key?, <<-PATTERN
34
+ (pair (sym {
35
+ :file
36
+ :template
37
+ :layout
38
+ :partial
39
+ }) $_)
40
+ PATTERN
41
+
42
+ def on_send(node)
43
+ return unless render?(node)
44
+
45
+ if render_literal?(node)
46
+ elsif option_pairs = render_with_options?(node)
47
+ if option_pairs.any? { |pair| ignore_key?(pair) }
48
+ return
49
+ end
50
+
51
+ if partial_node = option_pairs.map { |pair| partial_key?(pair) }.compact.first
52
+ if !literal?(partial_node)
53
+ add_offense(node, location: :expression)
54
+ end
55
+ else
56
+ add_offense(node, location: :expression)
57
+ end
58
+ else
59
+ add_offense(node, location: :expression)
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsViewRenderPathsExist < Cop
9
+ def_node_matcher :render?, <<-PATTERN
10
+ (send nil? :render $...)
11
+ PATTERN
12
+
13
+ def_node_matcher :render_str?, <<-PATTERN
14
+ (send nil? :render $(str $_) ...)
15
+ PATTERN
16
+
17
+ def_node_matcher :render_options?, <<-PATTERN
18
+ (send nil? :render (hash $...))
19
+ PATTERN
20
+
21
+ def_node_matcher :partial_key?, <<-PATTERN
22
+ (pair (sym :partial) $(str $_))
23
+ PATTERN
24
+
25
+ def on_send(node)
26
+ return unless cop_config["ViewPath"]
27
+
28
+ if args = render_str?(node)
29
+ node, path = args
30
+ unless resolve_partial(path.to_s)
31
+ add_offense(node, location: :expression, message: "Partial template could not be found")
32
+ end
33
+ elsif pairs = render_options?(node)
34
+ if pair = pairs.detect { |p| partial_key?(p) }
35
+ node, path = partial_key?(pair)
36
+
37
+ unless resolve_partial(path.to_s)
38
+ add_offense(node, location: :expression, message: "Partial template could not be found")
39
+ end
40
+ end
41
+ end
42
+ end
43
+
44
+ def resolve_partial(path)
45
+ parts = path.split(File::SEPARATOR)
46
+ parts << "_#{parts.pop}"
47
+ path = parts.join(File::SEPARATOR)
48
+
49
+ cop_config["ViewPath"].each do |view_path|
50
+ if m = Dir[File.join(config.path_relative_to_config(view_path), path) + "*"].first
51
+ return m
52
+ end
53
+ end
54
+ nil
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop"
4
+
5
+ module RuboCop
6
+ module Cop
7
+ module Standard
8
+ class RailsViewRenderShorthand < Cop
9
+ MSG = "Prefer `render` partial shorthand"
10
+
11
+ def_node_matcher :render_with_options?, <<-PATTERN
12
+ (send nil? :render (hash $...))
13
+ PATTERN
14
+
15
+ def_node_matcher :partial_key?, <<-PATTERN
16
+ (pair (sym :partial) $(str _))
17
+ PATTERN
18
+
19
+ def_node_matcher :locals_key?, <<-PATTERN
20
+ (pair (sym :locals) $_)
21
+ PATTERN
22
+
23
+ def on_send(node)
24
+ if option_pairs = render_with_options?(node)
25
+ partial_key = option_pairs.map { |pair| partial_key?(pair) }.compact.first
26
+ locals_key = option_pairs.map { |pair| locals_key?(pair) }.compact.first
27
+
28
+ if option_pairs.length == 1 && partial_key
29
+ add_offense(node, location: :expression, message: "Use `render #{partial_key.source}` instead")
30
+ elsif option_pairs.length == 2 && partial_key && locals_key
31
+ add_offense(node, location: :expression, message: "Use `render #{partial_key.source}, #{locals_key.source}` instead")
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rubocop/cop/standard/rails_application_record"
4
+ require "rubocop/cop/standard/rails_controller_render_action_symbol"
5
+ require "rubocop/cop/standard/rails_controller_render_literal"
6
+ require "rubocop/cop/standard/rails_controller_render_paths_exist"
7
+ require "rubocop/cop/standard/rails_controller_render_shorthand"
8
+ require "rubocop/cop/standard/rails_render_inline"
9
+ require "rubocop/cop/standard/rails_render_object_collection"
10
+ require "rubocop/cop/standard/rails_view_render_literal"
11
+ require "rubocop/cop/standard/rails_view_render_paths_exist"
12
+ require "rubocop/cop/standard/rails_view_render_shorthand"
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubocop-standard
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.12.0
5
+ platform: ruby
6
+ authors:
7
+ - Garen Torikian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.63'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.63'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionview
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ description: Code style checking for Ruby repositories starting with GitHub defaults
70
+ email:
71
+ - gjtorikian@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE
77
+ - README.md
78
+ - STYLEGUIDE.md
79
+ - config/default.yml
80
+ - config/rails.yml
81
+ - guides/rails-controller-render-shorthand.md
82
+ - guides/rails-render-inline.md
83
+ - guides/rails-render-literal.md
84
+ - lib/rubocop/cop/standard.rb
85
+ - lib/rubocop/cop/standard/rails_application_record.rb
86
+ - lib/rubocop/cop/standard/rails_controller_render_action_symbol.rb
87
+ - lib/rubocop/cop/standard/rails_controller_render_literal.rb
88
+ - lib/rubocop/cop/standard/rails_controller_render_paths_exist.rb
89
+ - lib/rubocop/cop/standard/rails_controller_render_shorthand.rb
90
+ - lib/rubocop/cop/standard/rails_render_inline.rb
91
+ - lib/rubocop/cop/standard/rails_render_object_collection.rb
92
+ - lib/rubocop/cop/standard/rails_view_render_literal.rb
93
+ - lib/rubocop/cop/standard/rails_view_render_paths_exist.rb
94
+ - lib/rubocop/cop/standard/rails_view_render_shorthand.rb
95
+ homepage: https://github.com/gjtorikian/rubocop-standard
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: 2.1.0
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.7.6
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: RuboCop Standard
119
+ test_files: []