immosquare-cleaner 0.1.54 → 0.1.55

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: af665f762e14a1bdcd0e70e34ce6d520db0acf3c4adffc8a418877fcffede1c4
4
- data.tar.gz: 7e8b7dfff3619c6a314b22f0a09959acec888e63d049a6c9dff311f495c2117a
3
+ metadata.gz: 65d2e535ec442aca2a519d4fabfcd0b12f657d5de198c23113f10408c1eb29d8
4
+ data.tar.gz: 599c4fa0d4699527fa9d3f120beab4ef1ba632e57de22a47f85528e63f88bb89
5
5
  SHA512:
6
- metadata.gz: 2fc51d6c15c4451af904583f070738e09e81e56c0e5c168a6d28cd7af0a936dfa4f223752b689a9884524762b785f14ce2757fd2f12956c1b4ef8236736e2393
7
- data.tar.gz: 63225554da9fbc25c57480eee2f393eb784b1da35e8eaeb3c7eb2f7d898c2c607d5cad1b4ed204ff3683f921df9e17d22b2decb8f7b87406c5a98d3739efa351
6
+ metadata.gz: 777ae6c1cfbf3513c31d1c26f407821a9eb8885f4d06ea5b5bd8c9d43f7e18ab88f24acf1828da08e0b393ab51d28c059e38b9d8be9336f3f48abe6f5793f115
7
+ data.tar.gz: b8c2d69225ec9b7e09ddb81610899a191b65387dafbd22a74693f0ec1e28aa1eb19388f80aee252fd9d4c91f14a0f8993a9d7d954449e6950b5969d5c134c98b
@@ -1,3 +1,3 @@
1
1
  module ImmosquareCleaner
2
- VERSION = "0.1.54".freeze
2
+ VERSION = "0.1.55".freeze
3
3
  end
@@ -72,7 +72,13 @@ module ImmosquareCleaner
72
72
  File.write(rubocop_config_with_version_path, rubocop_config.to_yaml)
73
73
  end
74
74
 
75
- cmds = ["bundle exec rubocop -c #{rubocop_config_with_version_path} \"#{file_path}\" #{ImmosquareCleaner.configuration.rubocop_options || "--autocorrect-all"}"]
75
+ ##============================================================##
76
+ ## --autocorrect-all : Auto-correct all offenses that RuboCop can correct, and leave all other offenses unchanged.
77
+ ## --no-parallel : Disable RuboCop's parallel processing for performance reasons because we pass only one file
78
+ ##============================================================##
79
+ rubocop_options = ImmosquareCleaner.configuration.rubocop_options || "--autocorrect-all --no-parallel"
80
+
81
+ cmds = ["bundle exec rubocop -c #{rubocop_config_with_version_path} \"#{file_path}\" #{rubocop_options}"]
76
82
  launch_cmds(cmds)
77
83
  File.normalize_last_line(file_path)
78
84
  return
@@ -89,13 +95,15 @@ module ImmosquareCleaner
89
95
  ##============================================================##
90
96
  ## JS files
91
97
  ## 16/05/2024
92
- ## Depuis eslint V9 (acutellement en V9.2.0), il y a des
98
+ ## ---------
99
+ ## Depuis eslint V9 (acutellement en V9.15.0), il y a des
93
100
  ## erreurs/warnings
94
101
  ## File ignored because of a matching ignore pattern. Use "--no-ignore" to disable file ignore settings or use "--no-warn-ignored" to suppress this warning
95
- ## alors que le fichier ne devrait pas être ignoré...
102
+ ## ---------
96
103
  ## Cela se produit quand le fichier est dans un dossier supérieur
97
- ## à celui de la racine du gem. Pour éviter ce problème on
98
- ## met le fichier dans un dossier temporaire et on le supprime
104
+ ## à celui de la racine du gem. (donc quand on lance depuis une application)
105
+ ## le fichier est ignoré par eslint car il est dans un dossier supérieur
106
+ ## Pour éviter ce problème on met le fichier dans un dossier temporaire et on le supprime
99
107
  ## par la suite.
100
108
  ##============================================================##
101
109
  begin
@@ -1,18 +1,18 @@
1
1
  namespace :immosquare_cleaner do
2
2
  ##============================================================##
3
- ## Function to clean translation files in rails app
3
+ ## Function to clean files in rails app
4
4
  ##============================================================##
5
- desc "Clean translation files in rails app"
6
- task :clean => :environment do
5
+ desc "clean files in rails app"
6
+ task :clean_app => :environment do
7
7
  file_paths = Dir.glob("#{Rails.root}/**/*").reject do |file_path|
8
- test1 = file_path.gsub("#{Rails.root}/", "")
9
- File.directory?(file_path) ||
10
- test1.start_with?("node_modules", "tmp", "public", "log", "app/assets/builds", "app/assets/fonts", "app/assets/images", "vendor") ||
11
- file_path.end_with?(".lock", ".lockb")
8
+ File.directory?(file_path) || file_path.gsub("#{Rails.root}/", "").start_with?("node_modules", "tmp", "public", "log", "app/assets/builds", "app/assets/fonts", "app/assets/images", "vendor") || file_path.end_with?(".lock", ".lockb")
12
9
  end
13
- file_paths.each do |file|
14
- puts file
15
- ImmosquareCleaner.clean(file)
10
+
11
+ puts "Cleaning files..."
12
+
13
+ file_paths.each.with_index do |file_path, index|
14
+ puts "#{index + 1}/#{file_paths.size} - #{file_path}"
15
+ ImmosquareCleaner.clean(file_path)
16
16
  end
17
17
  end
18
18
  end
@@ -0,0 +1,132 @@
1
+ module RuboCop
2
+ module Cop
3
+ module CustomCops
4
+ module Style
5
+ class CommentNormalization < Base
6
+
7
+ extend AutoCorrector
8
+
9
+ MSG = "Comments should be normalized with the standard format if start with ##".freeze
10
+ BORDER_LINE = "###{"=" * 60}##".freeze
11
+ SPACE = " ".freeze
12
+
13
+ def on_new_investigation
14
+ comment_blocks = find_comment_blocks(processed_source.comments)
15
+
16
+ comment_blocks.each do |block|
17
+ if needs_correction?(block)
18
+ ##============================================================##
19
+ ## Pour désactiver les cops suivants
20
+ ##============================================================##
21
+ buffer = processed_source.buffer
22
+ start_pos = buffer.line_range(block.first.location.line).begin_pos
23
+ end_pos = buffer.line_range(block.last.location.line).end_pos
24
+ range = Parser::Source::Range.new(buffer, start_pos, end_pos)
25
+ ignore_node(range)
26
+
27
+ ##============================================================##
28
+ ## Puis, on ajoute l'offense
29
+ ##============================================================##
30
+ add_offense(block.first) do |corrector|
31
+ corrector.replace(range, normalize_comment_block(block))
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+
40
+ ##============================================================##
41
+ ## On ne veut traiter que les commentaires qui :
42
+ ## - commencent par ## (et non #)
43
+ ## - ne sont pas des commentaires de fin de ligne ruby
44
+ ##============================================================##
45
+ def find_comment_blocks(comments)
46
+ blocks = []
47
+ current_block = []
48
+ filtered_comments = comments.select {|comment| line_content(comment).strip.start_with?("##") }
49
+
50
+ filtered_comments.each do |comment|
51
+ if current_block.empty? || comment.location.line == current_block.last.location.line + 1
52
+ current_block << comment
53
+ else
54
+ blocks << current_block
55
+ current_block = [comment]
56
+ end
57
+ end
58
+
59
+ blocks << current_block
60
+ blocks
61
+ end
62
+
63
+ ##============================================================##
64
+ ## Nous n'avons pas besoin de corriger les commentaires si :
65
+ ## - le premier et le dernier commentaire sont des lignes de bordure
66
+ ## - tous les commentaires commencent par ##
67
+ ## - il n'y a pas de ligne autre que les lignes de bordure qui commence par ##=
68
+ ##============================================================##
69
+ def needs_correction?(block)
70
+ return false if block.compact.empty?
71
+
72
+
73
+ return false if block.first.text == BORDER_LINE &&
74
+ block.last.text == BORDER_LINE &&
75
+ block.all? {|comment| comment.text.start_with?("##") } &&
76
+ block.each_with_index.none? do |comment, index|
77
+ index != 0 &&
78
+ index != block.length - 1 &&
79
+ comment.text.start_with?("##=")
80
+ end
81
+
82
+ true
83
+ end
84
+
85
+ ##============================================================##
86
+ ## Pour formater correctement le block de commentaires
87
+ ##============================================================##
88
+ def normalize_comment_block(block)
89
+ indent_level = indent_level(block.first)
90
+ body = block.map.with_index do |comment, index|
91
+ ##============================================================##
92
+ ## On met un espace après les ## si le caractère n'est pas un espace
93
+ ##============================================================##
94
+ text = comment.text.to_s.strip
95
+ text = text.gsub(/^##(?![=\s])/, "###{SPACE}")
96
+ if text.start_with?("##=")
97
+ index == 0 || index == block.size - 1 ? nil : "###{SPACE}---------"
98
+ else
99
+ text = "###{SPACE}#{text}" if !text.start_with?("###{SPACE}")
100
+ text = text.chomp("##").strip
101
+ text
102
+ end
103
+ end.compact
104
+
105
+
106
+ ##============================================================##
107
+ ## Le block va être remis à la place du block original sur
108
+ ## la colone du block original. Donc la première ligne du block
109
+ ## ne doit pas être indentée manuellement. Par contre les autres
110
+ ## lignes doivent être indentées sur la même colonne que la première
111
+ ##============================================================##
112
+ [BORDER_LINE, body, BORDER_LINE].flatten.map.with_index {|line, index| index == 0 ? line : "#{SPACE * indent_level}#{line}" }.join("\n")
113
+ end
114
+
115
+ ##============================================================##
116
+ ## Pour récupérer le contenu de la ligne courante
117
+ ##============================================================##
118
+ def line_content(comment)
119
+ processed_source.lines[comment.location.line - 1]
120
+ end
121
+
122
+ def indent_level(line)
123
+ line_content(line)[/\A */].size
124
+ end
125
+
126
+
127
+
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
@@ -2,7 +2,7 @@
2
2
  require:
3
3
  - rubocop/cop/style/method_call_with_args_parentheses_override
4
4
  - rubocop/cop/custom_cops/style/use_credentials_instead_of_env
5
- - rubocop/cop/custom_cops/pre/comment_normalization
5
+ - rubocop/cop/custom_cops/style/comment_normalization
6
6
  AllCops:
7
7
  NewCops: enable
8
8
  EnabledByDefault: false
@@ -94,7 +94,7 @@ Style/MethodCallWithArgsParentheses:
94
94
  Enabled: true
95
95
  CustomCops/Style/UseCredentialsInsteadOfEnv:
96
96
  Enabled: true
97
- CustomCops/Pre/CommentNormalization:
97
+ CustomCops/Style/CommentNormalization:
98
98
  Enabled: true
99
99
  Gemspec/RequireMFA:
100
100
  Enabled: false
data/linters/rubocop.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  require:
2
2
  - rubocop/cop/style/method_call_with_args_parentheses_override
3
3
  - rubocop/cop/custom_cops/style/use_credentials_instead_of_env
4
- - rubocop/cop/custom_cops/pre/comment_normalization
4
+ - rubocop/cop/custom_cops/style/comment_normalization
5
5
 
6
6
  # Pour activer toutes les méthodes de Rubocop et activer le cache pour les gros fichiers
7
7
  AllCops:
@@ -109,7 +109,7 @@ Style/MethodCallWithArgsParentheses:
109
109
  #################### CUSTOMS ###########################
110
110
  CustomCops/Style/UseCredentialsInsteadOfEnv:
111
111
  Enabled: true # On veut forcer l'utilisation de credentials au lieu de ENV pour les secrets
112
- CustomCops/Pre/CommentNormalization:
112
+ CustomCops/Style/CommentNormalization:
113
113
  Enabled: true # On veut forcer la normalisation des commentaires
114
114
 
115
115
  #################### GEM ###########################
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.54
4
+ version: 0.1.55
5
5
  platform: ruby
6
6
  authors:
7
7
  - immosquare
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2024-12-22 00:00:00.000000000 Z
10
+ date: 2024-12-23 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: erb_lint
@@ -132,7 +132,7 @@ files:
132
132
  - linters/prettier.yml
133
133
  - linters/rubocop-3.3.6.yml
134
134
  - linters/rubocop.yml
135
- - linters/rubocop/cop/custom_cops/pre/comment_normalization.rb
135
+ - linters/rubocop/cop/custom_cops/style/comment_normalization.rb
136
136
  - linters/rubocop/cop/custom_cops/style/use_credentials_instead_of_env.rb
137
137
  - linters/rubocop/cop/style/method_call_with_args_parentheses_override.rb
138
138
  - node_modules/.bin/acorn
@@ -1,122 +0,0 @@
1
- module RuboCop
2
- module Cop
3
- module CustomCops
4
- ##============================================================##
5
- ## Ce cop dépend du module PRE pour être lancer top dans le
6
- ## processus de vérification pour que ensuite Layout/CommentIndentation
7
- ## intende correctement les commentaires modifiés.
8
- ## Department Layout (y compris Layout/CommentIndentation)
9
- ## Department Lint
10
- ## Department Style
11
- ## Department Metricstoto
12
- ##============================================================##
13
- module Pre
14
- class CommentNormalization < Base
15
-
16
- extend AutoCorrector
17
-
18
- MSG = "Comments should be normalized with the standard format if start with ##".freeze
19
- BORDER_LINE = "###{"=" * 60}##".freeze
20
-
21
- ##============================================================##
22
- ## 1 - var js, fjs = d.getElementsByTagName(s)[0];
23
- ## équivaut à var js = null et var fjs(first JS) d.getElementsByTagName(s)[0]
24
- ## On compile le fichier avec Terser pour qu'il soit minifié
25
- ##============================================================##
26
- def on_new_investigation
27
- comment_blocks = find_comment_blocks(processed_source.comments)
28
-
29
- comment_blocks.each do |block|
30
- add_offense(block.first) do |corrector|
31
- corrector.replace(range_for_block(block), normalize_comment_block(block))
32
- end if needs_correction?(block)
33
- end
34
- end
35
-
36
- private
37
-
38
-
39
- ##============================================================##
40
- ## On ne veut traiter que les commentaires qui :
41
- ## - commencent par ## (et non #)
42
- ## - ne sont pas des commentaires de fin de ligne ruby
43
- ##============================================================##
44
- def find_comment_blocks(comments)
45
- blocks = []
46
- current_block = []
47
- filtered_comments = comments.select {|comment| current_line(comment).strip.start_with?("##") }
48
-
49
- filtered_comments.each do |comment|
50
- if current_block.empty? || consecutive_comments?(current_block.last, comment)
51
- current_block << comment
52
- else
53
- blocks << current_block unless current_block.empty?
54
- current_block = [comment]
55
- end
56
- end
57
-
58
- blocks << current_block unless current_block.empty?
59
- blocks
60
- end
61
-
62
- ##============================================================##
63
- ## Pour récupérer le contenu de la ligne courante
64
- ##============================================================##
65
- def current_line(comment)
66
- processed_source.lines[comment.location.line - 1]
67
- end
68
-
69
- def consecutive_comments?(previous_comment, current_comment)
70
- return false unless previous_comment && current_comment
71
-
72
- previous_line = previous_comment.location.line
73
- current_line = current_comment.location.line
74
- current_line == previous_line + 1
75
- end
76
-
77
- def needs_correction?(block)
78
- indent = indentation(block.first)
79
- expected_border = "#{indent}#{BORDER_LINE}"
80
-
81
- return false if block.first.text == expected_border &&
82
- block.last.text == expected_border &&
83
- block.all? {|comment| comment.text.start_with?("#{indent}## ") || comment.text == expected_border }
84
-
85
- true
86
- end
87
-
88
- def normalize_comment_block(block)
89
- indent = indentation(block.first)
90
- normalized_lines = []
91
-
92
- normalized_lines << "#{indent}#{BORDER_LINE}"
93
-
94
- block.each do |comment|
95
- text = comment.text.to_s.strip
96
- next if text.start_with?("##=")
97
-
98
- text = "## #{text}" if !text.start_with?("## ")
99
- text = text.chomp("##").strip
100
- normalized_lines << "#{indent}#{text}"
101
- end
102
-
103
- normalized_lines << "#{indent}#{BORDER_LINE}"
104
- normalized_lines.join("\n")
105
- end
106
-
107
- def indentation(comment)
108
- comment.text.match(/^\s*/)[0]
109
- end
110
-
111
- def range_for_block(block)
112
- start_pos = block.first.location.expression.begin_pos
113
- end_pos = block.last.location.expression.end_pos
114
-
115
- Parser::Source::Range.new(processed_source.buffer, start_pos, end_pos)
116
- end
117
-
118
- end
119
- end
120
- end
121
- end
122
- end