index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 'use strict';
  2. const report = require('../../utils/report');
  3. const ruleMessages = require('../../utils/ruleMessages');
  4. const validateOptions = require('../../utils/validateOptions');
  5. const ruleName = 'no-missing-end-of-source-newline';
  6. const messages = ruleMessages(ruleName, {
  7. rejected: 'Unexpected missing end-of-source newline',
  8. });
  9. const meta = {
  10. url: 'https://stylelint.io/user-guide/rules/list/no-missing-end-of-source-newline',
  11. };
  12. /** @type {import('stylelint').Rule} */
  13. const rule = (primary, _secondaryOptions, context) => {
  14. return (root, result) => {
  15. const validOptions = validateOptions(result, ruleName, { actual: primary });
  16. if (!validOptions) {
  17. return;
  18. }
  19. if (root.source == null) {
  20. throw new Error('The root node must have a source property');
  21. }
  22. // @ts-expect-error -- TS2339: Property 'inline' does not exist on type 'Source'.
  23. if (root.source.inline || root.source.lang === 'object-literal') {
  24. return;
  25. }
  26. const rootString = context.fix ? root.toString() : root.source.input.css;
  27. if (!rootString.trim() || rootString.endsWith('\n')) {
  28. return;
  29. }
  30. // Fix
  31. if (context.fix) {
  32. root.raws.after = context.newline;
  33. return;
  34. }
  35. report({
  36. message: messages.rejected,
  37. node: root,
  38. index: rootString.length - 1,
  39. result,
  40. ruleName,
  41. });
  42. };
  43. };
  44. rule.ruleName = ruleName;
  45. rule.messages = messages;
  46. rule.meta = meta;
  47. module.exports = rule;