Class: DiffTest::Integrations::RailsJs::Annotator
- Inherits:
-
Object
- Object
- DiffTest::Integrations::RailsJs::Annotator
- Defined in:
- lib/diff_test/integrations/rails_js/annotator.rb
Constant Summary collapse
- UNANNOTATE_REGEX =
/(\/\/|#) Automatically added by DiffTest.*?(\/\/|#) Automatically added by DiffTest\n/m
Class Method Summary collapse
- .annotate(content, file_path) ⇒ Object
- .annotate_file(file_path) ⇒ Object
- .annotated?(content) ⇒ Boolean
- .insert_at_first_non_comment_line_str(content, text_to_insert) ⇒ Object
- .unannotate(content) ⇒ Object
- .unannotate_file(file_path) ⇒ Object
Class Method Details
.annotate(content, file_path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/diff_test/integrations/rails_js/annotator.rb', line 19 def self.annotate(content, file_path) return content if annotated?(content) coffee = file_path.end_with?('.coffee') comment = coffee ? '#' : '//' safe_method_call = coffee ? '?' : '?.' typescript = file_path.end_with?('ts') || file_path.end_with?('tsx') disable_lint_comments = "" disable_lint_comments += "#{comment} @ts-ignore\n" if typescript disable_lint_comments += "#{comment} prettier-ignore\n" disable_lint_comments += "#{comment} eslint-disable-next-line\n" annotation = "#{comment} Automatically added by DiffTest\n#{disable_lint_comments};(globalThis || window)?.diffTestTrackJsFile#{safe_method_call}(\"#{file_path}\");\n#{comment} Automatically added by DiffTest\n" insert_at_first_non_comment_line_str(content, annotation) end |
.annotate_file(file_path) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/diff_test/integrations/rails_js/annotator.rb', line 5 def self.annotate_file(file_path) = DiffTest::Helper.(file_path) content = File.read() content = annotate(content, file_path) File.write(, content) end |
.annotated?(content) ⇒ Boolean
43 44 45 |
# File 'lib/diff_test/integrations/rails_js/annotator.rb', line 43 def self.annotated?(content) content.include?('diffTestTrackJsFile') end |
.insert_at_first_non_comment_line_str(content, text_to_insert) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/diff_test/integrations/rails_js/annotator.rb', line 47 def self.insert_at_first_non_comment_line_str(content, text_to_insert) lines = content.lines in_multiline_comment = false comment_type = nil insert_index = nil lines.each_with_index do |line, idx| stripped = line.strip # --- End of multi-line comments if in_multiline_comment case comment_type when :js if stripped.include?("*/") in_multiline_comment = false comment_type = nil end when :coffee if stripped.include?('###') in_multiline_comment = false comment_type = nil end end next end # --- Start of multi-line comments if stripped.start_with?('/*') in_multiline_comment = true comment_type = :js next elsif stripped.start_with?('###') in_multiline_comment = true comment_type = :coffee next end # --- Single-line comments if stripped.empty? || stripped.start_with?('//') || stripped.start_with?('#') next end # --- Found first non-comment line insert_index = idx break end insert_index ||= lines.size new_lines = lines.dup new_lines.insert(insert_index, text_to_insert) new_lines.join end |
.unannotate(content) ⇒ Object
38 39 40 41 |
# File 'lib/diff_test/integrations/rails_js/annotator.rb', line 38 def self.unannotate(content) content.gsub!(UNANNOTATE_REGEX, '') content end |
.unannotate_file(file_path) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/diff_test/integrations/rails_js/annotator.rb', line 12 def self.unannotate_file(file_path) = DiffTest::Helper.(file_path) content = File.read() content = unannotate(content) File.write(, content) end |