JavaScript Documentation Standards

WordPress follows the JSDoc 3 standard for inline JavaScript documentation.

What Should Be Documented What Should Be Documented

JavaScript documentation in WordPress takes the form of either formatted blocks of documentation or inline comments.

The following is a list of what should be documented in WordPress JavaScript files:

  • Functions and class methods
  • Objects
  • Closures
  • Object properties
  • Requires
  • Events
  • File headers

Top ↑

Documenting Tips Documenting Tips

Language Language

Short descriptions should be clear, simple, and brief. Document “what” and “when” – “why” should rarely need to be included. The “why” can go in the long description if needed. For example:

Functions and closures are third-person singular elements, meaning third-person singular verbs should be used to describe what each does.

Tip: Need help remembering how to conjugate for third-person singular verbs? Imagine prefixing the function, hook, class, or method summary with “It”:

  • Good: “(It) Does something.”
  • Bad: “(It) Do something.”

Functions: What does the function do?

  • Good: Handles a click on X element.
  • Bad: Included for back-compat for X element.

@since: The recommended tool to use when searching for the version something was added to WordPress is svn blame.

If, after using these tools, the version number cannot be determined, use @since Unknown.

Code Refactoring: Do not refactor code in the file when changes to the documentation.

Top ↑

Grammar Grammar

Descriptive elements should be written as complete sentences. The one exception to this standard is file header summaries, which are intended as file “titles” more than sentences.

The serial (Oxford) comma should be used when listing elements in summaries, descriptions, and parameter or return descriptions.

Top ↑

Formatting Guidelines Formatting Guidelines

The following guidelines should be followed to ensure that the content of the doc blocks can be parsed properly for inclusion in the code reference.

Short descriptions:

Short descriptions should be a single sentence and contain no markup of any kind. If the description refers to an HTML element or tag, then it should be written as “link tag”, not “<a>”. For example: “Fires when printing the link tag in the header”.

Long descriptions:

Markdown can be used, if needed, in a long description.

@param and @return tags:

No HTML or markdown is permitted in the descriptions for these tags. HTML elements and tags should be written as “audio element” or “link tag”.

Top ↑

Line wrapping Line wrapping

DocBlock text should wrap to the next line after 80 characters of text. If the DocBlock itself is indented on the left 20 character positions, the wrap could occur at position 100, but should not extend beyond a total of 120 characters wide.

Top ↑

Aligning comments Aligning comments

Related comments should be spaced so that they align to make them more easily readable.

For example:

/**
 * @param {very_long_type} name           Description.
 * @param {type}           very_long_name Description.
 */

Top ↑

Functions Functions

Functions should be formatted as follows:

  • Summary: A brief, one line explanation of the purpose of the function. Use a period at the end.
  • Description: A supplement to the summary, providing a more detailed description. Use a period at the end.
  • @deprecated x.x.x: Only use for deprecated functions, and provide the version the function was deprecated which should always be 3-digit (e.g. @since 3.6.0), and the function to use instead.
  • @since x.x.x: Should always be 3-digit (e.g. @since 3.6.0).
  • @access: Only use for functions if private. If the function is private, it is intended for internal use only, and there will be no documentation for it in the code reference.
  • @class: Use for class constructors.
  • @augments: For class constuctors, list direct parents.
  • @mixes: List mixins that are mixed into the object.
  • @alias: If this function is first assigned to a temporary variable this allows you to change the name it’s documented under.
  • @memberof: Namespace that this function is contained within if JSDoc is unable to resolve this automatically.
  • @static: For classes, used to mark that a function is a static method on the class constructor.
  • @see: A function or class relied on.
  • @link: URL that provides more information.
  • @fires: Event fired by the function. Events tied to a specific class should list the class name.
  • @listens: Events this function listens for. An event must be prefixed with the event namespace. Events tied to a specific class should list the class name.
  • @global: Marks this function as a global function to be included in the global namespace.
  • @param: Give a brief description of the variable; denote particulars (e.g. if the variable is optional, its default) with JSDoc @param syntax. Use a period at the end.
  • @yield: For generator functions, a description of the values expected to be yielded from this function. As with other descriptions, include a period at the end.
  • @return: Note the period after the description.
/**
 * Summary. (use period)
 *
 * Description. (use period)
 *
 * @since      x.x.x
 * @deprecated x.x.x Use new_function_name() instead.
 * @access     private
 *
 * @class
 * @augments parent
 * @mixes    mixin
 *
 * @alias    realName
 * @memberof namespace
 *
 * @see  Function/class relied on
 * @link URL
 * @global
 *
 * @fires   eventName
 * @fires   className#eventName
 * @listens event:eventName
 * @listens className~event:eventName
 *
 * @param {type}   var           Description.
 * @param {type}   [var]         Description of optional variable.
 * @param {type}   [var=default] Description of optional variable with default variable.
 * @param {Object} objectVar     Description.
 * @param {type}   objectVar.key Description of a key in the objectVar parameter.
 *
 * @yield {type} Yielded value description.
 *
 * @return {type} Return value description.
 */

Top ↑

Backbone classes Backbone classes

Backbone’s extend calls should be formatted as follows:

  • @lends This tag will allow JSDoc to recognize the extend function from Backbone as a class definition. This should be placed right before the Object that contains the class definition.

Backbone’s initialize functions should be formatted as follows:

  • Summary: A brief, one line explanation of the purpose of the function. Use a period at the end.
  • Description: A supplement to the summary, providing a more detailed description. Use a period at the end.
  • @deprecated x.x.x: Only use for deprecated classes, and provide the version the class was deprecated which should always be 3-digit (e.g. @since 3.6.0), and the class to use instead.
  • @since x.x.x: Should always be 3-digit (e.g. @since 3.6.0).
  • @constructs Marks this function as the constructor of this class.
  • @augments: List direct parents.
  • @mixes: List mixins that are mixed into the class.
  • @requires: Lists modules that this class requires. Multiple @requires tags can be used.
  • @alias: If this class is first assigned to a temporary variable this allows you to change the name it’s documented under.
  • @memberof: Namespace that this class is contained within if JSDoc is unable to resolve this automatically.
  • @static: For classes, used to mark that a function is a static method on the class constructor.
  • @see: A function or class relied on.
  • @link: URL that provides more information.
  • @fires: Event fired by the constructor. Should list the class name.
  • @param: Document the arguments passed to the constructor even if not explicitly listed in initialize. Use a period at the end.
    • Backbone Models are passed attributes and options parameters.
    • Backbone Views are passed an options parameter.
Class = Parent.extend( /** @lends namespace.Class.prototype */{
    /**
     * Summary. (use period)
     *
     * Description. (use period)
     *
     * @since      x.x.x
     * @deprecated x.x.x Use new_function_name() instead.
     * @access     private
     *
     * @constructs namespace.Class
     * @augments   Parent
     * @mixes      mixin
     *
     * @alias    realName
     * @memberof namespace
     *
     * @see   Function/class relied on
     * @link  URL
     * @fires Class#eventName
     *
     * @param {Object} attributes     The model's attributes.
     * @param {type}   attributes.key One of the model's attributes.
     * @param {Object} [options]      The model's options.
     * @param {type}   attributes.key One of the model's options.
     */
    initialize: function() {
        //Do stuff.
    }
} );

If a Backbone class does not have an initialize function it should be documented by using @inheritDoc as follows:

/**
 * Summary. (use period)
 *
 * Description. (use period)
 *
 * @since      x.x.x
 * @deprecated x.x.x Use new_function_name() instead.
 * @access     private
 *
 * @constructs namespace.Class
 * @memberOf   namespace
 * @augments   Parent
 * @mixes      mixin
 * @inheritDoc
 *
 * @alias    realName
 * @memberof namespace
 *
 * @see   Function/class relied on
 * @link  URL
 */
Class = Parent.extend( /** @lends namespace.Class.prototype */{
 // Functions and properties.
} );

Note: This currently doesn’t provide the expected functionality due to a bug with JSDoc’s inheritDoc tag. See the issue here

Top ↑

Local functions Local functions

At times functions will be assigned to a local variable before being assigned as a class member.

Such functions should be marked as inner functions of the namespace that uses them using ~.

The functions should be formatted as follows:

/**
 * Function description, you can use any JSDoc here as long as the function remains private.
 *
 * @alias namespace~doStuff
 */
var doStuff = function () {
 // Do stuff.
};

Class = Parent.extend( /** @lends namespace.Class.prototype */{
    /**
     * Class description
     *
     * @constructs namespace.Class
     *
     * @borrows namespace~doStuff as prototype.doStuff
     */
    initialize: function() {
        //Do stuff.
    },

    /*
     * This function will automatically have it's documentation copied from above.
     * You should make a comment ( not a DocBlock using /**, instead use /* or // )
     * noting that you're describing this function using @borrows.
     */
    doStuff: doStuff,
} );

Top ↑

Local ancestors Local ancestors

At times classes will have Ancestors that are only assigned to a local variable. Such classes should be assigned to the namespace their children are and be made inner classes using ~.

These should be documented as follows:

Top ↑

Class members Class members

Class members should be formatted as follows:

  • Short description: Use a period at the end.
  • @since x.x.x: Should always be 3-digit (e.g. @since 3.6.0).
  • @access: If the members is private, protected or public. Private members are intended for internal use only.
  • @type: List the type of the class member.
  • @property List all properties this object has in case it’s an Object. Use a period at the end.
  • @member: Optionally use this to override JSDoc’s member detection in place of @type to force a class member.
  • @memberof: Optionally use this to override what class this is a member of.
/**
 * Short description. (use period)
 *
 * @since  x.x.x
 * @access (private, protected, or public)
 *
 * @type     {type}
 * @property {type} key Description.
 *
 * @member   {type} realName
 * @memberof className
 */

Top ↑

Namespaces Namespaces

Namespaces should be formatted as follows:

  • Short description: Use a period at the end.
  • @namespace: Marks this symbol as a namespace, optionally provide a name as an override.
  • @since x.x.x: Should always be 3-digit (e.g. @since 3.6.0).
  • @memberof: Namespace that this namespace is contained in.
  • @property: Properties that this namespace exposes. Use a period at the end.
/**
 * Short description. (use period)
 *
 * @namespace realName
 * @memberof  parentNamespace
 *
 * @since x.x.x
 *
 * @property {type} key Description.
 */

Top ↑

Inline Comments Inline Comments

Inline comments inside methods and functions should be formatted as follows:

Top ↑

Single line comments Single line comments

// Extract the array values.

Top ↑

Multi-line comments Multi-line comments

/*
 * This is a comment that is long enough to warrant being stretched over
 * the span of multiple lines. You'll notice this follows basically
 * the same format as the JSDoc wrapping and comment block style.
 */

Important note: Multi-line comments must not begin with /** (double asterisk). Use /* (single asterisk) instead.

Top ↑

File Headers File Headers

The JSDoc file header block is used to give an overview of what is contained in the file.

Whenever possible, all WordPress JavaScript files should contain a header block.

WordPress uses JSHint for general code quality testing. Any inline configuration options should be placed at the end of the header block.

/**
 * Summary. (use period)
 *
 * Description. (use period)
 *
 * @link   URL
 * @file   This files defines the MyClass class.
 * @author AuthorName.
 * @since  x.x.x
 */

/** jshint {inline configuration here} */

Top ↑

Supported JSDoc Tags Supported JSDoc Tags

TagDescription
@abstractThis method can be implemented (or overridden) by the inheritor.
@accessSpecify the access level of this member (private, public, or protected).
@aliasTreat a member as if it had a different name.
@augmentsThis class inherits from a parent class.
@authorIdentify the author of an item.
@borrowsThis object uses something from another object.
@callbackDocument a callback function.
@classThis function is a class constructor.
@classdescUse the following text to describe the entire class.
@constantDocument an object as a constant.
@constructsThis function member will be the constructor for the previous class.
@copyrightDocument some copyright information.
@defaultDocument the default value.
@deprecatedDocument that this is no longer the preferred way.
@descriptionDescribe a symbol.
@enumDocument a collection of related properties.
@eventDocument an event.
@exampleProvide an example of how to use a documented item.
@exportsIdentify the member that is exported by a JavaScript module.
@externalDocument an external class/namespace/module.
@fileDescribe a file.
@firesDescribe the events this method may fire.
@functionDescribe a function or method.
@globalDocument a global object.
@ignore[todo] Remove this from the final output.
@innerDocument an inner object.
@instanceDocument an instance member.
@kindWhat kind of symbol is this?
@lendsDocument properties on an object literal as if they belonged to a symbol with a given name.
@license[todo] Document the software license that applies to this code.
@linkInline tag – create a link.
@memberDocument a member.
@memberofThis symbol belongs to a parent symbol.
@mixesThis object mixes in all the members from another object.
@mixinDocument a mixin object.
@moduleDocument a JavaScript module.
@nameDocument the name of an object.
@namespaceDocument a namespace object.
@paramDocument the parameter to a function.
@privateThis symbol is meant to be private.
@propertyDocument a property of an object.
@protectedThis member is meant to be protected.
@publicThis symbol is meant to be public.
@readonlyThis symbol is meant to be read-only.
@requiresThis file requires a JavaScript module.
@returnDocument the return value of a function.
@seeRefer to some other documentation for more information.
@sinceWhen was this feature added?
@staticDocument a static member.
@thisWhat does the ‘this’ keyword refer to here?
@throwsDescribe what errors could be thrown.
@todoDocument tasks to be completed.
@tutorialInsert a link to an included tutorial file.
@typeDocument the type of an object.
@typedefDocument a custom type.
@variationDistinguish different objects with the same name.
@versionDocuments the version number of an item.
@yieldDocument the yielded values of a generator function.

Top ↑

Unsupported JSDoc Tags Unsupported JSDoc Tags

TagWhy it’s not supported
@summaryShould not be used. See the example of how to separate a summary from the full description.
@virtualAn unsupported synonym. Use @abstract instead.
@extendsAn unsupported synonym. Use @augments instead.
@constructorAn unsupported synonym. Use @class instead.
@constAn unsupported synonym. Use @constant instead.
@defaultvalueAn unsupported synonym. Use @default instead.
@descAn unsupported synonym. Use @description instead.
@hostAn unsupported synonym. Use @external instead.
@fileoverviewAn unsupported synonym. Use @file instead.
@overviewAn unsupported synonym. Use @file instead.
@emitsAn unsupported synonym. Use @fires instead.
@funcAn unsupported synonym. Use @function instead.
@methodAn unsupported synonym. Use @function instead.
@varAn unsupported synonym. Use @member instead.
@emitsAn unsupported synonym. Use @fires instead.
@argAn unsupported synonym. Use @param instead.
@argumentAn unsupported synonym. Use @param instead.
@propAn unsupported synonym. Use @property instead.
@returnsAn unsupported synonym. Use @return instead.
@exceptionAn unsupported synonym. Use @throws instead.