index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/no-empty-first-line',
  12. fixable: true,
  13. deprecated: true,
  14. };
  15. /** @type {import('stylelint').Rule} */
  16. const rule = (primary, _secondaryOptions, context) => {
  17. return (root, result) => {
  18. const validOptions = validateOptions(result, ruleName, { actual: primary });
  19. // @ts-expect-error -- TS2339: Property 'inline' does not exist on type 'Source'. Property 'lang' does not exist on type 'Source'.
  20. if (!validOptions || root.source.inline || root.source.lang === 'object-literal') {
  21. return;
  22. }
  23. const rootString = context.fix ? root.toString() : (root.source && root.source.input.css) || '';
  24. if (!rootString.trim()) {
  25. return;
  26. }
  27. if (noEmptyFirstLineTest.test(rootString)) {
  28. if (context.fix) {
  29. if (root.first == null) {
  30. throw new Error('The root node must have the first node.');
  31. }
  32. if (root.first.raws.before == null) {
  33. throw new Error('The first node must have spaces before.');
  34. }
  35. root.first.raws.before = root.first.raws.before.trimStart();
  36. return;
  37. }
  38. report({
  39. message: messages.rejected,
  40. node: root,
  41. result,
  42. ruleName,
  43. });
  44. }
  45. };
  46. };
  47. rule.ruleName = ruleName;
  48. rule.messages = messages;
  49. rule.meta = meta;
  50. module.exports = rule;