index.cjs 9.4 KB

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