Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constants MAGIC_COMMENT_MATCHER #711

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:.*(?:\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))/).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