Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 238 lines (187 sloc) 5.355 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
require "parsejs/visitor"
require "yard"

module ParseJS
  class CommentScanner < Visitor
    include ParseJS::AST

    def initialize(*)
      @current_comment = nil
      super
    end

    def comment?
      return false unless @current_comment
      return false if @current_comment.empty?
      return @current_comment.any?(&:multiline?)
    end

    def stringify(node)
      Stringifier.to_string(Marshal.load(Marshal.dump(node)))
    end

    def visit_AssignmentExpression(expr)
      process_comment(expr)
      super
    end

    def visit_CommentedStatement(comment)
      @current_comment = comment.comments

      super
    end

    def visit_Property(property)
      @current_comment = property.comments
      process_comment(property)

      super
    end

    def process_comment(node)
      print_comment(node)
    end

    def print_comment(node)
      return unless comment?

      puts
      puts "-" * 10 + node.class.name + "-" * 10
      puts @current_comment.map { |c| next unless c.multiline?; c.body }.join("\n")
      puts "-" * (10 + node.class.name.size + 10)
      puts

      puts Stringifier.to_string(Marshal.load(Marshal.dump(node)))
      puts
    ensure
      @current_comment = nil
    end

    def strip_leading_whitespace(string)
      min = string.scan(/^[ \t]*(?=\S)/).min
      size = min ? min.size : 0
      string.gsub(/^[ \t]{#{size}}/, '')
    end
  end

  class ExtractDocs < CommentScanner
    include AST

    def initialize(*)
      @current_variables = []
      @current_class = []
    end

    def visit_Program(program)
      with_variables(program, []) { super }
    end

    def visit_FunctionDeclaration(decl)
      with_variables(decl, decl.params.list.map(&:val)) { super }
    end

    def visit_FunctionExpression(expr)
      with_variables(expr, expr.params.list.map(&:val)) { super }
    end

    def with_variables(expr, params=expr.params.map(&:val))
      locals = FindVars.find_variables(expr)
      @current_variables.push(locals | params)
      yield
    ensure
      @current_variables.pop
    end

    def variable?(name)
      @current_variables.find do |list|
        list.include?(name)
      end
    end

    def member_left(expr)
      object = expr

      while object
        if object.is_a?(Identifier)
          return object.val
        elsif object.is_a?(ThisExpression)
          return "this"
        elsif object.respond_to?(:object)
          object = object.object
        else
          return nil
        end
      end
    end

    def visit_AssignmentExpression(expr)
      right = expr.right
      in_class = false

      if right.is_a?(CallExpression)
        callee = expr.right.callee
        if callee.is_a?(MemberExpression) && !callee.computed
          property = callee.property.val
          if property == "extend"
            if expr.left.is_a?(MemberExpression)
              left_id = member_left(expr.left)
              extends = stringify(expr.right.callee).sub(/\.extend$/, '')

              if variable?(left_id)
                # found private class
              else
                klass = stringify(expr.left)

                left_string = stringify(expr.left)
                namespace_obj = build_namespace left_string.split(".")[0...-1].join("::")

                class_name = left_string.split(".")[-1]

                obj = YARD::CodeObjects::ClassObject.new(namespace_obj, class_name)
                obj.docstring = stripped_comment

                @current_class.push [klass, extends, obj]
                in_class = true
              end
            end
          end
        end
      end

      super

    ensure
      @current_class.pop if in_class
    end

    def build_namespace(namespace)
      if namespace.empty?
        YARD::Registry.root
      elsif ns = YARD::Registry.at(namespace)
        ns
      else
        name = namespace.gsub('::', '.')
        YARD::CodeObjects::NamespaceObject.new(:root, name)
      end
    end

    def current_class_name
      klass, parent = @current_class.last
      "#{klass} < #{parent}"
    end

    def current_yard_class
      @current_class.last[2]
    end

    def visit_Property(property)
      return if @current_class.empty?

      @current_comment = property.comments
      return if stripped_comment.empty?

      case property.value
      when FunctionDeclaration, FunctionExpression
        obj = YARD::CodeObjects::MethodObject.new(current_yard_class, property.key.val)
        obj.docstring = stripped_comment
      else
        # found a non-method property
      end

      super
    end

    def stripped_comment
      comments = @current_comment.map do |c|
        next unless c.multiline?
        c.body
      end

      string = comments.join("\n")
      string = string.gsub(/\A\**\n/, '')
      strip_leading_whitespace(string)
    end

    def process_comment(*)
    end
  end

  require "set"

  class FindVars < Visitor
    def self.find_variables(ast)
      ast = Marshal.load(Marshal.dump(ast))

      finder = new
      finder.accept(ast)
      finder.variables
    end

    attr_reader :variables

    def initialize(*)
      @variables = Set.new
      super
    end

    def visit_VariableDeclarator(expr)
      @variables << expr.id.val
    end

    def visit_FunctionDeclaration(*)
    end

    def visit_FunctionExpression(*)
    end
  end
end
Something went wrong with that request. Please try again.