rubocop-petal 0.9.0 → 1.1.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
  SHA256:
3
- metadata.gz: f457dc147b37b31a89fc5f511560442983a438b191e5723fe95d981f8cb32e91
4
- data.tar.gz: 197112fd588a74778acc75c2d0da8238dcc4681c49636b5ed7a543728b0abe97
3
+ metadata.gz: 5e03823477b0b4b7ae8716d58bc3f14fe3eb639979ae2f26a96f2a6f2b959dd9
4
+ data.tar.gz: 36e8f4940ac8ec120b918f580a625d40b56d32e2fcf4a0b502b90f16ad7881bb
5
5
  SHA512:
6
- metadata.gz: a455e3b3db660c5fa02a148263582aee5e82ad561638d8094f2beff087c08eb0e8414e45f2eccd4f1bbaa7d8740e41317fb5421dea0a9e39a41fbc947cb65f49
7
- data.tar.gz: f89c1da3350fccba89c879092bfb8734f2a903714017cf177e24183a3bb44864c86c90ab1648bb9a5cf8cf0f0347b7dc2f4bfdebb139a684ac9a45b7af5676a7
6
+ metadata.gz: 57d28ac81e61b2b04f5828254b9ae62e5a13e30cf153cc993cb600fd33a5295ee2976c70504bcef6ebadb1179bf30f66895fca3cf99cea170fc70a719de99b3b
7
+ data.tar.gz: 377ce23841da62012e8abe7b9512e2fe39afc74fb14bd42712ab65b983efb62969d96b4c9e4638996e996ea7a7db1c8559b12c15f0d262754b7eb324e6bee8fb
data/README.md CHANGED
@@ -5,6 +5,14 @@
5
5
 
6
6
  Petal custom cops. List of cops can be find [here](https://github.com/petalmd/rubocop-petal/tree/main/lib/rubocop/cop).
7
7
 
8
+ Petal global cops configuration for:
9
+
10
+ * rubocop
11
+ * rubocop-rspec
12
+ * rubocop-performance
13
+ * rubocop-rails
14
+ * rubocop-petal
15
+
8
16
  ## Installation
9
17
 
10
18
  Add this line to your application's Gemfile:
@@ -23,11 +31,11 @@ Or install it yourself as:
23
31
 
24
32
  ## Usage
25
33
 
26
- In your `.rubocop.yml` file, add `rubocop-petal`
34
+ In your `.rubocop.yml` file, add
27
35
 
28
36
  ```yaml
29
- require:
30
- - rubocop-petal
37
+ inherit_gem:
38
+ rubocop-petal: 'config/base.yml'
31
39
  ```
32
40
 
33
41
  ## Development
@@ -46,7 +54,7 @@ bundle exec rake 'new_cop[Rails/MyNewCop]'
46
54
  ```
47
55
 
48
56
  Have a look to [RuboCop documentation](https://docs.rubocop.org/rubocop/1.23/development.html) to get started with
49
- _node pattern_.
57
+ _node pattern_. [Rubocop AST](https://github.com/rubocop/rubocop-ast/blob/master/docs/modules/ROOT/pages/node_pattern.adoc)
50
58
 
51
59
  ## Release
52
60
 
data/config/base.yml ADDED
@@ -0,0 +1,110 @@
1
+ require:
2
+ - rubocop-rails
3
+ - rubocop-performance
4
+ - rubocop-rspec
5
+ - rubocop-petal
6
+
7
+ AllCops:
8
+ SuggestExtensions: false
9
+ DisplayCopNames: true
10
+ NewCops: enable
11
+ DisplayStyleGuide: true
12
+ ExtraDetails: true
13
+ StyleGuideBaseURL: https://rubystyle.guide
14
+ Layout/CaseIndentation:
15
+ IndentOneStep: true
16
+ Layout/MultilineAssignmentLayout:
17
+ SupportedTypes:
18
+ - case
19
+ - if
20
+ Layout/MultilineMethodCallIndentation:
21
+ EnforcedStyle: indented
22
+ IndentationWidth: 2
23
+ Layout/MultilineArrayLineBreaks:
24
+ Enabled: true
25
+ Layout/MultilineHashKeyLineBreaks:
26
+ Enabled: true
27
+ Layout/MultilineMethodArgumentLineBreaks:
28
+ Enabled: true
29
+ Lint/AmbiguousBlockAssociation:
30
+ Exclude:
31
+ - spec/**/*
32
+ Metrics/AbcSize:
33
+ Max: 20
34
+ Metrics/BlockLength:
35
+ Enabled: false
36
+ Metrics/BlockNesting:
37
+ Enabled: false
38
+ Metrics/ClassLength:
39
+ Enabled: false
40
+ Metrics/CyclomaticComplexity:
41
+ Max: 9
42
+ Metrics/MethodLength:
43
+ Enabled: false
44
+ Metrics/ModuleLength:
45
+ Enabled: false
46
+ Metrics/PerceivedComplexity:
47
+ Max: 10
48
+ RSpec:
49
+ StyleGuideBaseURL: https://rspec.rubystyle.guide
50
+ RSpec/ContextWording:
51
+ Prefixes:
52
+ - when
53
+ - with
54
+ - without
55
+ - if
56
+ - unless
57
+ - for
58
+ RSpec/ExampleLength:
59
+ Enabled: false
60
+ RSpec/ExampleWithoutDescription:
61
+ EnforcedStyle: single_line_only
62
+ RSpec/HookArgument:
63
+ Enabled: false
64
+ RSpec/LetSetup:
65
+ Enabled: false
66
+ RSpec/MessageSpies:
67
+ EnforcedStyle: receive
68
+ RSpec/MultipleExpectations:
69
+ Enabled: false
70
+ RSpec/MultipleMemoizedHelpers:
71
+ Enabled: false
72
+ RSpec/NamedSubject:
73
+ Enabled: false
74
+ RSpec/NestedGroups:
75
+ Max: 5
76
+ RSpec/PredicateMatcher:
77
+ Enabled: false
78
+ RSpec/StubbedMock:
79
+ Enabled: false
80
+ RSpec/DescribeClass:
81
+ Exclude:
82
+ - spec/integration/**/*.rb
83
+ Rails/SaveBang:
84
+ Enabled: true
85
+ Rails/NotNullColumn:
86
+ Enabled: false
87
+ Rails/UnknownEnv:
88
+ Environments:
89
+ - production
90
+ - development
91
+ - test
92
+ - staging
93
+ Rails/WhereExists:
94
+ Enabled: false
95
+ Rails/TableNameAssignment:
96
+ Enabled: true
97
+ Style/CollectionMethods:
98
+ PreferredMethods:
99
+ detect: detect
100
+ collect: map
101
+ Style/DateTime:
102
+ Enabled: true
103
+ Style/Documentation:
104
+ Enabled: false
105
+ Style/FrozenStringLiteralComment:
106
+ EnforcedStyle: always_true
107
+ Style/NumericLiterals:
108
+ MinDigits: 8
109
+ Style/ReturnNil:
110
+ Enabled: true
data/config/default.yml CHANGED
@@ -5,6 +5,18 @@ Grape/PreferNamespace:
5
5
  Include:
6
6
  - app/api/**/*
7
7
 
8
+ Migration/AlwaysBulkChangeTable:
9
+ Description: 'Suggest to always use `bulk: true` when using `change_table`.'
10
+ Enabled: true
11
+ Include:
12
+ - db/migrate/**
13
+
14
+ Migration/ChangeTableReferences:
15
+ Description: 'Prevent using `t.references` or `t.belongs_to` in a change_table.'
16
+ Enabled: true
17
+ Include:
18
+ - db/migrate/**
19
+
8
20
  Migration/ForeignKeyOption:
9
21
  Description: 'Specify the foreign key option to create the constraint.'
10
22
  Enabled: true
@@ -67,13 +79,6 @@ Rails/RiskyActiverecordInvocation:
67
79
  Description: 'Interpolation, use hash or parameterized syntax.'
68
80
  Enabled: true
69
81
 
70
- Rails/TableName:
71
- Description: 'Prevent usage of table_name= to in favor of convention.'
72
- Enabled: true
73
- StyleGuide: "https://rails.rubystyle.guide/#keep-ar-defaults"
74
- Include:
75
- - app/models/**/*
76
-
77
82
  Chewy/ResetOnType:
78
83
  Description: 'Prevent using reset! methods on Chewy type class'
79
84
  Enabled: true
@@ -14,7 +14,7 @@ module RuboCop
14
14
  # get :some_path {}
15
15
  #
16
16
  class UnnecessaryNamespace < Base
17
- MSG = 'Unnecessary usage of Grape namespace. '\
17
+ MSG = 'Unnecessary usage of Grape namespace. ' \
18
18
  'Specify endpoint name with an argument: `get :some_path`.'
19
19
  HTTP_ACTIONS = Set.new(%i[get head put post patch delete])
20
20
  GRAPE_NAMESPACE_ALIAS = Set.new(%i[namespace resource resources])
@@ -50,7 +50,7 @@ module RuboCop
50
50
  return if http_action_node.size != 1
51
51
 
52
52
  paths = paths_added_with_http_action(http_action_node.first)
53
- add_offense(node) if paths.size.zero?
53
+ add_offense(node) if paths.empty?
54
54
  end
55
55
 
56
56
  private
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Using `bulk: true` with `change_table` is recommended.
7
+ # Without `bulk: true`, `change_table` generates multiple
8
+ # `ALTER TABLE` statements and the migration process will
9
+ # be duplicated.
10
+ #
11
+ # @bad
12
+ # change_table :users do |t|
13
+ # t.string :first_name
14
+ # t.string :last_name
15
+ # end
16
+ #
17
+ # @good
18
+ # change_table :users, bulk: true do |t|
19
+ # t.string :first_name
20
+ # t.string :last_name
21
+ # end
22
+ #
23
+ class AlwaysBulkChangeTable < Rails::BulkChangeTable
24
+ MSG = 'Add `bulk: true` when using change_table.'
25
+ RESTRICT_ON_SEND = %i[change_table].freeze
26
+
27
+ def on_send(node)
28
+ return unless node.command?(:change_table)
29
+
30
+ unless include_bulk_options?(node)
31
+ add_offense(node)
32
+ return
33
+ end
34
+
35
+ add_offense(node) unless bulk_options_true?(node)
36
+ end
37
+
38
+ def bulk_options_true?(node)
39
+ options = node.arguments[1]
40
+ options.each_pair do |key, value|
41
+ return true if key.value == :bulk && value.true_type?
42
+ end
43
+ false
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Migration
6
+ # Prevent using `t.references` or `t.belongs_to` in a change_table.
7
+ # Internally, `t.references` use multiple `ALTER TABLE` statements.
8
+ # Since Rails cannot transform automatically `t.references` inside
9
+ # a `change_table bulk: true` we can manually create the equivalent
10
+ # `ALTER TABLE` statement using `t.bigint`, `t.index` and `t.foreign_key`.
11
+ #
12
+ # #bad
13
+ # change_table :subscriptions, bulk: true do |t|
14
+ # t.references :user, null: false, foreign_key: true
15
+ # end
16
+ #
17
+ # #good
18
+ # change_table :subscriptions, bulk: true do |t|
19
+ # t.bigint :user_id, null: false
20
+ # t.index :user_id
21
+ # t.foreign_key :users, column: :user_id
22
+ # end
23
+ class ChangeTableReferences < Base
24
+ MSG = 'Use a combination of `t.bigint`, `t.index` and `t.foreign_key` in a change_table.'
25
+
26
+ # @!method add_references_in_block?(node)
27
+ def_node_search :add_references_in_block?, <<~PATTERN
28
+ (send lvar /references|belongs_to/ ...)
29
+ PATTERN
30
+
31
+ # @!method change_table?(node)
32
+ def_node_search :change_table?, <<~PATTERN
33
+ (send nil? :change_table ...)
34
+ PATTERN
35
+
36
+ def on_block(node)
37
+ return unless change_table?(node)
38
+
39
+ references_node = node.children.detect { |n| add_references_in_block?(n) }
40
+ return unless references_node
41
+
42
+ arguments = references_node.child_nodes[1]
43
+ references_methods_range = references_node.source_range.with(end_pos: arguments.source_range.begin_pos - 1)
44
+ add_offense(references_methods_range)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RuboCop
4
4
  module Petal
5
- VERSION = '0.9.0'
5
+ VERSION = '1.1.0'
6
6
  end
7
7
  end
data/lib/rubocop/petal.rb CHANGED
@@ -3,9 +3,8 @@
3
3
  require_relative 'petal/version'
4
4
 
5
5
  module RuboCop
6
- module Petal # rubocop:disable Style/Documentation
6
+ module Petal
7
7
  class Error < StandardError; end
8
- # Your code goes here...
9
8
  PROJECT_ROOT = Pathname.new(__dir__).parent.parent.expand_path.freeze
10
9
  CONFIG_DEFAULT = PROJECT_ROOT.join('config', 'default.yml').freeze
11
10
  CONFIG = YAML.safe_load(CONFIG_DEFAULT.read).freeze
metadata CHANGED
@@ -1,49 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-petal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jean-Francis Bastien
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2023-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: 1.7.0
20
- - - "<"
17
+ - - "~>"
21
18
  - !ruby/object:Gem::Version
22
- version: '2.0'
19
+ version: 1.50.0
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - ">="
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.50.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop-performance
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
28
32
  - !ruby/object:Gem::Version
29
- version: 1.7.0
30
- - - "<"
33
+ version: 1.17.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
31
39
  - !ruby/object:Gem::Version
32
- version: '2.0'
40
+ version: 1.17.0
33
41
  - !ruby/object:Gem::Dependency
34
42
  name: rubocop-rails
35
43
  requirement: !ruby/object:Gem::Requirement
36
44
  requirements:
37
45
  - - "~>"
38
46
  - !ruby/object:Gem::Version
39
- version: '2.10'
47
+ version: '2.19'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.19'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 2.17.0
40
62
  type: :runtime
41
63
  prerelease: false
42
64
  version_requirements: !ruby/object:Gem::Requirement
43
65
  requirements:
44
66
  - - "~>"
45
67
  - !ruby/object:Gem::Version
46
- version: '2.10'
68
+ version: 2.17.0
47
69
  description:
48
70
  email:
49
71
  - jfbastien@petalmd.com
@@ -55,19 +77,21 @@ extra_rdoc_files:
55
77
  files:
56
78
  - LICENSE.txt
57
79
  - README.md
80
+ - config/base.yml
58
81
  - config/default.yml
59
82
  - lib/rubocop-petal.rb
60
83
  - lib/rubocop/cop/chewy/reset_on_type.rb
61
84
  - lib/rubocop/cop/grape/helpers_include_module.rb
62
85
  - lib/rubocop/cop/grape/prefer_namespace.rb
63
86
  - lib/rubocop/cop/grape/unnecessary_namespace.rb
87
+ - lib/rubocop/cop/migration/always_bulk_change_table.rb
88
+ - lib/rubocop/cop/migration/change_table_references.rb
64
89
  - lib/rubocop/cop/migration/foreign_key_option.rb
65
90
  - lib/rubocop/cop/migration/schema_statements_methods.rb
66
91
  - lib/rubocop/cop/performance/snif.rb
67
92
  - lib/rubocop/cop/petal_cops.rb
68
93
  - lib/rubocop/cop/rails/enum_prefix.rb
69
94
  - lib/rubocop/cop/rails/risky_activerecord_invocation.rb
70
- - lib/rubocop/cop/rails/table_name.rb
71
95
  - lib/rubocop/cop/rspec/authenticated_as.rb
72
96
  - lib/rubocop/cop/rspec/create_list_max.rb
73
97
  - lib/rubocop/cop/rspec/json_response.rb
@@ -1,26 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module Rails
6
- # This cop enforces the absence of explicit table name definitions.
7
- #
8
- # # bad
9
- # self.table_name = 'some_table_name'
10
- # self.table_name = :some_other_name
11
- class TableName < Base
12
- include ActiveRecordHelper
13
-
14
- MSG = %(
15
- Avoid using `self.table_name=` if at all possible. When ActiveRecord's defaults are used, it may be omitted.
16
- If you are working on a new model, you should name the table and model to fit the defaults
17
- If you absolutely must use it, disable the cop with `# rubocop:disable Rails/TableName`
18
- )
19
-
20
- def on_send(node)
21
- add_offense(node) if find_set_table_name(node).to_a.any?
22
- end
23
- end
24
- end
25
- end
26
- end