WordPress 编码标准文档

title: "JavaScript 文档标准" post_status: publish comment_status: open taxonomy: category: - wpcs-docs post_tag: - Inline Documentation Standards - Repos - Data


JavaScript 文档标准

WordPress 遵循 JSDoc 3 标准 进行内联 JavaScript 文档编写。

需要记录的内容

WordPress 中的 JavaScript 文档采用格式化文档块或内联注释的形式。

以下是 WordPress JavaScript 文件中应记录的内容列表:

文档编写技巧

语言规范

简短描述应清晰、简洁、扼要。主要记录"是什么"和"何时"——通常无需包含"为什么"。如有必要,"为什么"可放在详细描述中。例如:

函数和闭包属于第三人称单数元素,这意味着应使用第三人称单数动词来描述它们的功能。

[tip] 需要帮助记忆第三人称单数动词的变位规则吗?可以想象在函数、钩子、类或方法摘要前加上"它":

[/tip]

函数说明:函数的功能是什么?

@since 标签:在 WordPress 中查找功能添加版本时,推荐使用 svn blame 工具。

如果使用这些工具后仍无法确定版本号,请使用 @since Unknown

代码重构:修改文档时请勿重构文件中的代码。

语法规范

描述性内容应使用完整句子撰写。此标准唯一的例外是文件头摘要,其定位更接近于文件"标题"而非完整句子。

在摘要、描述、参数或返回值说明中列举多个元素时,应使用序列(牛津)逗号。

格式规范

为确保文档块内容能被正确解析并纳入代码参考,请遵循以下规范。

简短描述

简短描述应为单句,且不包含任何标记。若描述涉及 HTML 元素或标签,应写作“链接标签”而非“<a>”。例如:“在页眉中打印链接标签时触发”。

详细描述

如有需要,可在详细描述中使用 Markdown 格式。

@param@return 标签

这些标签的描述中不允许使用 HTML 或 Markdown。HTML 元素和标签应写作“音频元素”或“链接标签”。

行换行

文档块文本应在超过 80 个字符后换行。如果文档块本身在左侧缩进 20 个字符位置,换行可能发生在第 100 个字符位置,但总宽度不应超过 120 个字符。

对齐注释

相关注释应通过间距调整实现对齐,以提升可读性。

例如:

/**
 * @param {very_long_type} name           描述。
 * @param {type}           very_long_name 描述。
 */

函数

函数应按以下格式编写:

/**
 * 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.
 */

Backbone classes

Backbone's extend calls should be formatted as follows:

Backbone's initialize functions should be formatted as follows:

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}   options.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 JSDocs3 issue 1012.

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,
} );

本地祖先类

有时类会拥有仅被赋值给局部变量的祖先类。这类祖先类应被分配到其子类所在的命名空间,并使用 ~ 符号设为内部类。

类成员

类成员应按以下格式编写:

/**
 * 简短描述。(使用句号)
 *
 * @since  x.x.x
 * @access (private, protected, or public)
 *
 * @type     {type}
 * @property {type} key 描述。
 *
 * @member   {type} realName
 * @memberof className
 */

命名空间

命名空间应按以下格式编写:

/**
 * 简短描述。(使用句号)
 *
 * @namespace realName
 * @memberof  parentNamespace
 *
 * @since x.x.x
 *
 * @property {type} key 描述。
 */

行内注释

方法或函数内部的行内注释应遵循以下格式:

单行注释

// 提取数组的值。

多行注释

/*
 * 这是一个足够长的注释,需要跨越多行。
 * 您会注意到这基本上遵循与 JSDoc 包装和注释块样式相同的格式。
 */

重要提示: 多行注释不得以 /**(双星号)开头。请改用 /*(单星号)。

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} */

支持的 JSDoc 标签

Tag Description
@abstract This method can be implemented (or overridden) by the inheritor.
@access Specify the access level of this member (private, public, or protected).
@alias Treat a member as if it had a different name.
@augments This class inherits from a parent class.
@author Identify the author of an item.
@borrows This object uses something from another object.
@callback Document a callback function.
@class This function is a class constructor.
@classdesc Use the following text to describe the entire class.
@constant Document an object as a constant.
@constructs This function member will be the constructor for the previous class.
@copyright Document some copyright information.
@default Document the default value.
@deprecated Document that this is no longer the preferred way.
@description Describe a symbol.
@enum Document a collection of related properties.
@event Document an event.
@example Provide an example of how to use a documented item.
@exports Identify the member that is exported by a JavaScript module.
@external Document an external class/namespace/module.
@file Describe a file.
@fires Describe the events this method may fire.
@function Describe a function or method.
@global Document a global object.
@ignore [todo] Remove this from the final output.
@inner Document an inner object.
@instance Document an instance member.
@kind What kind of symbol is this?
@lends Document 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.
@link Inline tag - create a link.
@member Document a member.
@memberof This symbol belongs to a parent symbol.
@mixes This object mixes in all the members from another object.
@mixin Document a mixin object.
@module Document a JavaScript module.
@name Document the name of an object.
@namespace Document a namespace object.
@param Document the parameter to a function.
@private This symbol is meant to be private.
@property Document a property of an object.
@protected This member is meant to be protected.
@public This symbol is meant to be public.
@readonly This symbol is meant to be read-only.
@requires This file requires a JavaScript module.
@return Document the return value of a function.
@see Refer to some other documentation for more information.
@since Documents the version at which the function was added, or when significant changes are made.
@static Document a static member.
@this What does the 'this' keyword refer to here?
@throws Describe what errors could be thrown.
@todo Document tasks to be completed.
@tutorial Insert a link to an included tutorial file.
@type Document the type of an object.
@typedef Document a custom type.
@variation Distinguish different objects with the same name.
@version Documents the version number of an item.
@yield Document the yielded values of a generator function.

Unsupported JSDoc Tags

Tag Why it's not supported
@summary Should not be used. See the example of how to separate a summary from the full description.
@virtual An unsupported synonym. Use @abstract instead.
@extends An unsupported synonym. Use @augments instead.
@constructor An unsupported synonym. Use @class instead.
@const An unsupported synonym. Use @constant instead.
@defaultvalue An unsupported synonym. Use @default instead.
@desc An unsupported synonym. Use @description instead.
@host An unsupported synonym. Use @external instead.
@fileoverview An unsupported synonym. Use @file instead.
@overview An unsupported synonym. Use @file instead.
@emits An unsupported synonym. Use @fires instead.
@func An unsupported synonym. Use @function instead.
@method An unsupported synonym. Use @function instead.
@var An unsupported synonym. Use @member instead.
@emits An unsupported synonym. Use @fires instead.
@arg An unsupported synonym. Use @param instead.
@argument An unsupported synonym. Use @param instead.
@prop An unsupported synonym. Use @property instead.
@returns An unsupported synonym. Use @return instead.
@exception An unsupported synonym. Use @throws instead.