Commit 376a6ec7 authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

Build doc_map just once.

Extract DocMap class for building map of @tags in comments.  Run
the map building just once inside FileParser - instead of repeating
this thing in DocType and DocAst.

No real speed advantage, mainly removal of duplication.
parent f2bf6c67
Loading
Loading
Loading
Loading
+1 −16
Original line number Diff line number Diff line
@@ -16,9 +16,7 @@ module JsDuck

    # Given tagname and array of tags from DocParser, produces docs
    # of the type determined by tagname.
    def detect(tagname, docs)
      doc_map = build_doc_map(docs)

    def detect(tagname, docs, doc_map)
      case tagname
      when :class
        create_class(docs, doc_map)
@@ -39,19 +37,6 @@ module JsDuck

    private

    # Build map of at-tags for quick lookup
    def build_doc_map(docs)
      map = {}
      docs.each do |tag|
        if map[tag[:tagname]]
          map[tag[:tagname]] << tag
        else
          map[tag[:tagname]] = [tag]
        end
      end
      map
    end

    def create_class(docs, doc_map)
      return add_shared({
        :tagname => :class,

lib/jsduck/doc_map.rb

0 → 100644
+21 −0
Original line number Diff line number Diff line
module JsDuck

  # Helper for building at-tags lookup table.
  class DocMap

    # Builds map of at-tags for quick lookup
    def self.build(docs)
      map = {}
      docs.each do |tag|
        if map[tag[:tagname]]
          map[tag[:tagname]] << tag
        else
          map[tag[:tagname]] = [tag]
        end
      end
      map
    end

  end

end
+2 −19
Original line number Diff line number Diff line
@@ -5,13 +5,11 @@ module JsDuck
    # Given parsed documentation and code, returns the tagname for
    # documentation item.
    #
    # @param docs Result from DocParser
    # @param doc_map Result from DocParser turned into hash of tags.
    # @param code Result from Ast#detect or CssParser#parse
    # @returns One of: :class, :method, :event, :cfg, :property, :css_var, :css_mixin
    #
    def detect(docs, code)
      doc_map = build_doc_map(docs)

    def self.detect(doc_map, code)
      if doc_map[:class] || doc_map[:override]
        :class
      elsif doc_map[:event]
@@ -39,21 +37,6 @@ module JsDuck
        code[:tagname]
      end
    end

    private

    # Build map of at-tags for quick lookup
    def build_doc_map(docs)
      map = {}
      docs.each do |tag|
        if map[tag[:tagname]]
          map[tag[:tagname]] << tag
        else
          map[tag[:tagname]] = [tag]
        end
      end
      map
    end
  end

end
+10 −4
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ require 'jsduck/doc_parser'
require 'jsduck/merger'
require 'jsduck/ast'
require 'jsduck/doc_type'
require 'jsduck/doc_map'
require 'jsduck/doc_ast'
require 'jsduck/class_doc_expander'

@@ -17,7 +18,6 @@ module JsDuck
    class FileParser

      def initialize
        @doc_type = DocType.new
        @doc_parser = DocParser.new
        @class_doc_expander = ClassDocExpander.new
        @doc_ast = DocAst.new
@@ -50,10 +50,15 @@ module JsDuck
      # Parses the docs, detects tagname and expands class docset
      def expand(docset)
        docset[:comment] = @doc_parser.parse(docset[:comment], @doc_ast.filename, docset[:linenr])
        docset[:tagname] = @doc_type.detect(docset[:comment], docset[:code])
        docset[:doc_map] = DocMap.build(docset[:comment])
        docset[:tagname] = DocType.detect(docset[:doc_map], docset[:code])

        if docset[:tagname] == :class
          @class_doc_expander.expand(docset)
          # expand class into several docsets, and rebuild doc-maps for all of them.
          @class_doc_expander.expand(docset).map do |ds|
            ds[:doc_map] = DocMap.build(ds[:comment])
            ds
          end
        else
          docset
        end
@@ -62,7 +67,8 @@ module JsDuck
      # Merges comment and code parts of docset
      def merge(docset)
        @doc_ast.linenr = docset[:linenr]
        docset[:comment] = @doc_ast.detect(docset[:tagname], docset[:comment])
        docset[:comment] = @doc_ast.detect(docset[:tagname], docset[:comment], docset[:doc_map])
        docset.delete(:doc_map)

        @merger.merge(docset)
      end
+3 −1
Original line number Diff line number Diff line
require "jsduck/ast"
require "jsduck/doc_type"
require "jsduck/doc_map"
require "jsduck/js_parser"
require "jsduck/css_parser"
require "jsduck/doc_parser"
@@ -15,7 +16,8 @@ describe JsDuck::DocType do

    doc_parser = JsDuck::DocParser.new
    node[:comment] = doc_parser.parse(node[:comment])
    return JsDuck::DocType.new.detect(node[:comment], node[:code])
    node[:doc_map] = JsDuck::DocMap.build(node[:comment])
    return JsDuck::DocType.detect(node[:doc_map], node[:code])
  end

  describe "detects as class" do