immosquare-cleaner 0.1.70 → 0.1.71

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 782a12c647e52cbe70bc4acd7b09e66e4d2003f7f9bd19f3492a113e3fb439b4
4
- data.tar.gz: 259600c6629bbf15fcc2b88789905524225e5055003f3c326ba2f7586f4dc3a6
3
+ metadata.gz: 8aa35cadcbf70139cbb0af0a069f2a0fe2f0f2eebf7828a2a4f55cd561d244a0
4
+ data.tar.gz: 66bf6deadd22989fa4c3b77d3479760e68e0c8a17e2b3d102567686d2cc38e49
5
5
  SHA512:
6
- metadata.gz: 4518f8288d7f75406220f77dfee70b1c215465c1481a6ae3e6aabc91364c890d99ec77f03836ca4743ab5006e19ae1c2312c7af010b7ad31a4a234411575de60
7
- data.tar.gz: d132961013cec829005dda99912bddd0a5da2914bc014154e5fa5db6c744c158746fe2286b942750e5e1445ccad0ef7fbc5c537d248c3a86fe792bc1583f2e37
6
+ metadata.gz: e5f8431e8415eb8e90fe3373d7c3b2ac26c0753225b52ba4ac22b40323eb2943f7159520f672572c8f74d930f28ece4fef34837c8cefbd0431d801eede1f4e9c
7
+ data.tar.gz: 279c5b7297e477b301904215ce3db41d6c610b7fbd07d0a213f3490be33abb535a3fb6b8b4a865736b8a6bc582861e255c2adae6868738fbf6001e604e94dcdc
@@ -1,3 +1,3 @@
1
1
  module ImmosquareCleaner
2
- VERSION = "0.1.70".freeze
2
+ VERSION = "0.1.71".freeze
3
3
  end
@@ -0,0 +1,127 @@
1
+ module RuboCop
2
+ module Cop
3
+ module CustomCops
4
+ module Style
5
+ ##============================================================##
6
+ ## This cop checks that consecutive assignments are aligned
7
+ ##
8
+ ## @example
9
+ ## bad
10
+ ## d2 = "xxx"
11
+ ## variable = "yyy"
12
+ ## very_long_variable = "zzz"
13
+ ##
14
+ ## good
15
+ ## d2 = "xxx"
16
+ ## variable = "yyy"
17
+ ## very_long_variable = "zzz"
18
+ ##============================================================##
19
+ class AlignAssignments < Base
20
+
21
+ extend AutoCorrector
22
+
23
+ MSG = "Align the assignment operators of consecutive assignments.".freeze
24
+ ASSIGNMENT_TYPES = [:lvasgn, :casgn, :ivasgn, :cvasgn, :gvasgn].freeze
25
+
26
+ def on_new_investigation
27
+ @assignment_groups = find_assignment_groups
28
+ end
29
+
30
+ ASSIGNMENT_TYPES.each do |type|
31
+ define_method(:"on_#{type}") do |node|
32
+ check_alignment(node)
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def find_assignment_groups
39
+ assignments_by_line = {}
40
+
41
+ processed_source.ast.each_node do |node|
42
+ next unless ASSIGNMENT_TYPES.include?(node.type)
43
+
44
+ assignments_by_line[node.source_range.line] = node
45
+ end
46
+
47
+ group_consecutive_assignments(assignments_by_line)
48
+ end
49
+
50
+ def group_consecutive_assignments(assignments_by_line)
51
+ groups = []
52
+ current_group = []
53
+ previous_line = nil
54
+
55
+ assignments_by_line.keys.sort.each do |line|
56
+ if previous_line&.+(1) == line
57
+ current_group << assignments_by_line[previous_line] if current_group.empty?
58
+ current_group << assignments_by_line[line]
59
+ else
60
+ groups << current_group if current_group.size > 1
61
+ current_group = [assignments_by_line[line]]
62
+ end
63
+ previous_line = line
64
+ end
65
+
66
+ groups << current_group if current_group.size > 1
67
+ groups
68
+ end
69
+
70
+ def check_alignment(node)
71
+ group = @assignment_groups.find {|g| g.include?(node) }
72
+ return unless group
73
+
74
+ target_column = calculate_target_column(group)
75
+ current_column = assignment_operator_column(node)
76
+
77
+ return if current_column == target_column
78
+
79
+ spaces_needed = target_column - current_column
80
+ return if spaces_needed <= 0
81
+
82
+ add_offense(node.source_range, :message => MSG) do |corrector|
83
+ corrector.insert_before(assignment_operator_range(node), " " * spaces_needed)
84
+ end
85
+ end
86
+
87
+ def calculate_target_column(group)
88
+ base_column = group.first.source_range.column
89
+ max_var_length = group.map {|node| variable_name_length(node) }.max
90
+ base_column + max_var_length + 1
91
+ end
92
+
93
+ def variable_name_length(node)
94
+ case node.type
95
+ when :lvasgn, :ivasgn, :cvasgn, :gvasgn
96
+ node.children[0].to_s.length
97
+ when :casgn
98
+ if node.children[0]
99
+ "#{node.children[0].source}::#{node.children[1]}".length
100
+ else
101
+ node.children[1].to_s.length
102
+ end
103
+ end
104
+ end
105
+
106
+ def assignment_operator_column(node)
107
+ assignment_operator_range(node).column
108
+ end
109
+
110
+ def assignment_operator_range(node)
111
+ source = node.source
112
+ equals_pos = source.index("=")
113
+ return node.source_range if equals_pos.nil?
114
+
115
+ start_pos = node.source_range.begin_pos + equals_pos
116
+ range(processed_source.buffer, start_pos, start_pos + 1)
117
+ end
118
+
119
+ def range(buffer, start_pos, end_pos)
120
+ Parser::Source::Range.new(buffer, start_pos, end_pos)
121
+ end
122
+
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -3,6 +3,7 @@ require:
3
3
  - rubocop/cop/style/method_call_with_args_parentheses_override
4
4
  - rubocop/cop/custom_cops/style/comment_normalization
5
5
  - rubocop/cop/custom_cops/style/font_awesome_normalization
6
+ - rubocop/cop/custom_cops/style/align_assignments
6
7
  AllCops:
7
8
  NewCops: enable
8
9
  EnabledByDefault: false
data/linters/rubocop.yml CHANGED
@@ -2,6 +2,7 @@ require:
2
2
  - rubocop/cop/style/method_call_with_args_parentheses_override
3
3
  - rubocop/cop/custom_cops/style/comment_normalization
4
4
  - rubocop/cop/custom_cops/style/font_awesome_normalization
5
+ - rubocop/cop/custom_cops/style/align_assignments
5
6
 
6
7
  # Pour activer toutes les méthodes de Rubocop et activer le cache pour les gros fichiers
7
8
  AllCops:
@@ -113,6 +114,9 @@ CustomCops/Style/CommentNormalization:
113
114
  CustomCops/Style/FontAwesomeNormalization:
114
115
  Enabled: true # On veut forcer l'utilisation du nom complet de la font awesome
115
116
 
117
+ CustomCops/Style/AlignAssignments:
118
+ Enabled: true # On veut forcer l'alignement des assignations consécutives
119
+
116
120
  #################### GEM ###########################
117
121
  Gemspec/RequireMFA:
118
122
  Enabled: false # On ne veut pas obliger le MFA pour pusher un gem
data/package.json CHANGED
@@ -2,14 +2,14 @@
2
2
  "name": "immosquare-cleaner",
3
3
  "private": true,
4
4
  "dependencies": {
5
- "@eslint/js": "^9.24.0",
6
- "eslint": "^9.24.0",
5
+ "@eslint/js": "^9.29.0",
6
+ "eslint": "^9.29.0",
7
7
  "eslint-plugin-align-assignments": "^1.1.2",
8
8
  "eslint-plugin-align-import": "^1.0.0",
9
9
  "eslint-plugin-erb": "^2.1.1",
10
10
  "eslint-plugin-prefer-arrow": "^1.2.3",
11
- "eslint-plugin-sonarjs": "^3.0.2",
12
- "prettier": "^3.5.3"
11
+ "eslint-plugin-sonarjs": "^3.0.3",
12
+ "prettier": "^3.6.0"
13
13
  },
14
14
  "scripts": {"morning": "bundle update && bundle clean --force && bun update && bundle exec immosquare-cleaner 'package.json'"}
15
15
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.70
4
+ version: 0.1.71
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare
@@ -152,6 +152,7 @@ files:
152
152
  - linters/prettier.yml
153
153
  - linters/rubocop-3.4.1.yml
154
154
  - linters/rubocop.yml
155
+ - linters/rubocop/cop/custom_cops/style/align_assignments.rb
155
156
  - linters/rubocop/cop/custom_cops/style/comment_normalization.rb
156
157
  - linters/rubocop/cop/custom_cops/style/font_awesome_normalization.rb
157
158
  - linters/rubocop/cop/style/method_call_with_args_parentheses_override.rb
@@ -175,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
176
  - !ruby/object:Gem::Version
176
177
  version: '0'
177
178
  requirements: []
178
- rubygems_version: 3.6.7
179
+ rubygems_version: 3.7.0
179
180
  specification_version: 4
180
181
  summary: A gem to lint and organize files in a Rails application.
181
182
  test_files: []