Skip to content

Commit

Permalink
Add constants MAGIC_COMMENT_MATCHER
Browse files Browse the repository at this point in the history
  • Loading branch information
nard-tech committed Jan 7, 2020
1 parent ee88f0f commit e11aaac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
10 changes: 4 additions & 6 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ module AnnotateModels
}
}.freeze

MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze

class << self
def annotate_pattern(options = {})
if options[:wrapper_open]
Expand Down Expand Up @@ -537,7 +539,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
# need to insert it in correct position
if old_annotation.empty? || options[:force]
magic_comments_block = magic_comments_as_string(old_content)
old_content.gsub!(magic_comment_matcher, '')
old_content.gsub!(MAGIC_COMMENT_MATCHER, '')
old_content.sub!(annotate_pattern(options), '')

new_content = if %w(after bottom).include?(options[position].to_s)
Expand All @@ -561,12 +563,8 @@ def annotate_one_file(file_name, info_block, position, options = {})
true
end

def magic_comment_matcher
Regexp.new(/(^#\s*encoding:.*(?:\n|r\n))|(^# coding:.*(?:\n|\r\n))|(^# -\*- coding:.*(?:\n|\r\n))|(^# -\*- encoding\s?:.*(?:\n|\r\n))|(^#\s*frozen_string_literal:.+(?:\n|\r\n))|(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/)
end

def magic_comments_as_string(content)
magic_comments = content.scan(magic_comment_matcher).flatten.compact
magic_comments = content.scan(MAGIC_COMMENT_MATCHER).flatten.compact

if magic_comments.any?
magic_comments.join
Expand Down
8 changes: 3 additions & 5 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ module AnnotateRoutes
PREFIX_MD = '## Route Map'.freeze
HEADER_ROW = ['Prefix', 'Verb', 'URI Pattern', 'Controller#Action']

MAGIC_COMMENT_MATCHER = Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/).freeze

class << self
def do_annotations(options = {})
return unless routes_exists?
Expand Down Expand Up @@ -212,7 +214,7 @@ def extract_magic_comments_from_array(content_array)
new_content = []

content_array.map do |row|
if row =~ magic_comment_matcher
if row =~ MAGIC_COMMENT_MATCHER
magic_comments << row.strip
else
new_content << row
Expand Down Expand Up @@ -244,9 +246,5 @@ def real_content_and_header_position(real_content, header_position)
# and the default
return real_content, header_position
end

def magic_comment_matcher
Regexp.new(/(^#\s*encoding:.*)|(^# coding:.*)|(^# -\*- coding:.*)|(^# -\*- encoding\s?:.*)|(^#\s*frozen_string_literal:.+)|(^# -\*- frozen_string_literal\s*:.+-\*-)/)
end
end
end

0 comments on commit e11aaac

Please sign in to comment.