All files / src/handlers componentMethodsJsDocHandler.js

90.48% Statements 19/21
75% Branches 9/12
100% Functions 5/5
90% Lines 18/20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64                                  6x     6x 6x 7x 5x     6x                 11x 11x       11x 4x 1x     3x   3x 3x 3x 3x     3x               11x    
/*
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @flow
 */
 
import parseJsDoc from '../utils/parseJsDoc';
 
import type Documentation from '../Documentation';
 
// Merges two objects ignoring null/undefined.
function merge(obj1, obj2) {
  Iif (obj1 == null && obj2 == null) {
    return null;
  }
  const merged = {...obj1};
  for (const prop in obj2) {
    if (obj2[prop] != null) {
      merged[prop] = obj2[prop];
    }
  }
  return merged;
}
/**
 * Extract info from the methods jsdoc blocks. Must be run after
 * flowComponentMethodsHandler.
 */
export default function componentMethodsJsDocHandler(
  documentation: Documentation
) {
  let methods = documentation.get('methods');
  Iif (!methods) {
    return;
  }
 
  methods = methods.map(method => {
    if (!method.docblock) {
      return method;
    }
 
    const jsDoc = parseJsDoc(method.docblock);
 
    const returns = merge(jsDoc.returns, method.returns);
    const params = method.params.map(param => {
      const jsDocParam = jsDoc.params.find(p => p.name === param.name);
      return merge(jsDocParam, param);
    });
 
    return {
      ...method,
      description: jsDoc.description || null,
      returns,
      params,
    };
  });
 
  documentation.set('methods', methods);
}