index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. 'use strict';
  2. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  3. const isStandardSyntaxSelector = require('../../utils/isStandardSyntaxSelector');
  4. const keywordSets = require('../../reference/keywordSets');
  5. const optionsMatches = require('../../utils/optionsMatches');
  6. const parseSelector = require('../../utils/parseSelector');
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const validateOptions = require('../../utils/validateOptions');
  10. const vendor = require('../../utils/vendor');
  11. const { isString } = require('../../utils/validateTypes');
  12. const ruleName = 'selector-pseudo-element-no-unknown';
  13. const messages = ruleMessages(ruleName, {
  14. rejected: (selector) => `Unexpected unknown pseudo-element selector "${selector}"`,
  15. });
  16. const meta = {
  17. url: 'https://stylelint.io/user-guide/rules/list/selector-pseudo-element-no-unknown',
  18. };
  19. /** @type {import('stylelint').Rule} */
  20. const rule = (primary, secondaryOptions) => {
  21. return (root, result) => {
  22. const validOptions = validateOptions(
  23. result,
  24. ruleName,
  25. { actual: primary },
  26. {
  27. actual: secondaryOptions,
  28. possible: {
  29. ignorePseudoElements: [isString],
  30. },
  31. optional: true,
  32. },
  33. );
  34. if (!validOptions) {
  35. return;
  36. }
  37. root.walkRules((ruleNode) => {
  38. if (!isStandardSyntaxRule(ruleNode)) {
  39. return;
  40. }
  41. const selector = ruleNode.selector;
  42. // Return early before parse if no pseudos for performance
  43. if (!selector.includes(':')) {
  44. return;
  45. }
  46. parseSelector(selector, result, ruleNode, (selectorTree) => {
  47. selectorTree.walkPseudos((pseudoNode) => {
  48. const value = pseudoNode.value;
  49. if (!isStandardSyntaxSelector(value)) {
  50. return;
  51. }
  52. // Ignore pseudo-classes
  53. if (value.slice(0, 2) !== '::') {
  54. return;
  55. }
  56. if (optionsMatches(secondaryOptions, 'ignorePseudoElements', pseudoNode.value.slice(2))) {
  57. return;
  58. }
  59. const name = value.slice(2);
  60. if (vendor.prefix(name) || keywordSets.pseudoElements.has(name.toLowerCase())) {
  61. return;
  62. }
  63. report({
  64. message: messages.rejected(value),
  65. node: ruleNode,
  66. index: pseudoNode.sourceIndex,
  67. ruleName,
  68. result,
  69. });
  70. });
  71. });
  72. });
  73. };
  74. };
  75. rule.ruleName = ruleName;
  76. rule.messages = messages;
  77. rule.meta = meta;
  78. module.exports = rule;