index.mjs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import stylelint from 'stylelint';
  2. const inline = {
  3. start: { ltr: 'left', rtl: 'right' },
  4. end: { ltr: 'right', rtl: 'left' }
  5. };
  6. const physical4Prop = [
  7. [ [ 'top', 'left', 'bottom', 'right' ], 'inset' ],
  8. [ [ 'margin-top', 'margin-left', 'margin-bottom', 'margin-right' ], 'margin' ],
  9. [ [ 'padding-top', 'padding-left', 'padding-bottom', 'padding-right' ], 'padding' ]
  10. ];
  11. const physical2Prop = () => [
  12. [ [ 'top', 'bottom' ], 'inset-block' ],
  13. [ [ 'left', 'right' ], 'inset-inline' ],
  14. [ [ 'margin-top', 'margin-bottom' ], 'margin-block' ],
  15. [ [ 'margin-left', 'margin-right' ], 'margin-inline' ],
  16. [ [ 'padding-top', 'padding-bottom' ], 'padding-block' ],
  17. [ [ 'padding-left', 'padding-right' ], 'padding-inline' ],
  18. ];
  19. const physicalProp = dir => [
  20. [ [ 'top' ], 'inset-block-start' ],
  21. [ [ 'bottom' ], 'inset-block-end' ],
  22. [ [ inline.start[dir] ], 'inset-inline-start' ],
  23. [ [ inline.end[dir] ], 'inset-inline-end' ],
  24. [ [ 'margin-top' ], 'margin-block-start' ],
  25. [ [ 'margin-bottom' ], 'margin-block-end' ],
  26. [ [ `margin-${inline.start[dir]}` ], 'margin-inline-start' ],
  27. [ [ `margin-${inline.end[dir]}` ], 'margin-inline-end' ],
  28. [ [ 'padding-top' ], 'padding-block-start' ],
  29. [ [ 'padding-bottom' ], 'padding-block-end' ],
  30. [ [ `padding-${inline.start[dir]}` ], 'padding-inline-start' ],
  31. [ [ `padding-${inline.end[dir]}` ], 'padding-inline-end' ],
  32. // width, height
  33. [ [ 'width' ], 'inline-size' ],
  34. [ [ 'min-width' ], 'min-inline-size' ],
  35. [ [ 'max-width' ], 'max-inline-size' ],
  36. [ [ 'height' ], 'block-size' ],
  37. [ [ 'min-height' ], 'min-block-size' ],
  38. [ [ 'max-height' ], 'max-block-size' ],
  39. // border
  40. [ [ 'border-top' ], 'border-block-start' ],
  41. [ [ 'border-bottom' ], 'border-block-end' ],
  42. [ [ `border-${inline.start[dir]}` ], 'border-inline-start' ],
  43. [ [ `border-${inline.end[dir]}` ], 'border-inline-end' ],
  44. [ [ 'border-top-color' ], 'border-block-start-color' ],
  45. [ [ 'border-top-style' ], 'border-block-start-style' ],
  46. [ [ 'border-top-width' ], 'border-block-start-width' ],
  47. [ [ 'border-bottom-color' ], 'border-block-end-color' ],
  48. [ [ 'border-bottom-style' ], 'border-block-end-style' ],
  49. [ [ 'border-bottom-width' ], 'border-block-end-width' ],
  50. [ [ `border-${inline.start[dir]}-color` ], 'border-inline-start-color' ],
  51. [ [ `border-${inline.start[dir]}-style` ], 'border-inline-start-style' ],
  52. [ [ `border-${inline.start[dir]}-width` ], 'border-inline-start-width' ],
  53. [ [ `border-${inline.end[dir]}-color` ], 'border-inline-end-color' ],
  54. [ [ `border-${inline.end[dir]}-style` ], 'border-inline-end-style' ],
  55. [ [ `border-${inline.end[dir]}-width` ], 'border-inline-end-width' ],
  56. [ [ `border-top-${inline.start[dir]}-radius` ], 'border-start-start-radius' ],
  57. [ [ `border-bottom-${inline.start[dir]}-radius` ], 'border-end-start-radius' ],
  58. [ [ `border-top-${inline.end[dir]}-radius` ], 'border-start-end-radius' ],
  59. [ [ `border-bottom-${inline.end[dir]}-radius` ], 'border-end-end-radius' ],
  60. ];
  61. const physicalValue = dir => [
  62. [ /^clear$/i, {
  63. [inline.start[dir]]: 'inline-start',
  64. [inline.end[dir]]: 'inline-end'
  65. }],
  66. [ /^float$/i, {
  67. [inline.start[dir]]: 'inline-start',
  68. [inline.end[dir]]: 'inline-end'
  69. }],
  70. [ /^text-align$/i, {
  71. [inline.start[dir]]: 'start',
  72. [inline.end[dir]]: 'end'
  73. }]
  74. ];
  75. const validateRuleWithProps = (root, props, fn) => {
  76. // conditionally walk nodes with children
  77. if (root.nodes && root.nodes.length) {
  78. const args = [];
  79. const hasProps = props.every(prop => {
  80. const declIndex = root.nodes.findIndex(child => child.type === 'decl' && child.prop === prop);
  81. const decl = root.nodes[declIndex];
  82. if (decl) {
  83. args.push(decl, declIndex);
  84. }
  85. return decl;
  86. });
  87. if (hasProps) {
  88. fn(...args);
  89. }
  90. }
  91. };
  92. var ruleName = 'csstools/use-logical';
  93. var messages = stylelint.utils.ruleMessages(ruleName, {
  94. unexpectedProp(physicalProperty, logicalProperty) {
  95. return `Unexpected "${physicalProperty}" property. Use "${logicalProperty}".`;
  96. },
  97. unexpectedValue(property, physicalValue, logicalValue) {
  98. return `Unexpected "${physicalValue}" value in "${property}" property. Use "${logicalValue}".`;
  99. }
  100. });
  101. // walk all container nodes
  102. function walk(node, fn) {
  103. if (node.nodes && node.nodes.length) {
  104. const nodes = node.nodes.slice();
  105. const length = nodes.length;
  106. let index = -1;
  107. while (++index < length) {
  108. const child = nodes[index];
  109. if (!isDirRule(child)) {
  110. fn(child);
  111. walk(child, fn);
  112. }
  113. }
  114. }
  115. }
  116. const dirSelectorRegExp = /:dir\(ltr|rtl\)/i;
  117. const isDirRule = node => node.type === 'rule' && dirSelectorRegExp.test(node.selector);
  118. const reportedDecls = new WeakMap();
  119. function ruleFunc(method, opts, context) {
  120. const propExceptions = [].concat(Object(opts).except || []);
  121. const isAutofix = isContextAutofixing(context);
  122. const dir = /^rtl$/i.test(Object(opts).direction) ? 'rtl' : 'ltr';
  123. return (root, result) => {
  124. // validate the method
  125. const isMethodValid = stylelint.utils.validateOptions(result, ruleName, {
  126. actual: method,
  127. possible() {
  128. return isMethodIndifferent(method) ||
  129. isMethodAlways(method)
  130. }
  131. });
  132. const reportUnexpectedProperty = (decl, logicalProperty) => stylelint.utils.report({
  133. message: messages.unexpectedProp(decl.prop, logicalProperty),
  134. node: decl,
  135. result,
  136. ruleName
  137. });
  138. const reportUnexpectedValue = (node, value) => stylelint.utils.report({
  139. message: messages.unexpectedValue(node.prop, node.value, value),
  140. node,
  141. result,
  142. ruleName
  143. });
  144. if (isMethodValid && isMethodAlways(method)) {
  145. walk(root, node => {
  146. // validate or autofix 4 physical properties as logical shorthands
  147. physical4Prop.forEach(([ props, prop ]) => {
  148. validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex, blockEndDecl, blockEndIndex, inlineEndDecl, inlineEndIndex) => { // eslint-disable-line
  149. const firstInlineDecl = blockStartDecl;
  150. if (isAutofix) {
  151. const values = [ blockStartDecl.value, inlineStartDecl.value, blockEndDecl.value, inlineEndDecl.value ];
  152. if (values[1] === values[3]) {
  153. values.pop();
  154. if (values[2] === values[1]) {
  155. values.pop();
  156. if (values[1] === values[0]) {
  157. values.pop();
  158. }
  159. }
  160. }
  161. firstInlineDecl.cloneBefore({
  162. prop,
  163. value: values.length <= 2 ? values.join(' ') : `logical ${values.join(' ')}`
  164. });
  165. blockStartDecl.remove();
  166. inlineStartDecl.remove();
  167. blockEndDecl.remove();
  168. inlineEndDecl.remove();
  169. } else if (!isDeclReported(blockStartDecl) && !isDeclReported(inlineStartDecl) && !isDeclReported(blockEndDecl) && !isDeclReported(inlineEndDecl)) {
  170. reportUnexpectedProperty(firstInlineDecl, prop);
  171. reportedDecls.set(blockStartDecl);
  172. reportedDecls.set(inlineStartDecl);
  173. reportedDecls.set(blockEndDecl);
  174. reportedDecls.set(inlineEndDecl);
  175. }
  176. });
  177. });
  178. // validate or autofix 2 physical properties as logical shorthands
  179. physical2Prop().forEach(([ props, prop ]) => {
  180. validateRuleWithProps(node, props, (blockStartDecl, blockStartIndex, inlineStartDecl, inlineStartIndex) => { // eslint-disable-line
  181. const firstInlineDecl = blockStartIndex < inlineStartIndex
  182. ? blockStartDecl
  183. : inlineStartDecl;
  184. if (isAutofix) {
  185. firstInlineDecl.cloneBefore({
  186. prop,
  187. value: blockStartDecl.value === inlineStartDecl.value
  188. ? blockStartDecl.value
  189. : [ blockStartDecl.value, inlineStartDecl.value ].join(' ')
  190. });
  191. blockStartDecl.remove();
  192. inlineStartDecl.remove();
  193. } else if (!isDeclReported(blockStartDecl) && !isDeclReported(inlineStartDecl)) {
  194. reportUnexpectedProperty(firstInlineDecl, prop);
  195. reportedDecls.set(blockStartDecl);
  196. reportedDecls.set(inlineStartDecl);
  197. }
  198. });
  199. });
  200. // validate or autofix physical properties as logical
  201. physicalProp(dir).forEach(([ props, prop ]) => {
  202. validateRuleWithProps(node, props, physicalDecl => {
  203. if (!isDeclAnException(physicalDecl, propExceptions)) {
  204. if (isAutofix) {
  205. physicalDecl.prop = prop;
  206. } else if (!isDeclReported(physicalDecl)) {
  207. reportUnexpectedProperty(physicalDecl, prop);
  208. reportedDecls.set(physicalDecl);
  209. }
  210. }
  211. });
  212. });
  213. // validate or autofix physical values as logical
  214. physicalValue(dir).forEach(([ regexp, props ]) => {
  215. if (isNodeMatchingDecl(node, regexp) && !isDeclAnException(node, propExceptions)) {
  216. const valuekey = node.value.toLowerCase();
  217. if (valuekey in props) {
  218. const value = props[valuekey];
  219. if (isAutofix) {
  220. node.value = value;
  221. } else {
  222. reportUnexpectedValue(node, value);
  223. reportedDecls.set(node);
  224. }
  225. }
  226. }
  227. });
  228. });
  229. }
  230. };
  231. }ruleFunc.ruleName = ruleName;
  232. var index = stylelint.createPlugin(ruleName, ruleFunc);
  233. const isMethodIndifferent = method => method === 'ignore' || method === false || method === null;
  234. const isMethodAlways = method => method === 'always' || method === true;
  235. const isContextAutofixing = context => Boolean(Object(context).fix);
  236. const isNodeMatchingDecl = (decl, regexp) => decl.type === 'decl' && regexp.test(decl.prop);
  237. const isDeclAnException = (decl, propExceptions) => propExceptions.some(match => match instanceof RegExp
  238. ? match.test(decl.prop)
  239. : String(match || '').toLowerCase() === String(decl.prop || '').toLowerCase());
  240. const isDeclReported = decl => reportedDecls.has(decl);
  241. export { index as default };
  242. //# sourceMappingURL=index.mjs.map