Sha256: fbe00a50e6c4208f4c8f1a49815f6194badf530fd9fa3e167b020d4a78a93c95

Contents?: true

Size: 1.04 KB

Versions: 1

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module AnnotateRb
  module ModelAnnotator
    module FileParser
      # Extracts magic comments strings and returns them
      class MagicCommentParser
        MAGIC_COMMENTS = [
          HASH_ENCODING = /(^#\s*encoding:.*(?:\n|r\n))/,
          HASH_CODING = /(^# coding:.*(?:\n|\r\n))/,
          HASH_FROZEN_STRING = /(^#\s*frozen_string_literal:.+(?:\n|\r\n))/,
          STAR_ENCODING = /(^# -\*- encoding\s?:.*(?:\n|\r\n))/,
          STAR_CODING = /(^# -\*- coding:.*(?:\n|\r\n))/,
          STAR_FROZEN_STRING = /(^# -\*- frozen_string_literal\s*:.+-\*-(?:\n|\r\n))/,
          SORBET_TYPED_STRING = /(^#\s*typed:.*(?:\n|r\n))/.freeze
        ].freeze

        MAGIC_COMMENTS_REGEX = Regexp.union(*MAGIC_COMMENTS).freeze

        class << self
          def call(content)
            magic_comments = content.scan(MAGIC_COMMENTS_REGEX).flatten.compact

            if magic_comments.any?
              magic_comments.join
            else
              ""
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
annotaterb-4.5.0 lib/annotate_rb/model_annotator/file_parser/magic_comment_parser.rb