index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict';
  2. const eachDeclarationBlock = require('../../utils/eachDeclarationBlock');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const shorthandData = require('../../reference/shorthandData');
  6. const validateOptions = require('../../utils/validateOptions');
  7. const vendor = require('../../utils/vendor');
  8. const ruleName = 'declaration-block-no-shorthand-property-overrides';
  9. const messages = ruleMessages(ruleName, {
  10. rejected: (shorthand, original) => `Unexpected shorthand "${shorthand}" after "${original}"`,
  11. });
  12. const meta = {
  13. url: 'https://stylelint.io/user-guide/rules/list/declaration-block-no-shorthand-property-overrides',
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, { actual: primary });
  19. if (!validOptions) {
  20. return;
  21. }
  22. eachDeclarationBlock(root, (eachDecl) => {
  23. /** @type {Record<string, string>} */
  24. const declarations = {};
  25. eachDecl((decl) => {
  26. const prop = decl.prop;
  27. const unprefixedProp = vendor.unprefixed(prop);
  28. const prefix = vendor.prefix(prop).toLowerCase();
  29. const overrideables = shorthandData[unprefixedProp.toLowerCase()];
  30. if (!overrideables) {
  31. declarations[prop.toLowerCase()] = prop;
  32. return;
  33. }
  34. for (const longhandProp of overrideables) {
  35. if (!Object.prototype.hasOwnProperty.call(declarations, prefix + longhandProp)) {
  36. continue;
  37. }
  38. report({
  39. ruleName,
  40. result,
  41. node: decl,
  42. message: messages.rejected(prop, declarations[prefix + longhandProp]),
  43. });
  44. }
  45. });
  46. });
  47. };
  48. };
  49. rule.ruleName = ruleName;
  50. rule.messages = messages;
  51. rule.meta = meta;
  52. module.exports = rule;