Sha256: 8ae572d3e8768c750f25bed1f2266f1effd67aa8760c45e1b5ce29b96af287b8

Contents?: true

Size: 1.49 KB

Versions: 73

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

ERB_GLOB = "**/*.html{+*,}.erb"
RB_GLOB = "**/*.rb"
BLOCK_EXPR = /\s*((\s+|\))do|\{)(\s*\|[^|]*\|)?\s*\Z/.freeze # copied from Rails: action_view/template/handlers/erb/erubi.rb

namespace :primer_view_components do
  desc "Report arguments used in each component"
  task :report, [:paths] do |_, args|
    require "rubocop"
    require "better_html"
    require "better_html/parser"
    require "erb_lint/processed_source"
    require_relative "helpers/ast_processor"

    paths = args[:paths].split
    stats = {}

    rb_files = paths.reduce([]) { |mem, path| mem + Dir[File.join(path, RB_GLOB)] }

    rb_files.each do |f|
      ast = RuboCop::AST::ProcessedSource.from_file(f, RUBY_VERSION.to_f).ast
      AstProcessor.process_ast(ast, stats)
    end

    erb_files = paths.reduce([]) { |mem, path| mem + Dir[File.join(path, ERB_GLOB)] }

    erb_files.each do |f|
      erb_ast = ERBLint::ProcessedSource.new(f, File.read(f)).ast

      erb_ast.descendants(:erb).each do |erb_node|
        indicator, _, code_node, = *erb_node

        next if indicator&.children&.first == "#" # don't analyze comments

        trimmed_source = code_node.loc.source.sub(BLOCK_EXPR, "").strip
        ast = RuboCop::AST::ProcessedSource.new(trimmed_source, RUBY_VERSION.to_f).ast
        AstProcessor.process_ast(ast, stats)
      end
    end

    File.open(".primer-view-components-report.json", "w") do |f|
      f.write(JSON.pretty_generate(stats))
      f.write($INPUT_RECORD_SEPARATOR)
    end
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
primer_view_components-0.0.72 lib/tasks/primer_view_components.rake
primer_view_components-0.0.71 lib/tasks/primer_view_components.rake
primer_view_components-0.0.70 lib/tasks/primer_view_components.rake
primer_view_components-0.0.69 lib/tasks/primer_view_components.rake
primer_view_components-0.0.68 lib/tasks/primer_view_components.rake
primer_view_components-0.0.67 lib/tasks/primer_view_components.rake
primer_view_components-0.0.66 lib/tasks/primer_view_components.rake
primer_view_components-0.0.65 lib/tasks/primer_view_components.rake
primer_view_components-0.0.64 lib/tasks/primer_view_components.rake
primer_view_components-0.0.63 lib/tasks/primer_view_components.rake
primer_view_components-0.0.62 lib/tasks/primer_view_components.rake
primer_view_components-0.0.61 lib/tasks/primer_view_components.rake
primer_view_components-0.0.60 lib/tasks/primer_view_components.rake