index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. 'use strict';
  2. const isCustomProperty = require('../../utils/isCustomProperty');
  3. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  4. const report = require('../../utils/report');
  5. const ruleMessages = require('../../utils/ruleMessages');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const optionsMatches = require('../../utils/optionsMatches');
  8. const { isRegExp, isString } = require('../../utils/validateTypes');
  9. const { isRule } = require('../../utils/typeGuards');
  10. const ruleName = 'property-case';
  11. const messages = ruleMessages(ruleName, {
  12. expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/list/property-case',
  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. {
  24. actual: primary,
  25. possible: ['lower', 'upper'],
  26. },
  27. {
  28. actual: secondaryOptions,
  29. possible: {
  30. ignoreSelectors: [isString, isRegExp],
  31. },
  32. optional: true,
  33. },
  34. );
  35. if (!validOptions) {
  36. return;
  37. }
  38. root.walkDecls((decl) => {
  39. const prop = decl.prop;
  40. if (!isStandardSyntaxProperty(prop)) {
  41. return;
  42. }
  43. if (isCustomProperty(prop)) {
  44. return;
  45. }
  46. const { parent } = decl;
  47. if (!parent) throw new Error('A parent node must be present');
  48. if (isRule(parent)) {
  49. const { selector } = parent;
  50. if (selector && optionsMatches(secondaryOptions, 'ignoreSelectors', selector)) {
  51. return;
  52. }
  53. }
  54. const expectedProp = primary === 'lower' ? prop.toLowerCase() : prop.toUpperCase();
  55. if (prop === expectedProp) {
  56. return;
  57. }
  58. if (context.fix) {
  59. decl.prop = expectedProp;
  60. return;
  61. }
  62. report({
  63. message: messages.expected(prop, expectedProp),
  64. node: decl,
  65. ruleName,
  66. result,
  67. });
  68. });
  69. };
  70. };
  71. rule.ruleName = ruleName;
  72. rule.messages = messages;
  73. rule.meta = meta;
  74. module.exports = rule;