Sha256: 0e71fe824a6b52e982756f17c172beb0b2705439171687c00482e3e5ffae5929

Contents?: true

Size: 847 Bytes

Versions: 4

Compression:

Stored size: 847 Bytes

Contents

# frozen_string_literal: true

require_relative "./regexes"

module Harri
  module Processor
    # Removes the redundant imports from a string.
    def self.remove_imports(text, import_info)
      import_regex = Harri::Regexes.import_declaration_regex import_info[:module]
      match = text.match import_regex

      return text if !match

      if import_info[:imports].empty?
        # There are no imports, so the whole module is redundant.
        text.sub(/^#{Regexp.quote(match[0])}/, "")
      else
        # Filter out specific imports within the module.
        filtered_imports = import_info[:imports].reduce(match[0]) do |result, import|
          reference_regex = Harri::Regexes.named_reference_regex import
          result.sub reference_regex, ""
        end
        text.sub match[0], filtered_imports
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
harri-0.1.3 lib/harri/processor.rb
harri-0.1.2 lib/harri/processor.rb
harri-0.1.1 lib/harri/processor.rb
harri-0.1.0 lib/harri/processor.rb