Skip to content

Custom member types

Rene Saarsoo edited this page Feb 21, 2013 · 7 revisions

There are two broad categories of member types we can create:

  • :method_like - such members have parameters and possibly a return value. Things like methods and events.

  • :property_like - such members have a type and possibly some kind of (default) value. Things like properties and configs.

Method-like members are way easier to implement, so we start with them.

Method-like members

Lets implement a @hook tag to describe a special class of functions that will provide a way to hook into the internals of a class and extend it. For example:

/**
 * @hook before
 * Runs before the component is initialized.
 * @param {Object} self The component itself.
 * @param {Object} cfg Config options passed in constructor.
 */

An implementation for this is quite simple:

require "jsduck/tag/tag"

class Hook < JsDuck::Tag::Tag
  def initialize
    @tagname = :hook
    @pattern = "hook"
    @member_type = {
      :name => :hook,
      :category => :method_like,
      :title => "Hooks",
      :position => MEMBER_POS_EVENT + 0.1,
    }
  end

  def parse_doc(scanner)
    return {
      :tagname => :hook,
      :name => scanner.ident,
    }
  end

  def process_doc(context, hook_tags, position)
    context[:name] = hook_tags[0][:name]
  end

end

The thing that turns this tag into a new member type is the setting of @member_type variable. There we give a :name to our new type, specify our chosen :category, and set a few rendering options.

:position defines the ordering of member sections in the final page. Here we position hooks right after events. The title is shown at the top of each members section and is also used as a title for toolbar button.

parse_doc and process_doc are fairly trivial. We parse the name of the hook using builtin scanner.ident method, and assign the value to :name field in context hash. The :name field is a special field that every member type must have - it gets further processed internally for JSDuck.

Property-like members

Let's define a @constant tag. It should work pretty much like @property, but with the semantic difference of documenting unchangable values. For example:

/**
 * @constant
 * Acceleration of objects in Earth's gravitational field.
 */
var ACCELERATION = 9.80665;
Clone this wiki locally