Sha256: e35e945fb39a592f5046478a2874deec5188cea5836d3325ea50a9d76f5b9e19

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

require "ripper"

module SmartTodo
  module Parser
    # This class is used to parse Ruby code and will stop each time
    # a Ruby comment is encountered. It will detect if a TODO comment
    # is a Smart Todo and will gather the comments associated to the TODO.
    class CommentParser < Ripper::Filter
      def initialize(*)
        super
        @node = nil
      end

      # @param comment [String] the actual Ruby comment
      # @param data [Array<TodoNode>]
      # @return [Array<TodoNode>]
      def on_comment(comment, data)
        if todo_metadata?(comment)
          append_existing_node(data)
          @node = TodoNode.new(comment)
        elsif todo_comment?(comment)
          @node << comment
        else
          append_existing_node(data)
          @node = nil
        end

        data
      end

      # @param init [Array]
      # @return [Array<TodoNode>]
      def parse(init = [])
        super(init)

        init.tap { append_existing_node(init) }
      end

      private

      # @param comment [String] the actual Ruby comment
      # @return [nil, Integer]
      def todo_metadata?(comment)
        /^#\sTODO\(/ =~ comment
      end

      # Check if the comment is associated with the Smart Todo
      # @param comment [String] the actual Ruby comment
      # @return [true, false]
      #
      # @example When a comment is associated to a SmartTodo
      #   TODO(on_date(...), to: '...')
      #     This is an associated comment
      #
      # @example When a comment is not associated to a SmartTodo
      #   TODO(on_date(...), to: '...')
      #   This is an associated comment (Note the indentation)
      def todo_comment?(comment)
        @node&.indented_comment?(comment)
      end

      # @param data [Array<TodoNode>]
      # @return [Array<TodoNode>]
      def append_existing_node(data)
        data << @node if @node
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
smart_todo-1.6.0 lib/smart_todo/parser/comment_parser.rb
smart_todo-1.5.0 lib/smart_todo/parser/comment_parser.rb
smart_todo-1.4.3 lib/smart_todo/parser/comment_parser.rb
smart_todo-1.3.1 lib/smart_todo/parser/comment_parser.rb
smart_todo-1.3.0 lib/smart_todo/parser/comment_parser.rb