index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. 'use strict';
  2. const getImportantPosition = require('../../utils/getImportantPosition');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const { assert } = require('../../utils/validateTypes');
  7. const ruleName = 'declaration-no-important';
  8. const messages = ruleMessages(ruleName, {
  9. rejected: 'Unexpected !important',
  10. });
  11. const meta = {
  12. url: 'https://stylelint.io/user-guide/rules/list/declaration-no-important',
  13. };
  14. /** @type {import('stylelint').Rule} */
  15. const rule = (primary) => {
  16. return (root, result) => {
  17. const validOptions = validateOptions(result, ruleName, { actual: primary });
  18. if (!validOptions) {
  19. return;
  20. }
  21. root.walkDecls((decl) => {
  22. if (!decl.important) {
  23. return;
  24. }
  25. const pos = getImportantPosition(decl.toString());
  26. assert(pos);
  27. report({
  28. message: messages.rejected,
  29. node: decl,
  30. index: pos.index,
  31. result,
  32. ruleName,
  33. });
  34. });
  35. };
  36. };
  37. rule.ruleName = ruleName;
  38. rule.messages = messages;
  39. rule.meta = meta;
  40. module.exports = rule;