annotations.js 1.1 KB

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