index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const isAutoprefixable = require('../../utils/isAutoprefixable');
  4. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  5. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  6. const optionsMatches = require('../../utils/optionsMatches');
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const setDeclarationValue = require('../../utils/setDeclarationValue');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const vendor = require('../../utils/vendor');
  12. const { isString } = require('../../utils/validateTypes');
  13. const ruleName = 'value-no-vendor-prefix';
  14. const messages = ruleMessages(ruleName, {
  15. rejected: (value) => `Unexpected vendor-prefix "${value}"`,
  16. });
  17. const meta = {
  18. url: 'https://stylelint.io/user-guide/rules/list/value-no-vendor-prefix',
  19. };
  20. const valuePrefixes = ['-webkit-', '-moz-', '-ms-', '-o-'];
  21. /**
  22. * @param {string} value
  23. * @returns {boolean}
  24. */
  25. const hasPrefix = (value) => {
  26. const lowerValue = value.toLowerCase();
  27. return valuePrefixes.some((prefix) => lowerValue.startsWith(prefix));
  28. };
  29. /** @type {import('stylelint').Rule} */
  30. const rule = (primary, secondaryOptions, context) => {
  31. return (root, result) => {
  32. const validOptions = validateOptions(
  33. result,
  34. ruleName,
  35. { actual: primary },
  36. {
  37. optional: true,
  38. actual: secondaryOptions,
  39. possible: {
  40. ignoreValues: [isString],
  41. },
  42. },
  43. );
  44. if (!validOptions) {
  45. return;
  46. }
  47. root.walkDecls((decl) => {
  48. const { value } = decl;
  49. if (
  50. !isStandardSyntaxDeclaration(decl) ||
  51. !isStandardSyntaxProperty(decl.prop) ||
  52. !value.startsWith('-')
  53. ) {
  54. return;
  55. }
  56. if (optionsMatches(secondaryOptions, 'ignoreValues', vendor.unprefixed(value))) {
  57. return;
  58. }
  59. const parsedValue = valueParser(value);
  60. parsedValue.walk((node) => {
  61. if (!hasPrefix(node.value)) {
  62. return;
  63. }
  64. if (!isAutoprefixable.propertyValue(node.value)) {
  65. return;
  66. }
  67. if (context.fix) {
  68. node.value = isAutoprefixable.unprefix(node.value);
  69. return;
  70. }
  71. report({
  72. message: messages.rejected(node.value),
  73. node: decl,
  74. index: decl.prop.length + (decl.raws.between || '').length + node.sourceIndex,
  75. result,
  76. ruleName,
  77. });
  78. });
  79. setDeclarationValue(decl, parsedValue.toString());
  80. });
  81. };
  82. };
  83. rule.ruleName = ruleName;
  84. rule.messages = messages;
  85. rule.meta = meta;
  86. module.exports = rule;