"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _iterateJsdoc = _interopRequireDefault(require("../iterateJsdoc"));

var _warnRemovedSettings = _interopRequireDefault(require("../warnRemovedSettings"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/**
 * We can skip checking for a return value, in case the documentation is inherited
 * or the method is either a constructor or an abstract method.
 *
 * In either of these cases the return value is optional or not defined.
 *
 * @param {*} utils
 *   a reference to the utils which are used to probe if a tag is present or not.
 * @returns {boolean}
 *   true in case deep checking can be skipped; otherwise false.
 */
const canSkip = utils => {
  return utils.hasATag([// inheritdoc implies that all documentation is inherited
  // see https://jsdoc.app/tags-inheritdoc.html
  //
  // Abstract methods are by definition incomplete,
  // so it is not an error if it declares a return value but does not implement it.
  'abstract', 'virtual', // Constructors do not have a return value by definition (https://jsdoc.app/tags-class.html)
  // So we can bail out here, too.
  'class', 'constructor', // Return type is specified by type in @type
  'type', // This seems to imply a class as well
  'interface']) || utils.isConstructor() || // Though ESLint avoided getters: https://github.com/eslint/eslint/blob/master/lib/rules/valid-jsdoc.js#L435
  //  ... getters seem that they should, unlike setters, always return:
  utils.isSetter() || utils.avoidDocs();
};

var _default = (0, _iterateJsdoc.default)(({
  report,
  utils,
  context
}) => {
  (0, _warnRemovedSettings.default)(context, 'require-returns'); // A preflight check. We do not need to run a deep check
  // in case the @returns comment is optional or undefined.

  if (canSkip(utils)) {
    return;
  }

  const _ref = context.options[0] || {},
        _ref$forceRequireRetu = _ref.forceRequireReturn,
        forceRequireReturn = _ref$forceRequireRetu === void 0 ? false : _ref$forceRequireRetu,
        _ref$forceReturnsWith = _ref.forceReturnsWithAsync,
        forceReturnsWithAsync = _ref$forceReturnsWith === void 0 ? false : _ref$forceReturnsWith;

  const tagName = utils.getPreferredTagName({
    tagName: 'returns'
  });

  if (!tagName) {
    return;
  }

  const tags = utils.getTags(tagName);

  if (tags.length > 1) {
    report(`Found more than one @${tagName} declaration.`);
  }

  const iteratingFunction = utils.isIteratingFunction(); // In case the code returns something, we expect a return value in JSDoc.

  const _tags = _slicedToArray(tags, 1),
        tag = _tags[0];

  const missingReturnTag = typeof tag === 'undefined' || tag === null;

  const shouldReport = () => {
    if (!missingReturnTag) {
      return false;
    }

    if (forceRequireReturn && (iteratingFunction || utils.isVirtualFunction())) {
      return true;
    }

    const isAsync = !iteratingFunction && utils.hasTag('async') || iteratingFunction && utils.isAsync();

    if (forceReturnsWithAsync && isAsync) {
      return true;
    }

    return !isAsync && iteratingFunction && utils.hasReturnValue();
  };

  if (shouldReport()) {
    report(`Missing JSDoc @${tagName} declaration.`);
  }
}, {
  contextDefaults: true,
  meta: {
    schema: [{
      additionalProperties: false,
      properties: {
        contexts: {
          items: {
            type: 'string'
          },
          type: 'array'
        },
        exemptedBy: {
          items: {
            type: 'string'
          },
          type: 'array'
        },
        forceRequireReturn: {
          default: false,
          type: 'boolean'
        },
        forceReturnsWithAsync: {
          default: false,
          type: 'boolean'
        }
      },
      type: 'object'
    }],
    type: 'suggestion'
  }
});

exports.default = _default;
module.exports = exports.default;
//# sourceMappingURL=requireReturns.js.map