【问题标题】:How to generate JSDoc for `pipe`d ES6 function如何为`pipe`d ES6函数生成JSDoc
【发布时间】:2020-02-09 14:41:11
【问题描述】:

我有一个 ES6 风格的函数,它是使用 asyncPipe 的函数组合定义的。

import { getItemAsync } from 'expo-secure-store';

const asyncPipe = (...fns) => x => fns.reduce(async (y, f) => f(await y), x);

const getToken = () => getItemAsync('token');

const liftedGetToken = async ({ ...rest }) => ({
  token: await getToken(),
  ...rest,
});

const liftedFetch = ({ body, route, token, method = 'GET' } = {}) =>
  fetch(route, {
    ...(body && { body: JSON.stringify(body) }),
    headers: {
      'Content-Type': 'application/json',
      ...(token && { Authorization: `Bearer ${token}` }),
    },
    method,
  });

const json = res => res.json();

/**
 * @method
 * @param {Object} fetchSettings the settings for the fetch request
 * @param {Object} fetchSettings.body the body of the request
 * @param {string} fetchSettings.route the URL of the request
 * @param {string} fetchSettings.method the method of the request
 * @param {string} fetchSettings.token should only be used for testing and unauthenticated requests
 */
const request = asyncPipe(liftedGetToken, liftedFetch, json);

如您所见,我尝试向其添加 JSDoc 描述。但是当我在任何地方使用它时,我的编辑器 VSCode 并没有建议它的参数。您如何使用 JSDoc 声明这些类型的函数?以及如何获取此函数的参数以与 Intellisense 一起使用?

【问题讨论】:

标签: javascript ecmascript-6 functional-programming intellisense jsdoc


【解决方案1】:

VSCode 将尝试在asyncPipe 中显示匿名函数的注释。如果您在其中添加 JSDoc 注释,您可以看到行为:

const asyncPipe = (...fns) =>
  /**
   * My asyncPipe description
   * @param {Object} x Any object
   */
  x => fns.reduce(async (y, f) => f(await y), x);

const request = asyncPipe(liftedGetToken, liftedFetch, json);

不幸的是,在 JSDoc 中无法像您尝试的那样覆盖匿名函数的文档。但是,您可以像这样强制执行 VSCode,请注意这会引入额外的函数调用:

const doRequest = asyncPipe(liftedGetToken, liftedFetch, json);

/**
 * @method
 * @param {Object} fetchSettings the settings for the fetch request
 * @param {Object} fetchSettings.body the body of the request
 * @param {string} fetchSettings.route the URL of the request
 * @param {string} fetchSettings.method the method of the request
 * @param {string} fetchSettings.token should only be used for testing and unauthenticated requests
 */
const request = fetchSettings => doRequest(fetchSettings);

【讨论】:

    【解决方案2】:

    VSCode 在后台使用 TypeScript 引擎,它不擅长从函数组合中推断类型,而且正如您所见,它不能将无点组合识别为函数声明。

    如果你想要类型提示,你可以通过在它周围包裹一个指向函数来指定组合函数的参数。

    我会这样写 - 注意:默认值使 JSDoc 不需要类型提示,但无论如何您可能希望保留 JSDoc 用于描述。还要确保默认值回退导致的失败会产生足够的错误消息。

    /**
      * http request with JSON parsing and token management.
      * @param {Object} fetchSettings the settings for the fetch request
      * @param {Object} fetchSettings.body the body of the request
      * @param {string} fetchSettings.route the URL of the request
      * @param {string} fetchSettings.method the method of the request
      * @param {string} fetchSettings.token should only be used for testing and unauthenticated requests
      */
    const request = ({
      body = {},
      route = '',
      method = 'GET',
      token = ''
    }) => asyncPipe(liftedGetToken, liftedFetch, json)({
      body, route, method, token
    });
    

    【讨论】:

      猜你喜欢
      • 2016-05-12
      • 2012-08-12
      • 2014-02-14
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-09
      • 2014-03-04
      • 2020-08-08
      相关资源
      最近更新 更多