Sha256: 8a655dbe558bda33040290df3aac394eab4b5cccc1ca84d20c52ba97d43b3dc8

Contents?: true

Size: 1.63 KB

Versions: 3

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module SmartTodo
  class CommentParser
    attr_reader :todos

    def initialize
      @todos = []
    end

    if Prism.respond_to?(:parse_comments)
      def parse(source, filepath = "-e")
        parse_comments(Prism.parse_comments(source), filepath)
      end

      def parse_file(filepath)
        parse_comments(Prism.parse_file_comments(filepath), filepath)
      end
    else
      def parse(source, filepath = "-e")
        parse_comments(Prism.parse(source, filepath).comments, filepath)
      end

      def parse_file(filepath)
        parse_comments(Prism.parse_file(filepath).comments, filepath)
      end
    end

    class << self
      def parse(source)
        parser = new
        parser.parse(source)
        parser.todos
      end
    end

    private

    if defined?(Prism::InlineComment)
      def inline?(comment)
        comment.is_a?(Prism::InlineComment)
      end
    else
      def inline?(comment)
        comment.type == :inline
      end
    end

    def parse_comments(comments, filepath)
      current_todo = nil

      comments.each do |comment|
        next unless inline?(comment)

        source = comment.location.slice

        if source.match?(/^#\sTODO\(/)
          todos << current_todo if current_todo
          current_todo = Todo.new(source, filepath)
        elsif current_todo && (indent = source[/^#(\s*)/, 1].length) && (indent - current_todo.indent == 2)
          current_todo << "#{source[(indent + 1)..]}\n"
        else
          todos << current_todo if current_todo
          current_todo = nil
        end
      end

      todos << current_todo if current_todo
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
smart_todo-1.9.0 lib/smart_todo/comment_parser.rb
smart_todo-1.8.0 lib/smart_todo/comment_parser.rb
smart_todo-1.7.0 lib/smart_todo/comment_parser.rb