index.js 1.6 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-empty-first-line';
  6. const noEmptyFirstLineTest = /^\s*[\r\n]/;
  7. const messages = ruleMessages(ruleName, {
  8. rejected: 'Unexpected empty line',
  9. });
  10. const meta = {
  11. url: 'https://stylelint.io/user-guide/rules/list/no-empty-first-line',
  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. // @ts-expect-error -- TS2339: Property 'inline' does not exist on type 'Source'. Property 'lang' does not exist on type 'Source'.
  18. if (!validOptions || root.source.inline || root.source.lang === 'object-literal') {
  19. return;
  20. }
  21. const rootString = context.fix ? root.toString() : (root.source && root.source.input.css) || '';
  22. if (!rootString.trim()) {
  23. return;
  24. }
  25. if (noEmptyFirstLineTest.test(rootString)) {
  26. if (context.fix) {
  27. if (root.first == null) {
  28. throw new Error('The root node must have the first node.');
  29. }
  30. if (root.first.raws.before == null) {
  31. throw new Error('The first node must have spaces before.');
  32. }
  33. root.first.raws.before = root.first.raws.before.trimStart();
  34. return;
  35. }
  36. report({
  37. message: messages.rejected,
  38. node: root,
  39. result,
  40. ruleName,
  41. });
  42. }
  43. };
  44. };
  45. rule.ruleName = ruleName;
  46. rule.messages = messages;
  47. rule.meta = meta;
  48. module.exports = rule;