pragmater 7.1.0 → 8.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- # Formats pragma comments in a consistent manner.
5
- class Formatter
6
- def self.shebang_format
7
- %r(\A\#\!\s?\/.*ruby\Z)
8
- end
9
-
10
- def self.pragma_format
11
- /
12
- \A # Start of line.
13
- \# # Start of comment.
14
- \s? # Space - optional.
15
- \w+ # Key - 1 or more word characters only.
16
- \: # Key and value delimiter.
17
- \s? # Space - optional.
18
- [\w\-]+ # Value - 1 or more word or dash characters.
19
- \Z # End of line.
20
- /x
21
- end
22
-
23
- def self.valid_formats
24
- Regexp.union shebang_format, pragma_format
25
- end
26
-
27
- def initialize string
28
- @string = string
29
- end
30
-
31
- def format_shebang
32
- return string unless string.match? self.class.shebang_format
33
-
34
- _, path = string.split "!"
35
- "#! #{path.strip}"
36
- end
37
-
38
- def format_pragma
39
- return string unless string.match? self.class.pragma_format
40
-
41
- key, value = string.split ":"
42
- "# #{key.gsub(/\#\s?/, "")}: #{value.strip}"
43
- end
44
-
45
- def format
46
- klass = self.class
47
-
48
- case string
49
- when klass.shebang_format then format_shebang
50
- when klass.pragma_format then format_pragma
51
- else string
52
- end
53
- end
54
-
55
- private
56
-
57
- attr_reader :string
58
- end
59
- end
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Pragmater
4
- # Writes formatted pragma comments to source file.
5
- # :reek:TooManyInstanceVariables
6
- # :reek:MissingSafeMethod
7
- class Writer
8
- # rubocop:disable Metrics/ParameterLists
9
- def initialize file_path, new_comments, formatter: Formatter, commenter: Commenter
10
- @file_path = file_path
11
- @file_lines = File.readlines file_path
12
- @formatter = formatter
13
- @commenter = commenter
14
- @old_comments = file_comments
15
- @new_comments = new_comments
16
- end
17
- # rubocop:enable Metrics/ParameterLists
18
-
19
- def add
20
- comments = format commenter.new(old_comments, new_comments).add
21
- lines = comments + file_lines_without_comments
22
- insert_spacing! lines, comments
23
- write { lines.join }
24
- end
25
-
26
- def remove
27
- lines = format(commenter.new(old_comments, new_comments).remove) + file_lines_without_comments
28
- remove_spacing! lines
29
- write { lines.join }
30
- end
31
-
32
- private
33
-
34
- attr_reader :file_path, :file_lines, :new_comments, :old_comments, :formatter, :commenter
35
-
36
- def file_comments
37
- file_lines.select { |line| line =~ formatter.valid_formats }
38
- end
39
-
40
- def file_lines_without_comments
41
- file_lines.reject { |line| old_comments.include? line }
42
- end
43
-
44
- # :reek:UtilityFunction
45
- def format lines
46
- lines.map { |line| "#{line}\n" }
47
- end
48
-
49
- # :reek:UtilityFunction
50
- def insert_spacing! lines, comments
51
- comment_count = comments.size
52
-
53
- return if comments.empty?
54
- return if lines.size == 1
55
- return if lines[comment_count] == "\n"
56
-
57
- lines.insert comment_count, "\n"
58
- end
59
-
60
- # :reek:UtilityFunction
61
- def remove_spacing! lines
62
- lines.delete_at 0 if lines.first == "\n"
63
- end
64
-
65
- def write
66
- File.open(file_path, "w") { |file| file.write yield }
67
- end
68
- end
69
- end