gitlab-styles 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.gitlab-ci.yml +13 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +1 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +43 -0
  10. data/Rakefile +6 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/default.yml +1229 -0
  14. data/gitlab-styles.gemspec +29 -0
  15. data/lib/gitlab/styles.rb +6 -0
  16. data/lib/gitlab/styles/rubocop.rb +30 -0
  17. data/lib/gitlab/styles/rubocop/cop/active_record_dependent.rb +30 -0
  18. data/lib/gitlab/styles/rubocop/cop/active_record_serialize.rb +22 -0
  19. data/lib/gitlab/styles/rubocop/cop/custom_error_class.rb +68 -0
  20. data/lib/gitlab/styles/rubocop/cop/gem_fetcher.rb +41 -0
  21. data/lib/gitlab/styles/rubocop/cop/in_batches.rb +20 -0
  22. data/lib/gitlab/styles/rubocop/cop/migration/add_column.rb +56 -0
  23. data/lib/gitlab/styles/rubocop/cop/migration/add_column_with_default_to_large_table.rb +59 -0
  24. data/lib/gitlab/styles/rubocop/cop/migration/add_concurrent_foreign_key.rb +31 -0
  25. data/lib/gitlab/styles/rubocop/cop/migration/add_concurrent_index.rb +38 -0
  26. data/lib/gitlab/styles/rubocop/cop/migration/add_index.rb +52 -0
  27. data/lib/gitlab/styles/rubocop/cop/migration/add_timestamps.rb +29 -0
  28. data/lib/gitlab/styles/rubocop/cop/migration/datetime.rb +40 -0
  29. data/lib/gitlab/styles/rubocop/cop/migration/hash_index.rb +55 -0
  30. data/lib/gitlab/styles/rubocop/cop/migration/remove_concurrent_index.rb +33 -0
  31. data/lib/gitlab/styles/rubocop/cop/migration/remove_index.rb +30 -0
  32. data/lib/gitlab/styles/rubocop/cop/migration/reversible_add_column_with_default.rb +39 -0
  33. data/lib/gitlab/styles/rubocop/cop/migration/safer_boolean_column.rb +98 -0
  34. data/lib/gitlab/styles/rubocop/cop/migration/timestamps.rb +31 -0
  35. data/lib/gitlab/styles/rubocop/cop/migration/update_column_in_batches.rb +45 -0
  36. data/lib/gitlab/styles/rubocop/cop/polymorphic_associations.rb +27 -0
  37. data/lib/gitlab/styles/rubocop/cop/project_path_helper.rb +55 -0
  38. data/lib/gitlab/styles/rubocop/cop/redirect_with_status.rb +48 -0
  39. data/lib/gitlab/styles/rubocop/cop/rspec/single_line_hook.rb +42 -0
  40. data/lib/gitlab/styles/rubocop/migration_helpers.rb +15 -0
  41. data/lib/gitlab/styles/rubocop/model_helpers.rb +15 -0
  42. data/lib/gitlab/styles/version.rb +5 -0
  43. metadata +169 -0
@@ -0,0 +1,27 @@
1
+ require_relative '../model_helpers'
2
+
3
+ module Gitlab
4
+ module Styles
5
+ module Rubocop
6
+ module Cop
7
+ # Cop that prevents the use of polymorphic associations
8
+ class PolymorphicAssociations < RuboCop::Cop::Cop
9
+ include ModelHelpers
10
+
11
+ MSG = 'Do not use polymorphic associations, use separate tables instead'.freeze
12
+
13
+ def on_send(node)
14
+ return unless in_model?(node)
15
+ return unless node.children[1] == :belongs_to
16
+
17
+ node.children.last.each_node(:pair) do |pair|
18
+ key_name = pair.children[0].children[0]
19
+
20
+ add_offense(pair, :expression) if key_name == :polymorphic
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,55 @@
1
+ module Gitlab
2
+ module Styles
3
+ module Rubocop
4
+ module Cop
5
+ class ProjectPathHelper < RuboCop::Cop::Cop
6
+ MSG = 'Use short project path helpers without explicitly passing the namespace: ' \
7
+ '`foo_project_bar_path(project, bar)` instead of ' \
8
+ '`foo_namespace_project_bar_path(project.namespace, project, bar)`.'.freeze
9
+
10
+ METHOD_NAME_PATTERN = /\A([a-z_]+_)?namespace_project(?:_[a-z_]+)?_(?:url|path)\z/
11
+
12
+ def on_send(node)
13
+ return unless method_name(node).to_s =~ METHOD_NAME_PATTERN
14
+
15
+ namespace_expr, project_expr = arguments(node)
16
+ return unless namespace_expr && project_expr
17
+
18
+ return unless namespace_expr.type == :send
19
+ return unless method_name(namespace_expr) == :namespace
20
+ return unless receiver(namespace_expr) == project_expr
21
+
22
+ add_offense(node, :selector)
23
+ end
24
+
25
+ def autocorrect(node)
26
+ helper_name = method_name(node).to_s.sub('namespace_project', 'project')
27
+
28
+ arguments = arguments(node)
29
+ arguments.shift # Remove namespace argument
30
+
31
+ replacement = "#{helper_name}(#{arguments.map(&:source).join(', ')})"
32
+
33
+ lambda do |corrector|
34
+ corrector.replace(node.source_range, replacement)
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def receiver(node)
41
+ node.children[0]
42
+ end
43
+
44
+ def method_name(node)
45
+ node.children[1]
46
+ end
47
+
48
+ def arguments(node)
49
+ node.children[2..-1]
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,48 @@
1
+ module Gitlab
2
+ module Styles
3
+ module Rubocop
4
+ module Cop
5
+ # This cop prevents usage of 'redirect_to' in actions 'destroy' without specifying 'status'.
6
+ # See https://gitlab.com/gitlab-org/gitlab-ce/issues/31840
7
+ class RedirectWithStatus < RuboCop::Cop::Cop
8
+ MSG = 'Do not use "redirect_to" without "status" in "destroy" action'.freeze
9
+
10
+ def on_def(node)
11
+ return unless in_controller?(node)
12
+ return unless destroy?(node) || destroy_all?(node)
13
+
14
+ node.each_descendant(:send) do |def_node|
15
+ next unless redirect_to?(def_node)
16
+
17
+ methods = []
18
+
19
+ def_node.children.last.each_node(:pair) do |pair|
20
+ methods << pair.children.first.children.first
21
+ end
22
+
23
+ add_offense(def_node, :selector) unless methods.include?(:status)
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def in_controller?(node)
30
+ node.location.expression.source_buffer.name.end_with?('_controller.rb')
31
+ end
32
+
33
+ def destroy?(node)
34
+ node.children.first == :destroy
35
+ end
36
+
37
+ def destroy_all?(node)
38
+ node.children.first == :destroy_all
39
+ end
40
+
41
+ def redirect_to?(node)
42
+ node.children[1] == :redirect_to
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,42 @@
1
+ require 'rubocop-rspec'
2
+
3
+ module Gitlab
4
+ module Styles
5
+ module Rubocop
6
+ module Cop
7
+ module RSpec
8
+ # This cop checks for single-line hook blocks
9
+ #
10
+ # @example
11
+ #
12
+ # # bad
13
+ # before { do_something }
14
+ # after(:each) { undo_something }
15
+ #
16
+ # # good
17
+ # before do
18
+ # do_something
19
+ # end
20
+ #
21
+ # after(:each) do
22
+ # undo_something
23
+ # end
24
+ class SingleLineHook < RuboCop::Cop::Cop
25
+ MESSAGE = "Don't use single-line hook blocks.".freeze
26
+
27
+ def_node_search :rspec_hook?, <<~PATTERN
28
+ (send nil {:after :around :before} ...)
29
+ PATTERN
30
+
31
+ def on_block(node)
32
+ return unless rspec_hook?(node)
33
+ return unless node.single_line?
34
+
35
+ add_offense(node, :expression, MESSAGE)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,15 @@
1
+ module Gitlab
2
+ module Styles
3
+ module Rubocop
4
+ # Module containing helper methods for writing migration cops.
5
+ module MigrationHelpers
6
+ # Returns true if the given node originated from the db/migrate directory.
7
+ def in_migration?(node)
8
+ dirname = File.dirname(node.location.expression.source_buffer.name)
9
+
10
+ dirname.end_with?('db/migrate', 'db/post_migrate')
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module Gitlab
2
+ module Styles
3
+ module Rubocop
4
+ module ModelHelpers
5
+ # Returns true if the given node originated from the models directory.
6
+ def in_model?(node)
7
+ path = node.location.expression.source_buffer.name
8
+ models_path = File.join(Dir.pwd, 'app', 'models')
9
+
10
+ path.start_with?(models_path)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,5 @@
1
+ module Gitlab
2
+ module Styles
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitlab-styles
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - GitLab
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-18 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.49'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.49'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.15'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop-gitlab-security
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.15'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description:
98
+ email:
99
+ - remy@rymai.me
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".gitlab-ci.yml"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - default.yml
116
+ - gitlab-styles.gemspec
117
+ - lib/gitlab/styles.rb
118
+ - lib/gitlab/styles/rubocop.rb
119
+ - lib/gitlab/styles/rubocop/cop/active_record_dependent.rb
120
+ - lib/gitlab/styles/rubocop/cop/active_record_serialize.rb
121
+ - lib/gitlab/styles/rubocop/cop/custom_error_class.rb
122
+ - lib/gitlab/styles/rubocop/cop/gem_fetcher.rb
123
+ - lib/gitlab/styles/rubocop/cop/in_batches.rb
124
+ - lib/gitlab/styles/rubocop/cop/migration/add_column.rb
125
+ - lib/gitlab/styles/rubocop/cop/migration/add_column_with_default_to_large_table.rb
126
+ - lib/gitlab/styles/rubocop/cop/migration/add_concurrent_foreign_key.rb
127
+ - lib/gitlab/styles/rubocop/cop/migration/add_concurrent_index.rb
128
+ - lib/gitlab/styles/rubocop/cop/migration/add_index.rb
129
+ - lib/gitlab/styles/rubocop/cop/migration/add_timestamps.rb
130
+ - lib/gitlab/styles/rubocop/cop/migration/datetime.rb
131
+ - lib/gitlab/styles/rubocop/cop/migration/hash_index.rb
132
+ - lib/gitlab/styles/rubocop/cop/migration/remove_concurrent_index.rb
133
+ - lib/gitlab/styles/rubocop/cop/migration/remove_index.rb
134
+ - lib/gitlab/styles/rubocop/cop/migration/reversible_add_column_with_default.rb
135
+ - lib/gitlab/styles/rubocop/cop/migration/safer_boolean_column.rb
136
+ - lib/gitlab/styles/rubocop/cop/migration/timestamps.rb
137
+ - lib/gitlab/styles/rubocop/cop/migration/update_column_in_batches.rb
138
+ - lib/gitlab/styles/rubocop/cop/polymorphic_associations.rb
139
+ - lib/gitlab/styles/rubocop/cop/project_path_helper.rb
140
+ - lib/gitlab/styles/rubocop/cop/redirect_with_status.rb
141
+ - lib/gitlab/styles/rubocop/cop/rspec/single_line_hook.rb
142
+ - lib/gitlab/styles/rubocop/migration_helpers.rb
143
+ - lib/gitlab/styles/rubocop/model_helpers.rb
144
+ - lib/gitlab/styles/version.rb
145
+ homepage: https://github.com/gitlab-org/gitlab-styles
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.6.13
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: GitLab style guides and shared style configs.
169
+ test_files: []