JSDoc包含来自另一个@typedef对象的所有属性

时间:2017-02-08 21:32:01

标签: javascript jsdoc

如果我使用JSDoc创建两个对象,其中一个具有另一个的所有属性,我该如何显示?

例如你有:

/** 
 * @typdef Foo
 * @type {Object}
 * @property {string} bar
 * @property {string} baz
 */

/**
 * @typedef Foobar
 * @type {Object}
 * @property {float} value
 * @property {string} bar
 * @property {string} baz
 */

但我不想两次写出bar / baz,我只想继承它。

2 个答案:

答案 0 :(得分:2)

我相信你会这样做......

/** Type definition for Foo.
 * @typdef {Object} Foo
 * @property {string} bar
 * @property {string} baz
 */

/** Type definition for Foobar.
 * @typedef {Foo} Foobar
 * @property {float} value
 */

请注意Foobar Foo是如何“继承”的。{/ p>

答案 1 :(得分:0)

您可以制作一个简单的jsdoc插件,以便可以在@augments上使用@extends / typedef。例如,可以在here(如下所示)中找到这样的插件:

/**
 * Define a jsdoc plugin to update typedefs that use augments.
 */
exports.handlers = {
  /**
   * Modify typedefs that use augments (extends).  Add the base typedef's
   * properties to the augmented typedefs.
   */
  parseComplete: function (e) {
    var typedefs = {},
        augmentedTypedefs = {},
        numAugmented = 0;
    /* Make a dictionary of all known typedefs and a dictionary of augmented
     * typedefs */
    e.doclets.forEach(function (doclet) {
      if (doclet.kind === 'typedef') {
        typedefs[doclet.longname] = doclet;
        if (doclet.augments && doclet.augments.length) {
          augmentedTypedefs[doclet.longname] = doclet;
        }
      }
    });
    while (Object.keys(augmentedTypedefs).length !== numAugmented) {
      numAugmented = Object.keys(augmentedTypedefs).length;
      Object.keys(augmentedTypedefs).forEach(function (name) {
        var doclet = augmentedTypedefs[name];
        /* If this typedef is augmented by an augmented typedef, skip it for
         * now.  Ignore self references */
        if (doclet.augments.some(function (augmentName) {
          return augmentName !== name && augmentedTypedefs[augmentName];
        })) {
          return;
        }
        /* Ensure we have properties */
        doclet.properties = doclet.properties || [];
        /* Make a dictionary so we don't clobber known properties. */
        var properties = {};
        doclet.properties.forEach(function (prop) {
          properties[prop.name] = prop;
        });
        /* For each augment base, add its properties if we don't already have
         * them.  If the typedef augments two other typedefs that each have a
         * property of the same name, the last listed will be shown (done by
         * reversing the augments list). */
        doclet.augments.slice().reverse().forEach(function (augmentName) {
          if (augmentName !== name && typedefs[augmentName] && typedefs[augmentName].properties) {
            typedefs[augmentName].properties.forEach(function (prop) {
              if (!properties[prop.name]) {
                /* Make a copy so we don't mutate the original property. */
                prop = Object.assign(prop);
                /* Add a value that a rendering template could use to show that
                 * the property was inherted from a parent.  Since that in turn
                 * could have been inherited, preserve a known value. */
                prop.inherited = prop.inherited || augmentName;
                /* Add the property to the typedef and to the list of known
                 * properties. */
                doclet.properties.push(prop);
                properties[prop.name] = prop;
              }
            });
          }
        });
        /* We've finished processing this typedef, so remove it from the
         * augmented list. */
        delete augmentedTypedefs[name];
      });
    }
  }
};

此时,您可以执行以下操作:

/** 
 * @typdef Foo
 * @type {Object}
 * @property {string} bar
 * @property {string} baz
 */

/**
 * @typedef Foobar
 * @type {Object}
 * @augments Foo
 * @property {float} value
 */

如果需要(see an example),可以修改模板以显示继承属性和非继承属性: