Class: JsDuck::JsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/jsduck/js_parser.rb

Overview

JavaScript parser that internally uses Esprima.js

Instance Method Summary collapse

Constructor Details

#initialize(input, options = {}) ⇒ JsParser

Initializes the parser with JavaScript source code to be parsed.



10
11
12
13
14
15
16
# File 'lib/jsduck/js_parser.rb', line 10

def initialize(input, options = {})
  @input = input

  # Initialize line number counting
  @start_index = 0
  @start_linenr = 1
end

Instance Method Details

#parseObject

Parses JavaScript source code and returns array of hashes like this:

{
    :comment => "The contents of the comment",
    :code => {...AST data structure for code following the comment...},
    :linenr => 12,  // Beginning with 1
    :type => :doc_comment, // or :plain_comment
}


27
28
29
30
31
32
# File 'lib/jsduck/js_parser.rb', line 27

def parse
  @ast = Esprima.parse(@input)

  @ast["comments"] = merge_comments(@ast["comments"])
  locate_comments
end