getPropertyValuePath.js 642 B

123456789101112131415161718
  1. import getPropertyName from './getPropertyName.js';
  2. /**
  3. * Given an ObjectExpression, this function returns the path of the value of
  4. * the property with name `propertyName`. if the property is an ObjectMethod we
  5. * return the ObjectMethod itself.
  6. */
  7. export default function getPropertyValuePath(path, propertyName) {
  8. const property = path
  9. .get('properties')
  10. .find((propertyPath) => !propertyPath.isSpreadElement() &&
  11. getPropertyName(propertyPath) === propertyName);
  12. if (property) {
  13. return property.isObjectMethod()
  14. ? property
  15. : property.get('value');
  16. }
  17. return null;
  18. }