How to use the doctrine.type function in doctrine

To help you get started, we’ve selected a few doctrine examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Polymer / tools / packages / analyzer / src / javascript / function-scanner.ts View on Github external
const summary = (summaryTag && summaryTag.description) || '';
    const description = docs.description;

    let functionReturn = getReturnFromAnnotation(docs);
    if (functionReturn === undefined && babel.isFunction(node)) {
      functionReturn = inferReturnFromBody(node);
    }

    // TODO(justinfagnani): consolidate with similar param processing code in
    // docs.ts
    const functionParams: {type: string, desc: string, name: string}[] = [];
    const templateTypes: string[] = [];
    for (const tag of docs.tags) {
      if (tag.title === 'param') {
        functionParams.push({
          type: tag.type ? doctrine.type.stringify(tag.type) : 'N/A',
          desc: tag.description || '',
          name: tag.name || 'N/A'
        });
      } else if (tag.title === 'template') {
        for (let t of (tag.description || '').split(',')) {
          t = t.trim();
          if (t.length > 0) {
            templateTypes.push(t);
          }
        }
      }
    }
    // TODO(fks): parse params directly from `fn`, merge with docs.tags data

    const specificName = functionName.slice(functionName.lastIndexOf('.') + 1);
    this.functions.add(new ScannedFunction(
github Polymer / polymer-analyzer / src / javascript / esutil.ts View on Github external
// Parameter with a default: method(param = "default")
    name = nodeParam.left.name;
    defaultValue = generate(nodeParam.right).code;

  } else {
    // Some AST pattern we don't recognize. Hope the code generator does
    // something reasonable.
    name = generate(nodeParam).code;
  }

  let type;
  let description;
  const tag = paramTags.get(name);
  if (tag) {
    if (tag.type) {
      type = doctrine.type.stringify(tag.type);
    }
    if (tag.description) {
      description = tag.description;
    }
  }

  const param: MethodParam = {name, type, defaultValue, rest, description};
  return param;
}
github Polymer / tools / packages / analyzer / src / polymer / analyze-properties.ts View on Github external
for (const property of esutil.getSimpleObjectProperties(node)) {
    const prop = toScannedPolymerProperty(
        property, document.sourceRangeForNode(property)!, document);
    if (prop === undefined) {
      continue;
    }

    // toScannedPolymerProperty does the wrong thing for us with type. We want
    // type to be undefined unless there's a positive signal for the type.
    // toScannedPolymerProperty will give Object because it infers based on the
    // property declaration.
    prop.type = undefined;
    const typeTag = jsdoc.getTag(prop.jsdoc, 'type');
    if (typeTag) {
      prop.type =
          typeTag.type ? doctrine.type.stringify(typeTag.type) : undefined;
    }
    prop.published = true;

    let isComputed = false;

    const value = property.value;
    if (babel.isIdentifier(value)) {
      // Polymer supports this simple syntax, where only the attribute
      // deserializer is specified.
      prop.attributeType = value.name;

    } else if (!babel.isObjectExpression(value)) {
      continue;

    } else {
      /**
github teambit / bit / src / jsdoc / parser.js View on Github external
function formatTag(tag: Object): Object {
  delete tag.title;
  if (!tag.type) return tag;
  let formattedType = doctrine.type.stringify(tag.type);
  if (tag.type.type === doctrine.type.Syntax.TypeApplication) {
    // Doctrine adds a dot after the generic type for historical reasons.
    // see here for more info: https://github.com/eslint/doctrine/issues/185
    formattedType = formattedType.replace('.<', '<');
  }
  if (tag.type.type === doctrine.type.Syntax.OptionalType) {
    // Doctrine shows an optional type with a suffix `=` (e.g. `string=`), we prefer the more
    // common syntax `?` (e.g. `string?`)
    formattedType = formattedType.replace('=', '?');
  }
  tag.type = formattedType;

  return tag;
}
github teambit / bit / src / jsdoc / parser.js View on Github external
function formatTag(tag: Object): Object {
  delete tag.title;
  if (!tag.type) return tag;
  let formattedType = doctrine.type.stringify(tag.type);
  if (tag.type.type === doctrine.type.Syntax.TypeApplication) {
    // Doctrine adds a dot after the generic type for historical reasons.
    // see here for more info: https://github.com/eslint/doctrine/issues/185
    formattedType = formattedType.replace('.<', '<');
  }
  if (tag.type.type === doctrine.type.Syntax.OptionalType) {
    // Doctrine shows an optional type with a suffix `=` (e.g. `string=`), we prefer the more
    // common syntax `?` (e.g. `string?`)
    formattedType = formattedType.replace('=', '?');
  }
  tag.type = formattedType;

  return tag;
}
github azu / babel-plugin-jsdoc-to-assert / src / generators.js View on Github external
constructor(tag) {
    this.nameOfValue = tag.name;
    this.typeString = doctrine.type.stringify(tag.type, {compact: true});
    this.jsdocLikeString = `@${tag.title} {${this.typeString}} ${tag.name}`;
  }
github Polymer / tools / packages / analyzer / src / javascript / class-scanner.ts View on Github external
ret = getReturnFromAnnotation(jsdocAnn);

      if (babel.isFunctionExpression(node)) {
        (node.params || []).forEach((nodeParam) => {
          const param = toMethodParam(nodeParam, jsdocAnn);
          params.set(param.name, param);
        });
      } else {
        for (const tag of (jsdocAnn.tags || [])) {
          if (tag.title !== 'param' || !tag.name) {
            continue;
          }
          let tagType;
          let tagDescription;
          if (tag.type) {
            tagType = doctrine.type.stringify(tag.type);
          }
          if (tag.description) {
            tagDescription = tag.description;
          }
          params.set(
              tag.name,
              {name: tag.name, type: tagType, description: tagDescription});
        }
      }
    }

    if (ret === undefined && babel.isFunctionExpression(node)) {
      ret = inferReturnFromBody(node);
    }

    return {
github angular / dgeni / packages / jsdoc / processors / doctrine-tag-parser.js View on Github external
getType: function(tag) {
    var isOptional = tag.type.type === 'OptionalType';
    var mainType = isOptional ? tag.type.expression : tag.type;
    var mainTypeString = doctrine.type.stringify(mainType);
    var isUnion = mainType.type === 'UnionType';
    var typeList;
    if ( isUnion ) {
      typeList = _.map(mainType.elements, function(element) {
        return doctrine.type.stringify(element);
      });
    } else {
      typeList = [mainTypeString];
    }

    return {
      description: mainTypeString,
      optional: isOptional,
      typeList: typeList
    };
  }
github Polymer / tools / packages / analyzer / src / javascript / esutil.ts View on Github external
export function getClosureType(
    node: babel.Node,
    parsedJsdoc: doctrine.Annotation|undefined,
    sourceRange: SourceRange,
    document: ParsedDocument): Result {
  if (parsedJsdoc) {
    const typeTag = jsdoc.getTag(parsedJsdoc, 'type');
    if (typeTag) {
      return {successful: true, value: doctrine.type.stringify(typeTag.type!)};
    }
  }
  const type = VALID_EXPRESSION_TYPES.get(node.type);
  if (type) {
    return {successful: true, value: type};
  }
  if (babel.isIdentifier(node)) {
    return {
      successful: true,
      value: CLOSURE_CONSTRUCTOR_MAP.get(node.name) || node.name
    };
  }
  const warning = new Warning({
    code: 'no-closure-type',
    message: `Unable to determine closure type for expression of type ` +
        `${node.type}`,
github oxygenhq / oxygen / tools / apidoc.js View on Github external
commentParsed.getReturn = function() {
                for (var tag of this.tags) {
                    if (tag.title === 'return') {
                        var type = doctrine.type.stringify(tag.type, {compact:true});
                        type = type.replace(/<|>/ig, function(m){
                            return '&' + (m == '>' ? 'g' : 'l') + 't;';
                        });

                        return {
                            description: tag.description.replace(/(\r\n|\n)/gm,''), 
                            type: type
                        };
                    }
                }
            };
            commentParsed.getParams = function() {