require-loading-label.js 1.4 KB

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