require-loading-label.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module.exports = {
  2. meta: {
  3. type: 'problem',
  4. fixable: null,
  5. docs: {
  6. description:
  7. 'Require loadingLabel prop when isLoading is specified on OLButton',
  8. },
  9. schema: [],
  10. },
  11. create(context) {
  12. return {
  13. 'JSXOpeningElement[name.name="OLButton"]'(node) {
  14. const attributes = new Map(
  15. node.attributes.map(attr => [attr.name?.name, attr])
  16. )
  17. const isLoadingAttr = attributes.get('isLoading')
  18. const loadingLabelAttr = attributes.get('loadingLabel')
  19. if (isLoadingAttr && !loadingLabelAttr) {
  20. const isLoadingValue = isLoadingAttr.value
  21. if (
  22. !isLoadingValue ||
  23. (isLoadingValue.type === 'JSXExpressionContainer' &&
  24. isLoadingValue.expression.type === 'Literal' &&
  25. isLoadingValue.expression.value === true)
  26. ) {
  27. context.report({
  28. node: isLoadingAttr,
  29. message:
  30. 'Button with isLoading prop must also specify loadingLabel',
  31. })
  32. } else if (
  33. isLoadingValue.type === 'JSXExpressionContainer' &&
  34. isLoadingValue.expression.type !== 'Literal'
  35. ) {
  36. context.report({
  37. node: isLoadingAttr,
  38. message:
  39. 'Button with isLoading prop must also specify loadingLabel',
  40. })
  41. }
  42. }
  43. },
  44. }
  45. },
  46. }