index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = rule;
  6. exports.ruleName = exports.meta = exports.messages = void 0;
  7. var _stylelint = require("stylelint");
  8. var _utils = require("../../utils");
  9. var hasOwnProp = Object.prototype.hasOwnProperty;
  10. var ruleName = (0, _utils.namespace)("declaration-nested-properties");
  11. exports.ruleName = ruleName;
  12. var messages = _stylelint.utils.ruleMessages(ruleName, {
  13. expected: function expected(prop) {
  14. return "Expected property \"".concat(prop, "\" to be in a nested form");
  15. },
  16. rejected: function rejected(prop) {
  17. return "Unexpected nested property \"".concat(prop, "\"");
  18. }
  19. });
  20. exports.messages = messages;
  21. var meta = {
  22. url: (0, _utils.ruleUrl)(ruleName)
  23. };
  24. exports.meta = meta;
  25. function rule(expectation, options) {
  26. return function (root, result) {
  27. var validOptions = _stylelint.utils.validateOptions(result, ruleName, {
  28. actual: expectation,
  29. possible: ["always", "never"]
  30. }, {
  31. actual: options,
  32. possible: {
  33. except: ["only-of-namespace"]
  34. },
  35. optional: true
  36. });
  37. if (!validOptions) {
  38. return;
  39. }
  40. if (expectation === "always") {
  41. root.walk(function (item) {
  42. if (item.type !== "rule" && item.type !== "atrule") {
  43. return;
  44. }
  45. var warningCandidates = {};
  46. item.each(function (decl) {
  47. var prop = decl.prop,
  48. type = decl.type,
  49. selector = decl.selector;
  50. // Looking for namespaced non-nested properties
  51. // Namespaced prop is basically a prop with a `-` in a name, e.g. `margin-top`
  52. if (type === "decl") {
  53. if (!(0, _utils.isStandardSyntaxProperty)(prop)) {
  54. return;
  55. }
  56. // Add simple namespaced prop decls to warningCandidates.ns
  57. // (prop names with browser prefixes are ignored)
  58. var seekNamespace = /^([a-zA-Z\d]+)-/.exec(prop);
  59. if (seekNamespace && seekNamespace[1]) {
  60. var ns = seekNamespace[1];
  61. if (!hasOwnProp.call(warningCandidates, ns)) {
  62. warningCandidates[ns] = [];
  63. }
  64. warningCandidates[ns].push({
  65. node: decl
  66. });
  67. }
  68. }
  69. // Nested props, `prop: [value] { <nested decls> }`
  70. if (type === "rule" || type === "decl" && decl.isNested) {
  71. // `background:red {` - selector;
  72. // `background: red {` - nested prop; space is decisive here
  73. var testForProp = (0, _utils.parseNestedPropRoot)(selector || decl.toString());
  74. if (testForProp && testForProp.propName !== undefined) {
  75. var _ns = testForProp.propName.value;
  76. if (!hasOwnProp.call(warningCandidates, _ns)) {
  77. warningCandidates[_ns] = [];
  78. }
  79. warningCandidates[_ns].push({
  80. node: decl,
  81. nested: true
  82. });
  83. }
  84. }
  85. });
  86. // Now check if the found properties deserve warnings
  87. Object.keys(warningCandidates).forEach(function (namespace) {
  88. var exceptIfOnlyOfNs = (0, _utils.optionsHaveException)(options, "only-of-namespace");
  89. var moreThanOneProp = warningCandidates[namespace].length > 1;
  90. warningCandidates[namespace].forEach(function (candidate) {
  91. if (candidate.nested === true) {
  92. if (exceptIfOnlyOfNs) {
  93. // If there is only one prop inside a nested prop - warn (reverse "always")
  94. if (candidate.nested === true && candidate.node.nodes.length === 1) {
  95. _stylelint.utils.report({
  96. message: messages.rejected(namespace),
  97. node: candidate.node,
  98. result: result,
  99. ruleName: ruleName
  100. });
  101. }
  102. }
  103. } else {
  104. // Don't warn on non-nested namespaced props if there are
  105. // less than 2 of them, and except: "only-of-namespace" is set
  106. if (exceptIfOnlyOfNs && !moreThanOneProp) {
  107. return;
  108. }
  109. _stylelint.utils.report({
  110. message: messages.expected(candidate.node.prop),
  111. node: candidate.node,
  112. result: result,
  113. ruleName: ruleName
  114. });
  115. }
  116. });
  117. });
  118. });
  119. } else if (expectation === "never") {
  120. root.walk(function (item) {
  121. // Just check if there are ANY nested props
  122. if (item.type === "rule") {
  123. // `background:red {` - selector;
  124. // `background: red {` - nested prop; space is decisive here
  125. var testForProp = (0, _utils.parseNestedPropRoot)(item.selector);
  126. if (testForProp && testForProp.propName !== undefined) {
  127. _stylelint.utils.report({
  128. message: messages.rejected(testForProp.propName.value),
  129. result: result,
  130. ruleName: ruleName,
  131. node: item
  132. });
  133. }
  134. }
  135. });
  136. }
  137. };
  138. }
  139. rule.ruleName = ruleName;
  140. rule.messages = messages;
  141. rule.meta = meta;