immosquare-cleaner 0.1.53 → 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: 9196f9f8512b09892046a08e62a7d88651c64a303de24c06939f879892e52cf7
4
- data.tar.gz: ebb02f2943eec1220d90bd122fb8eb74e93924e08e140d03d869dc9a046f2f35
3
+ metadata.gz: 65d2e535ec442aca2a519d4fabfcd0b12f657d5de198c23113f10408c1eb29d8
4
+ data.tar.gz: 599c4fa0d4699527fa9d3f120beab4ef1ba632e57de22a47f85528e63f88bb89
5
5
  SHA512:
6
- metadata.gz: fad7b547a77506b0c92bc672a395e0ccee00ca2f7092fb838d9b621138e059bd57bc74e5317d0fbb4228a65899d1c591704e2f30fa8e0118afa0ce65c133da3a
7
- data.tar.gz: ea5754c29eaf9d6bcd7f2e7b1531808d8a540a021f28f446d36cbd159ccd400c33cf2a1ed691da9b43ce096bf86408f5b4f625f3ed897d417029daa336b6e1da
6
+ metadata.gz: 777ae6c1cfbf3513c31d1c26f407821a9eb8885f4d06ea5b5bd8c9d43f7e18ab88f24acf1828da08e0b393ab51d28c059e38b9d8be9336f3f48abe6f5793f115
7
+ data.tar.gz: b8c2d69225ec9b7e09ddb81610899a191b65387dafbd22a74693f0ec1e28aa1eb19388f80aee252fd9d4c91f14a0f8993a9d7d954449e6950b5969d5c134c98b
@@ -4,9 +4,9 @@ require "immosquare-cleaner"
4
4
  require "optparse"
5
5
 
6
6
 
7
- ##===========================================================================##
7
+ ##============================================================##
8
8
  ## We check if bun.sh is installed
9
- ##===========================================================================##
9
+ ##============================================================##
10
10
  def bun_installed?
11
11
  system("which bun > /dev/null 2>&1")
12
12
  end
@@ -18,49 +18,49 @@ end
18
18
 
19
19
 
20
20
  options = {}
21
- ##===========================================================================##
22
- ## optparse is a standard library of ruby, it's used to parse command line ##
23
- ##===========================================================================##
21
+ ##============================================================##
22
+ ## optparse is a standard library of ruby, it's used to parse command line
23
+ ##============================================================##
24
24
  OptionParser.new do |opts|
25
25
  opts.banner = "Usage: immosquare-cleaner [options] file"
26
26
 
27
- ##===========================================================================##
28
- ## Capture all single-letter options (like '-t', '-a', etc.) ##
29
- ##===========================================================================##
27
+ ##============================================================##
28
+ ## Capture all single-letter options (like '-t', '-a', etc.)
29
+ ##============================================================##
30
30
  ("a".."z").each do |letter|
31
31
  opts.on("-#{letter} [OPTION]", "Option -#{letter}") do |value|
32
32
  options[letter.to_sym] = value
33
33
  end
34
34
  end
35
35
 
36
- ##===========================================================================##
37
- ## If you also want to capture longer options, add them here ##
38
- ## Example: ##
39
- ## opts.on("--tag TAG", "Tag option") do |t| ##
40
- ## options[:tag] = t ##
41
- ## end ##
42
- ##===========================================================================##
36
+ ##============================================================##
37
+ ## If you also want to capture longer options, add them here
38
+ ## Example:
39
+ ## opts.on("--tag TAG", "Tag option") do |t|
40
+ ## options[:tag] = t
41
+ ## end
42
+ ##============================================================##
43
43
  end.parse!
44
44
 
45
- ##===========================================================================##
46
- ## Check if the file path is provided ##
47
- ##===========================================================================##
45
+ ##============================================================##
46
+ ## Check if the file path is provided
47
+ ##============================================================##
48
48
  file_path = ARGV[0]
49
49
  if !file_path
50
50
  puts("Error: Please provide a file path.")
51
51
  exit 1
52
52
  end
53
53
 
54
- ##===========================================================================##
55
- ## Check if the file exists ##
56
- ##===========================================================================##
54
+ ##============================================================##
55
+ ## Check if the file exists
56
+ ##============================================================##
57
57
  if !File.exist?(file_path)
58
58
  puts("Error: The file '#{file_path}' does not exist.")
59
59
  exit 1
60
60
  end
61
61
 
62
62
 
63
- ##===========================================================================##
64
- ## We can now call the clean method, passing the file path and options ##
65
- ##===========================================================================##
63
+ ##============================================================##
64
+ ## We can now call the clean method, passing the file path and options
65
+ ##============================================================##
66
66
  ImmosquareCleaner.clean(file_path, **options)
@@ -1,3 +1,3 @@
1
1
  module ImmosquareCleaner
2
- VERSION = "0.1.53".freeze
2
+ VERSION = "0.1.55".freeze
3
3
  end
@@ -7,24 +7,24 @@ require_relative "immosquare-cleaner/configuration"
7
7
  require_relative "immosquare-cleaner/markdown"
8
8
  require_relative "immosquare-cleaner/railtie" if defined?(Rails)
9
9
 
10
- ##===========================================================================##
10
+ ##============================================================##
11
11
  ## Importing the 'English' library allows us to use more human-readable
12
12
  ## global variables, such as $INPUT_RECORD_SEPARATOR instead of $/,
13
13
  ## which enhances code clarity and makes it easier to understand
14
14
  ## the purpose of these variables in our code.
15
- ##===========================================================================##
15
+ ##============================================================##
16
16
  module ImmosquareCleaner
17
17
  class << self
18
18
 
19
- ##===========================================================================##
19
+ ##============================================================##
20
20
  ## Constants
21
- ##===========================================================================##
21
+ ##============================================================##
22
22
  SHEBANG = "#!/usr/bin/env ruby".freeze
23
23
  RUBY_FILES = [".rb", ".rake", "Gemfile", "Rakefile", ".axlsx", ".gemspec", ".ru", ".podspec", ".jbuilder", ".rabl", ".thor", "config.ru", "Berksfile", "Capfile", "Guardfile", "Podfile", "Thorfile", "Vagrantfile"].freeze
24
24
 
25
- ##===========================================================================##
25
+ ##============================================================##
26
26
  ## Gem configuration
27
- ##===========================================================================##
27
+ ##============================================================##
28
28
  attr_writer :configuration
29
29
 
30
30
  def configuration
@@ -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
@@ -157,10 +165,10 @@ module ImmosquareCleaner
157
165
  File.expand_path("..", __dir__)
158
166
  end
159
167
 
160
- ##===========================================================================##
168
+ ##============================================================##
161
169
  ## We change the current directory to the gem root to ensure the gem's paths
162
170
  ## are used when executing the commands
163
- ##===========================================================================##
171
+ ##============================================================##
164
172
  def launch_cmds(cmds)
165
173
  Dir.chdir(gem_root) do
166
174
  cmds.each {|cmd| system(cmd) }
@@ -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,6 +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/style/comment_normalization
5
6
  AllCops:
6
7
  NewCops: enable
7
8
  EnabledByDefault: false
@@ -93,5 +94,7 @@ Style/MethodCallWithArgsParentheses:
93
94
  Enabled: true
94
95
  CustomCops/Style/UseCredentialsInsteadOfEnv:
95
96
  Enabled: true
97
+ CustomCops/Style/CommentNormalization:
98
+ Enabled: true
96
99
  Gemspec/RequireMFA:
97
100
  Enabled: false
data/linters/rubocop.yml CHANGED
@@ -1,6 +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/style/comment_normalization
4
5
 
5
6
  # Pour activer toutes les méthodes de Rubocop et activer le cache pour les gros fichiers
6
7
  AllCops:
@@ -108,6 +109,8 @@ Style/MethodCallWithArgsParentheses:
108
109
  #################### CUSTOMS ###########################
109
110
  CustomCops/Style/UseCredentialsInsteadOfEnv:
110
111
  Enabled: true # On veut forcer l'utilisation de credentials au lieu de ENV pour les secrets
112
+ CustomCops/Style/CommentNormalization:
113
+ Enabled: true # On veut forcer la normalisation des commentaires
111
114
 
112
115
  #################### GEM ###########################
113
116
  Gemspec/RequireMFA:
metadata CHANGED
@@ -1,103 +1,114 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: immosquare-cleaner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.53
4
+ version: 0.1.55
5
5
  platform: ruby
6
- original_platform: ''
7
6
  authors:
8
7
  - immosquare
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-12-20 00:00:00.000000000 Z
10
+ date: 2024-12-23 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: erb_lint
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.7'
19
+ - - "<="
18
20
  - !ruby/object:Gem::Version
19
- version: '0'
21
+ version: '1000.0'
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
23
25
  requirements:
24
- - - "~>"
26
+ - - ">="
25
27
  - !ruby/object:Gem::Version
26
- version: '0'
28
+ version: '0.7'
29
+ - - "<="
30
+ - !ruby/object:Gem::Version
31
+ version: '1000.0'
27
32
  - !ruby/object:Gem::Dependency
28
33
  name: htmlbeautifier
29
34
  requirement: !ruby/object:Gem::Requirement
30
35
  requirements:
31
- - - "~>"
36
+ - - ">="
32
37
  - !ruby/object:Gem::Version
33
- version: '1'
38
+ version: '1.4'
39
+ - - "<="
40
+ - !ruby/object:Gem::Version
41
+ version: '1000.0'
34
42
  type: :runtime
35
43
  prerelease: false
36
44
  version_requirements: !ruby/object:Gem::Requirement
37
45
  requirements:
38
- - - "~>"
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '1.4'
49
+ - - "<="
39
50
  - !ruby/object:Gem::Version
40
- version: '1'
51
+ version: '1000.0'
41
52
  - !ruby/object:Gem::Dependency
42
53
  name: immosquare-extensions
43
54
  requirement: !ruby/object:Gem::Requirement
44
55
  requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
56
  - - ">="
49
57
  - !ruby/object:Gem::Version
50
- version: 0.1.18
58
+ version: '0.1'
59
+ - - "<="
60
+ - !ruby/object:Gem::Version
61
+ version: '1000.0'
51
62
  type: :runtime
52
63
  prerelease: false
53
64
  version_requirements: !ruby/object:Gem::Requirement
54
65
  requirements:
55
- - - "~>"
56
- - !ruby/object:Gem::Version
57
- version: '0'
58
66
  - - ">="
59
67
  - !ruby/object:Gem::Version
60
- version: 0.1.18
68
+ version: '0.1'
69
+ - - "<="
70
+ - !ruby/object:Gem::Version
71
+ version: '1000.0'
61
72
  - !ruby/object:Gem::Dependency
62
73
  name: immosquare-yaml
63
74
  requirement: !ruby/object:Gem::Requirement
64
75
  requirements:
65
- - - "~>"
66
- - !ruby/object:Gem::Version
67
- version: '0'
68
76
  - - ">="
69
77
  - !ruby/object:Gem::Version
70
- version: 0.1.26
78
+ version: '0.1'
79
+ - - "<="
80
+ - !ruby/object:Gem::Version
81
+ version: '1000.0'
71
82
  type: :runtime
72
83
  prerelease: false
73
84
  version_requirements: !ruby/object:Gem::Requirement
74
85
  requirements:
75
- - - "~>"
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
86
  - - ">="
79
87
  - !ruby/object:Gem::Version
80
- version: 0.1.26
88
+ version: '0.1'
89
+ - - "<="
90
+ - !ruby/object:Gem::Version
91
+ version: '1000.0'
81
92
  - !ruby/object:Gem::Dependency
82
93
  name: rubocop
83
94
  requirement: !ruby/object:Gem::Requirement
84
95
  requirements:
85
- - - "~>"
86
- - !ruby/object:Gem::Version
87
- version: '1'
88
96
  - - ">="
89
97
  - !ruby/object:Gem::Version
90
- version: 1.62.1
98
+ version: '1.68'
99
+ - - "<="
100
+ - !ruby/object:Gem::Version
101
+ version: '1000.0'
91
102
  type: :runtime
92
103
  prerelease: false
93
104
  version_requirements: !ruby/object:Gem::Requirement
94
105
  requirements:
95
- - - "~>"
96
- - !ruby/object:Gem::Version
97
- version: '1'
98
106
  - - ">="
99
107
  - !ruby/object:Gem::Version
100
- version: 1.62.1
108
+ version: '1.68'
109
+ - - "<="
110
+ - !ruby/object:Gem::Version
111
+ version: '1000.0'
101
112
  description: Immosquare-cleaner streamlines Rails applications by running tools like
102
113
  RuboCop, ERBLint, Stylelint and more. It ensures code quality, readability, and
103
114
  consistency across the application.
@@ -121,6 +132,7 @@ files:
121
132
  - linters/prettier.yml
122
133
  - linters/rubocop-3.3.6.yml
123
134
  - linters/rubocop.yml
135
+ - linters/rubocop/cop/custom_cops/style/comment_normalization.rb
124
136
  - linters/rubocop/cop/custom_cops/style/use_credentials_instead_of_env.rb
125
137
  - linters/rubocop/cop/style/method_call_with_args_parentheses_override.rb
126
138
  - node_modules/.bin/acorn
@@ -34471,7 +34483,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
34471
34483
  requirements:
34472
34484
  - - ">="
34473
34485
  - !ruby/object:Gem::Version
34474
- version: 2.7.2
34486
+ version: 3.1.6
34475
34487
  required_rubygems_version: !ruby/object:Gem::Requirement
34476
34488
  requirements:
34477
34489
  - - ">="