index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. const isAutoprefixable = require('../../utils/isAutoprefixable');
  3. const isStandardSyntaxRule = require('../../utils/isStandardSyntaxRule');
  4. const optionsMatches = require('../../utils/optionsMatches');
  5. const parseSelector = require('../../utils/parseSelector');
  6. const report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const validateOptions = require('../../utils/validateOptions');
  9. const { isString } = require('../../utils/validateTypes');
  10. const ruleName = 'selector-no-vendor-prefix';
  11. const messages = ruleMessages(ruleName, {
  12. rejected: (selector) => `Unexpected vendor-prefix "${selector}"`,
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/list/selector-no-vendor-prefix',
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, secondaryOptions, context) => {
  19. return (root, result) => {
  20. const validOptions = validateOptions(
  21. result,
  22. ruleName,
  23. { actual: primary },
  24. {
  25. actual: secondaryOptions,
  26. possible: {
  27. ignoreSelectors: [isString],
  28. },
  29. optional: true,
  30. },
  31. );
  32. if (!validOptions) {
  33. return;
  34. }
  35. root.walkRules((ruleNode) => {
  36. if (!isStandardSyntaxRule(ruleNode)) {
  37. return;
  38. }
  39. const selector = ruleNode.selector;
  40. parseSelector(selector, result, ruleNode, (selectorTree) => {
  41. selectorTree.walkPseudos((pseudoNode) => {
  42. if (isAutoprefixable.selector(pseudoNode.value)) {
  43. if (optionsMatches(secondaryOptions, 'ignoreSelectors', pseudoNode.value)) {
  44. return;
  45. }
  46. if (context.fix) {
  47. ruleNode.selector = isAutoprefixable.unprefix(ruleNode.selector);
  48. return;
  49. }
  50. report({
  51. result,
  52. ruleName,
  53. message: messages.rejected(pseudoNode.value),
  54. node: ruleNode,
  55. index: (ruleNode.raws.before || '').length + pseudoNode.sourceIndex,
  56. });
  57. }
  58. });
  59. });
  60. });
  61. };
  62. };
  63. rule.ruleName = ruleName;
  64. rule.messages = messages;
  65. rule.meta = meta;
  66. module.exports = rule;