index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 'use strict';
  2. const { isPlainObject } = require('is-plain-object');
  3. const { fork, parse, find } = require('css-tree');
  4. const declarationValueIndex = require('../../utils/declarationValueIndex');
  5. const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
  6. const report = require('../../utils/report');
  7. const ruleMessages = require('../../utils/ruleMessages');
  8. const validateOptions = require('../../utils/validateOptions');
  9. const validateObjectWithArrayProps = require('../../utils/validateObjectWithArrayProps');
  10. const isCustomProperty = require('../../utils/isCustomProperty');
  11. const isStandardSyntaxValue = require('../../utils/isStandardSyntaxValue');
  12. const isStandardSyntaxProperty = require('../../utils/isStandardSyntaxProperty');
  13. const isStandardSyntaxDeclaration = require('../../utils/isStandardSyntaxDeclaration');
  14. const { isAtRule } = require('../../utils/typeGuards');
  15. const { isRegExp, isString } = require('../../utils/validateTypes');
  16. const ruleName = 'declaration-property-value-no-unknown';
  17. const messages = ruleMessages(ruleName, {
  18. rejected: (property, value) => `Unexpected unknown value "${value}" for property "${property}"`,
  19. rejectedParseError: (property, value) =>
  20. `Cannot parse property value "${value}" for property "${property}"`,
  21. });
  22. const meta = {
  23. url: 'https://stylelint.io/user-guide/rules/declaration-property-value-no-unknown',
  24. };
  25. /** @type {import('stylelint').Rule} */
  26. const rule = (primary, secondaryOptions) => {
  27. return (root, result) => {
  28. const validOptions = validateOptions(
  29. result,
  30. ruleName,
  31. { actual: primary },
  32. {
  33. actual: secondaryOptions,
  34. possible: {
  35. ignoreProperties: [validateObjectWithArrayProps(isString, isRegExp)],
  36. propertiesSyntax: [isPlainObject],
  37. typesSyntax: [isPlainObject],
  38. },
  39. optional: true,
  40. },
  41. );
  42. if (!validOptions) {
  43. return;
  44. }
  45. const ignoreProperties = Array.from(
  46. Object.entries((secondaryOptions && secondaryOptions.ignoreProperties) || {}),
  47. );
  48. /** @type {(name: string, propValue: string) => boolean} */
  49. const isPropIgnored = (name, value) => {
  50. const [, valuePattern] =
  51. ignoreProperties.find(([namePattern]) => matchesStringOrRegExp(name, namePattern)) || [];
  52. return valuePattern && matchesStringOrRegExp(value, valuePattern);
  53. };
  54. const propertiesSyntax = (secondaryOptions && secondaryOptions.propertiesSyntax) || {};
  55. const typesSyntax = (secondaryOptions && secondaryOptions.typesSyntax) || {};
  56. const forkedLexer = fork({
  57. properties: propertiesSyntax,
  58. types: typesSyntax,
  59. }).lexer;
  60. root.walkDecls((decl) => {
  61. const { prop, value, parent } = decl;
  62. // NOTE: CSSTree's `fork()` doesn't support `-moz-initial`, but it may be possible in the future.
  63. // See https://github.com/stylelint/stylelint/pull/6511#issuecomment-1412921062
  64. if (/^-moz-initial$/i.test(value)) return;
  65. if (!isStandardSyntaxDeclaration(decl)) return;
  66. if (!isStandardSyntaxProperty(prop)) return;
  67. if (!isStandardSyntaxValue(value)) return;
  68. if (isCustomProperty(prop)) return;
  69. if (isPropIgnored(prop, value)) return;
  70. /** @type {import('css-tree').CssNode} */
  71. let cssTreeValueNode;
  72. try {
  73. cssTreeValueNode = parse(value, { context: 'value' });
  74. if (containsUnsupportedFunction(cssTreeValueNode)) return;
  75. } catch (e) {
  76. const index = declarationValueIndex(decl);
  77. const endIndex = index + value.length;
  78. report({
  79. message: messages.rejectedParseError(prop, value),
  80. node: decl,
  81. index,
  82. endIndex,
  83. result,
  84. ruleName,
  85. });
  86. return;
  87. }
  88. const { error } =
  89. parent && isAtRule(parent)
  90. ? forkedLexer.matchAtruleDescriptor(parent.name, prop, cssTreeValueNode)
  91. : forkedLexer.matchProperty(prop, cssTreeValueNode);
  92. if (!error) return;
  93. if (!('mismatchLength' in error)) return;
  94. const { mismatchLength, mismatchOffset, name, rawMessage } = error;
  95. if (name !== 'SyntaxMatchError') return;
  96. if (rawMessage !== 'Mismatch') return;
  97. const mismatchValue = value.slice(mismatchOffset, mismatchOffset + mismatchLength);
  98. const index = declarationValueIndex(decl) + mismatchOffset;
  99. const endIndex = index + mismatchLength;
  100. report({
  101. message: messages.rejected(prop, mismatchValue),
  102. node: decl,
  103. index,
  104. endIndex,
  105. result,
  106. ruleName,
  107. });
  108. });
  109. };
  110. };
  111. /**
  112. * TODO: This function avoids false positives because CSSTree doesn't fully support
  113. * some math functions like `clamp()` via `fork()`. In the future, it may be unnecessary.
  114. *
  115. * @see https://github.com/stylelint/stylelint/pull/6511#issuecomment-1412921062
  116. * @see https://github.com/stylelint/stylelint/issues/6635#issuecomment-1425787649
  117. *
  118. * @param {import('css-tree').CssNode} cssTreeNode
  119. * @returns {boolean}
  120. */
  121. function containsUnsupportedFunction(cssTreeNode) {
  122. return Boolean(
  123. find(
  124. cssTreeNode,
  125. (node) => node.type === 'Function' && ['clamp', 'min', 'max', 'env'].includes(node.name),
  126. ),
  127. );
  128. }
  129. rule.ruleName = ruleName;
  130. rule.messages = messages;
  131. rule.meta = meta;
  132. module.exports = rule;