logicalPropertiesLinter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { lintWarning } from './utils';
  2. const linter = (key, value, info) => {
  3. switch (key) {
  4. case 'marginLeft':
  5. case 'marginRight':
  6. case 'paddingLeft':
  7. case 'paddingRight':
  8. case 'left':
  9. case 'right':
  10. case 'borderLeft':
  11. case 'borderLeftWidth':
  12. case 'borderLeftStyle':
  13. case 'borderLeftColor':
  14. case 'borderRight':
  15. case 'borderRightWidth':
  16. case 'borderRightStyle':
  17. case 'borderRightColor':
  18. case 'borderTopLeftRadius':
  19. case 'borderTopRightRadius':
  20. case 'borderBottomLeftRadius':
  21. case 'borderBottomRightRadius':
  22. lintWarning(`You seem to be using non-logical property '${key}' which is not compatible with RTL mode. Please use logical properties and values instead. For more information: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties.`, info);
  23. return;
  24. case 'margin':
  25. case 'padding':
  26. case 'borderWidth':
  27. case 'borderStyle':
  28. // case 'borderColor':
  29. if (typeof value === 'string') {
  30. const valueArr = value.split(' ').map(item => item.trim());
  31. if (valueArr.length === 4 && valueArr[1] !== valueArr[3]) {
  32. lintWarning(`You seem to be using '${key}' property with different left ${key} and right ${key}, which is not compatible with RTL mode. Please use logical properties and values instead. For more information: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties.`, info);
  33. }
  34. }
  35. return;
  36. case 'clear':
  37. case 'textAlign':
  38. if (value === 'left' || value === 'right') {
  39. lintWarning(`You seem to be using non-logical value '${value}' of ${key}, which is not compatible with RTL mode. Please use logical properties and values instead. For more information: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties.`, info);
  40. }
  41. return;
  42. case 'borderRadius':
  43. if (typeof value === 'string') {
  44. const radiusGroups = value.split('/').map(item => item.trim());
  45. const invalid = radiusGroups.reduce((result, group) => {
  46. if (result) {
  47. return result;
  48. }
  49. const radiusArr = group.split(' ').map(item => item.trim());
  50. // borderRadius: '2px 4px'
  51. if (radiusArr.length >= 2 && radiusArr[0] !== radiusArr[1]) {
  52. return true;
  53. }
  54. // borderRadius: '4px 4px 2px'
  55. if (radiusArr.length === 3 && radiusArr[1] !== radiusArr[2]) {
  56. return true;
  57. }
  58. // borderRadius: '4px 4px 2px 4px'
  59. if (radiusArr.length === 4 && radiusArr[2] !== radiusArr[3]) {
  60. return true;
  61. }
  62. return result;
  63. }, false);
  64. if (invalid) {
  65. lintWarning(`You seem to be using non-logical value '${value}' of ${key}, which is not compatible with RTL mode. Please use logical properties and values instead. For more information: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Logical_Properties.`, info);
  66. }
  67. }
  68. return;
  69. default:
  70. }
  71. };
  72. export default linter;