annotations.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. /**
  2. * @fileoverview Utility functions for type annotation detection.
  3. * @author Yannick Croissant
  4. * @author Vitor Balocco
  5. */
  6. 'use strict';
  7. /**
  8. * Checks if we are declaring a `props` argument with a flow type annotation.
  9. * @param {ASTNode} node The AST node being checked.
  10. * @param {Object} context
  11. * @returns {Boolean} True if the node is a type annotated props declaration, false if not.
  12. */
  13. function isAnnotatedFunctionPropsDeclaration(node, context) {
  14. if (!node || !node.params || !node.params.length) {
  15. return false;
  16. }
  17. const typeNode = node.params[0].type === 'AssignmentPattern' ? node.params[0].left : node.params[0];
  18. const tokens = context.getFirstTokens(typeNode, 2);
  19. const isAnnotated = typeNode.typeAnnotation;
  20. const isDestructuredProps = typeNode.type === 'ObjectPattern';
  21. const isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
  22. return (isAnnotated && (isDestructuredProps || isProps));
  23. }
  24. module.exports = {
  25. isAnnotatedFunctionPropsDeclaration
  26. };