Sha256: 20cf72f0d2cf4abceff3d0b9a9591431b2a42fa649683293903de840dd3a811c

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

# typed: false
# frozen_string_literal: true

require "ast/node"

require "packwerk/node"
require "packwerk/offense"
require "packwerk/parsers"

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

5 entries across 5 versions & 1 rubygems

Version Path
packwerk-1.1.3 lib/packwerk/file_processor.rb
packwerk-1.1.2 lib/packwerk/file_processor.rb
packwerk-1.1.1 lib/packwerk/file_processor.rb
packwerk-1.1.0 lib/packwerk/file_processor.rb
packwerk-1.0.2 lib/packwerk/file_processor.rb