Sha256: 2722236f893f16ab5b4584b38bcc355291324d946ee6a96c72b9f8d898db45f5

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

# typed: false
# frozen_string_literal: true

require "ast/node"

module Packwerk
  class FileProcessor
    class UnknownFileTypeResult < Offense
      def initialize(file:)
        super(file: file, message: "unknown file type")
      end
    end

    def initialize(node_processor_factory:, parser_factory: nil)
      @node_processor_factory = node_processor_factory
      @parser_factory = parser_factory || Packwerk::Parsers::Factory.instance
    end

    def call(file_path)
      parser = @parser_factory.for_path(file_path)
      return [UnknownFileTypeResult.new(file: file_path)] if parser.nil?

      node = File.open(file_path, "r", external_encoding: Encoding::UTF_8) do |file|
        parser.call(io: file, file_path: file_path)
      rescue Parsers::ParseError => e
        return [e.result]
      end

      result = []
      if node
        node_processor = @node_processor_factory.for(filename: file_path, node: node)
        node_visitor = Packwerk::NodeVisitor.new(node_processor: node_processor)

        node_visitor.visit(node, ancestors: [], result: result)
      end
      result
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
packwerk-1.3.2 lib/packwerk/file_processor.rb
packwerk-1.3.1 lib/packwerk/file_processor.rb
packwerk-1.3.0 lib/packwerk/file_processor.rb
packwerk-1.2.0 lib/packwerk/file_processor.rb