index.js 1.4 KB

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