momocop 0.1.7 → 0.1.9

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: 2217b77f50980c7b628b6be000f20a76093b41f13933b5e0e4c04c860215eb68
4
- data.tar.gz: 3fd67a7eb04625b7021db01f453477f804b8e48609c9f41e859579d46239d89e
3
+ metadata.gz: dd1936728acc124e621799990c68af8e6aa54c7cd984ce038efcbe8929c2416c
4
+ data.tar.gz: 0b6b2242cb847844e0279ff35193c391922facae5c32dc2ca86cf7778e97aebc
5
5
  SHA512:
6
- metadata.gz: 5d5dfa8b78691d8cf4bc8938d3bed507969eec0dbc3940032a22eb3ca0fa705abbecc82f1f4586be8e8d9e963afd5144572399c2854abed205f85c866b75fbe4
7
- data.tar.gz: 7feb8e1104912f6d595becbd4b4fa20ec3fd38910892d52da3f64b12f5fc6ac37f4ef352f6a57b29b094d5c2fce434895eb9c54d8bc0e5516c5ce24b9ce9a082
6
+ metadata.gz: 34e3af0e7bcb5a2b3091fa79e713ceb4fd50acc2bec9c7c815680fb93cb37e0052ba08d70e2adc35a5ba4718c4b622e738acfc0a0e6f33113021978eedb3b1f7
7
+ data.tar.gz: fa078fbc91b3bfe61ac9da25463b59eaab3871e882c16f9e59e8d40d04bb12d82e948d6c08d933a6534b7600661fb6ce13db3009fbfcf850d5ca4cdf2708ce85
data/.rubocop.yml CHANGED
@@ -56,6 +56,9 @@ Style/FrozenStringLiteralComment:
56
56
  Style/IfUnlessModifier:
57
57
  Enabled: false
58
58
 
59
+ Style/MultilineBlockChain:
60
+ Enabled: false
61
+
59
62
  Style/ParallelAssignment:
60
63
  Enabled: false
61
64
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Momocop
4
- VERSION = '0.1.7'
4
+ VERSION = '0.1.9'
5
5
  end
@@ -43,6 +43,14 @@ module RuboCop
43
43
 
44
44
  block_node = node.block_node
45
45
 
46
+ # Add block if it's missing
47
+ unless block_node
48
+ add_offense(node, message: MSG) do |corrector|
49
+ indentation = ' ' * node.loc.column
50
+ corrector.replace(node.source_range, "#{node.source} do\n#{indentation}end")
51
+ end
52
+ end
53
+
46
54
  # Check missing associations
47
55
  defined_associations = get_defined_association_names(block_node)
48
56
  model_associations = get_model_association_names(class_name)
@@ -52,8 +60,6 @@ module RuboCop
52
60
  return unless missing_associations.any?
53
61
 
54
62
  add_offense(node, message: MSG) do |corrector|
55
- next unless block_node
56
-
57
63
  # Add newline before closing block if it's a one-liner
58
64
  if one_line_block?(block_node)
59
65
  indentation = ' ' * node.loc.column
@@ -66,6 +72,8 @@ module RuboCop
66
72
 
67
73
  # TODO: calculate indentation size
68
74
  indentation = ' ' * (node.loc.column + 2)
75
+
76
+ # use send node if blockless
69
77
  corrector.insert_after(block_node.loc.begin, "\n#{indentation}#{definition}")
70
78
  end
71
79
  end
@@ -76,6 +84,8 @@ module RuboCop
76
84
  end
77
85
 
78
86
  private def one_line_block?(block_node)
87
+ return false if block_node.nil?
88
+
79
89
  block_node.loc.begin.line == block_node.loc.end.line
80
90
  end
81
91
 
@@ -50,6 +50,14 @@ module RuboCop
50
50
 
51
51
  block_node = node.block_node
52
52
 
53
+ # Add block if it's missing
54
+ unless block_node
55
+ add_offense(node, message: MSG) do |corrector|
56
+ indentation = ' ' * node.loc.column
57
+ corrector.replace(node.source_range, "#{node.source} do\n#{indentation}end")
58
+ end
59
+ end
60
+
53
61
  # Check missing associations
54
62
 
55
63
  # Exclude defined sequences
@@ -49,11 +49,15 @@ module RuboCop
49
49
  return unless inside_factory_bot_define?(node)
50
50
 
51
51
  block_node = node.block_node
52
+ return unless block_node
53
+
52
54
  entire_definitions = defined_properties(block_node)
53
55
 
54
56
  sections =
55
57
  entire_definitions
56
- .slice_when { |a, b| b.loc.last_line - a.loc.last_line > 1 }
58
+ .slice_when { |a, b|
59
+ (range_with_comments(b).first_line - range_with_comments(a).last_line) > 1
60
+ }
57
61
  .select { |definitions| definitions.size >= 2 }
58
62
 
59
63
  sections.each do |definitions|
@@ -83,11 +87,31 @@ module RuboCop
83
87
 
84
88
  private def defined_properties(block_node)
85
89
  body_node = block_node&.children&.last
86
- body_node&.children&.select { |node| definition_node?(node) }
90
+
91
+ # empty block
92
+ return [] unless body_node
93
+
94
+ # begin
95
+ if body_node.begin_type?
96
+ body_node&.children&.select { |node| definition_node?(node) } || []
97
+ # block
98
+ elsif body_node.send_type? && definition_node?(body_node)
99
+ [body_node]
100
+ else
101
+ []
102
+ end
87
103
  end
88
104
 
105
+ RUBOCOP_HELPER_METHODS = %i[trait transient before after].freeze
106
+
89
107
  private def definition_node?(node)
90
- node.send_type? || (node.block_type? && node.children.first.send_type?)
108
+ if node.send_type?
109
+ return !RUBOCOP_HELPER_METHODS.include?(node.method_name)
110
+ elsif node.block_type? && node.children.first.send_type?
111
+ return !RUBOCOP_HELPER_METHODS.include?(node.children.first.method_name)
112
+ end
113
+
114
+ return false
91
115
  end
92
116
 
93
117
  private def definition_type(node)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: momocop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - supermomonga
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-19 00:00:00.000000000 Z
11
+ date: 2024-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 1.49.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 1.49.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rubocop-rails
43
43
  requirement: !ruby/object:Gem::Requirement