annotations.js 951 B

1234567891011121314151617181920212223242526272829
  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. * @returns {Boolean} True if the node is a type annotated props declaration, false if not.
  11. */
  12. function isAnnotatedFunctionPropsDeclaration(node, context) {
  13. if (!node || !node.params || !node.params.length) {
  14. return false;
  15. }
  16. const tokens = context.getFirstTokens(node.params[0], 2);
  17. const isAnnotated = node.params[0].typeAnnotation;
  18. const isDestructuredProps = node.params[0].type === 'ObjectPattern';
  19. const isProps = tokens[0].value === 'props' || (tokens[1] && tokens[1].value === 'props');
  20. return (isAnnotated && (isDestructuredProps || isProps));
  21. }
  22. module.exports = {
  23. isAnnotatedFunctionPropsDeclaration: isAnnotatedFunctionPropsDeclaration
  24. };