index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. const isCustomProperty = require('../../utils/isCustomProperty');
  3. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  4. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  5. const optionsMatches = require('../../utils/optionsMatches');
  6. const properties = require('known-css-properties').all;
  7. const report = require('../../utils/report');
  8. const ruleMessages = require('../../utils/ruleMessages');
  9. const { isAtRule, isRule } = require('../../utils/typeGuards');
  10. const validateOptions = require('../../utils/validateOptions');
  11. const vendor = require('../../utils/vendor');
  12. const { isBoolean, isRegExp, isString } = require('../../utils/validateTypes');
  13. const ruleName = 'property-no-unknown';
  14. const messages = ruleMessages(ruleName, {
  15. rejected: (property) => `Unexpected unknown property "${property}"`,
  16. });
  17. const meta = {
  18. url: 'https://stylelint.io/user-guide/rules/property-no-unknown',
  19. };
  20. /** @type {import('stylelint').Rule} */
  21. const rule = (primary, secondaryOptions) => {
  22. const allValidProperties = new Set(properties);
  23. return (root, result) => {
  24. const validOptions = validateOptions(
  25. result,
  26. ruleName,
  27. { actual: primary },
  28. {
  29. actual: secondaryOptions,
  30. possible: {
  31. ignoreProperties: [isString, isRegExp],
  32. checkPrefixed: [isBoolean],
  33. ignoreSelectors: [isString, isRegExp],
  34. ignoreAtRules: [isString, isRegExp],
  35. },
  36. optional: true,
  37. },
  38. );
  39. if (!validOptions) {
  40. return;
  41. }
  42. const shouldCheckPrefixed = secondaryOptions && secondaryOptions.checkPrefixed;
  43. root.walkDecls(checkStatement);
  44. /**
  45. * @param {import('postcss').Declaration} decl
  46. */
  47. function checkStatement(decl) {
  48. const prop = decl.prop;
  49. if (!isStandardSyntaxProperty(prop)) {
  50. return;
  51. }
  52. if (!isStandardSyntaxDeclaration(decl)) {
  53. return;
  54. }
  55. if (isCustomProperty(prop)) {
  56. return;
  57. }
  58. if (!shouldCheckPrefixed && vendor.prefix(prop)) {
  59. return;
  60. }
  61. if (optionsMatches(secondaryOptions, 'ignoreProperties', prop)) {
  62. return;
  63. }
  64. const parent = decl.parent;
  65. if (
  66. parent &&
  67. isRule(parent) &&
  68. optionsMatches(secondaryOptions, 'ignoreSelectors', parent.selector)
  69. ) {
  70. return;
  71. }
  72. /** @type {import('postcss').Node | undefined} */
  73. let node = parent;
  74. while (node && node.type !== 'root') {
  75. if (isAtRule(node) && optionsMatches(secondaryOptions, 'ignoreAtRules', node.name)) {
  76. return;
  77. }
  78. node = node.parent;
  79. }
  80. if (allValidProperties.has(prop.toLowerCase())) {
  81. return;
  82. }
  83. report({
  84. message: messages.rejected(prop),
  85. node: decl,
  86. result,
  87. ruleName,
  88. word: prop,
  89. });
  90. }
  91. };
  92. };
  93. rule.ruleName = ruleName;
  94. rule.messages = messages;
  95. rule.meta = meta;
  96. module.exports = rule;