index.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. const blockString = require('../../utils/blockString');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const whitespaceChecker = require('../../utils/whitespaceChecker');
  7. const { isAtRule, isRule } = require('../../utils/typeGuards');
  8. const ruleName = 'declaration-block-semicolon-newline-before';
  9. const messages = ruleMessages(ruleName, {
  10. expectedBefore: () => 'Expected newline before ";"',
  11. expectedBeforeMultiLine: () => 'Expected newline before ";" in a multi-line declaration block',
  12. rejectedBeforeMultiLine: () =>
  13. 'Unexpected whitespace before ";" in a multi-line declaration block',
  14. });
  15. const meta = {
  16. url: 'https://stylelint.io/user-guide/rules/list/declaration-block-semicolon-newline-before',
  17. };
  18. /** @type {import('stylelint').Rule} */
  19. const rule = (primary) => {
  20. const checker = whitespaceChecker('newline', primary, messages);
  21. return function (root, result) {
  22. const validOptions = validateOptions(result, ruleName, {
  23. actual: primary,
  24. possible: ['always', 'always-multi-line', 'never-multi-line'],
  25. });
  26. if (!validOptions) {
  27. return;
  28. }
  29. root.walkDecls((decl) => {
  30. const parentRule = decl.parent;
  31. if (!parentRule) throw new Error('A parent node must be present');
  32. if (!isAtRule(parentRule) && !isRule(parentRule)) {
  33. return;
  34. }
  35. if (!parentRule.raws.semicolon && parentRule.last === decl) {
  36. return;
  37. }
  38. const declString = decl.toString();
  39. checker.beforeAllowingIndentation({
  40. source: declString,
  41. index: declString.length,
  42. lineCheckStr: blockString(parentRule),
  43. err: (m) => {
  44. report({
  45. message: m,
  46. node: decl,
  47. index: decl.toString().length - 1,
  48. result,
  49. ruleName,
  50. });
  51. },
  52. });
  53. });
  54. };
  55. };
  56. rule.ruleName = ruleName;
  57. rule.messages = messages;
  58. rule.meta = meta;
  59. module.exports = rule;