logicalPropertiesLinter.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { lintWarning } from "./utils";
  2. var linter = function 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 '".concat(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. var valueArr = value.split(' ').map(function (item) {
  31. return item.trim();
  32. });
  33. if (valueArr.length === 4 && valueArr[1] !== valueArr[3]) {
  34. lintWarning("You seem to be using '".concat(key, "' property with different left ").concat(key, " and right ").concat(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);
  35. }
  36. }
  37. return;
  38. case 'clear':
  39. case 'textAlign':
  40. if (value === 'left' || value === 'right') {
  41. lintWarning("You seem to be using non-logical value '".concat(value, "' of ").concat(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);
  42. }
  43. return;
  44. case 'borderRadius':
  45. if (typeof value === 'string') {
  46. var radiusGroups = value.split('/').map(function (item) {
  47. return item.trim();
  48. });
  49. var invalid = radiusGroups.reduce(function (result, group) {
  50. if (result) {
  51. return result;
  52. }
  53. var radiusArr = group.split(' ').map(function (item) {
  54. return item.trim();
  55. });
  56. // borderRadius: '2px 4px'
  57. if (radiusArr.length >= 2 && radiusArr[0] !== radiusArr[1]) {
  58. return true;
  59. }
  60. // borderRadius: '4px 4px 2px'
  61. if (radiusArr.length === 3 && radiusArr[1] !== radiusArr[2]) {
  62. return true;
  63. }
  64. // borderRadius: '4px 4px 2px 4px'
  65. if (radiusArr.length === 4 && radiusArr[2] !== radiusArr[3]) {
  66. return true;
  67. }
  68. return result;
  69. }, false);
  70. if (invalid) {
  71. lintWarning("You seem to be using non-logical value '".concat(value, "' of ").concat(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);
  72. }
  73. }
  74. return;
  75. default:
  76. }
  77. };
  78. export default linter;