index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. 'use strict';
  2. const valueParser = require('postcss-value-parser');
  3. const getDeclarationValue = require('../../utils/getDeclarationValue');
  4. const optionsMatches = require('../../utils/optionsMatches');
  5. const report = require('../../utils/report');
  6. const ruleMessages = require('../../utils/ruleMessages');
  7. const validateOptions = require('../../utils/validateOptions');
  8. const { isRegExp, isString } = require('../../utils/validateTypes');
  9. const ruleName = 'annotation-no-unknown';
  10. const messages = ruleMessages(ruleName, {
  11. rejected: (annotation) => `Unexpected unknown annotation "${annotation}"`,
  12. });
  13. const meta = {
  14. url: 'https://stylelint.io/user-guide/rules/annotation-no-unknown',
  15. };
  16. /** @type {import('stylelint').Rule} */
  17. const rule = (primary, secondaryOptions) => {
  18. return (root, result) => {
  19. const validOptions = validateOptions(
  20. result,
  21. ruleName,
  22. { actual: primary },
  23. {
  24. actual: secondaryOptions,
  25. possible: {
  26. ignoreAnnotations: [isString, isRegExp],
  27. },
  28. optional: true,
  29. },
  30. );
  31. if (!validOptions) {
  32. return;
  33. }
  34. root.walkDecls(checkStatement);
  35. /**
  36. * @param {import('postcss').Declaration} decl
  37. */
  38. function checkStatement(decl) {
  39. if (decl.important) return;
  40. if (!decl.value.includes('!')) return;
  41. const parsedValue = valueParser(getDeclarationValue(decl));
  42. parsedValue.walk((node) => {
  43. if (!isAnnotation(node)) return;
  44. const value = node.value;
  45. const tokenValue = value.slice(1);
  46. if (optionsMatches(secondaryOptions, 'ignoreAnnotations', tokenValue)) {
  47. return;
  48. }
  49. report({
  50. message: messages.rejected(value),
  51. node: decl,
  52. result,
  53. ruleName,
  54. word: value,
  55. });
  56. });
  57. }
  58. /**
  59. * @param {valueParser.Node} node
  60. */
  61. function isAnnotation(node) {
  62. return node.type === 'word' && node.value.startsWith('!');
  63. }
  64. };
  65. };
  66. rule.ruleName = ruleName;
  67. rule.messages = messages;
  68. rule.meta = meta;
  69. module.exports = rule;