index.mjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { isConfigurationComment } from '../../utils/configurationComment.mjs';
  2. import report from '../../utils/report.mjs';
  3. import ruleMessages from '../../utils/ruleMessages.mjs';
  4. import validateOptions from '../../utils/validateOptions.mjs';
  5. const ruleName = 'no-empty-source';
  6. const messages = ruleMessages(ruleName, {
  7. rejected: 'Unexpected empty source',
  8. });
  9. const meta = {
  10. url: 'https://stylelint.io/user-guide/rules/no-empty-source',
  11. };
  12. /** @type {import('stylelint').CoreRules[ruleName]} */
  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. // after a fix has been applied root.toString() may differ from root.source.input.css
  20. // i.e. root.source.input.css remains unchanged after a fix
  21. const rootString = context.fix ? root.toString() : (root.source && root.source.input.css) || '';
  22. let hasNotableChild =
  23. Boolean(rootString.trim()) &&
  24. root.nodes.some((child) => {
  25. if (isConfigurationComment(child, context.configurationComment)) return false;
  26. return true;
  27. });
  28. if (!hasNotableChild) {
  29. // Assume a case when a non-standard syntax is used, such as JSX. See #8547.
  30. if (root.nodes.length === 0 && Boolean(root.raws.after?.trim())) {
  31. hasNotableChild = true;
  32. }
  33. }
  34. if (hasNotableChild) {
  35. return;
  36. }
  37. report({
  38. message: messages.rejected,
  39. messageArgs: [],
  40. node: root,
  41. result,
  42. ruleName,
  43. });
  44. };
  45. };
  46. rule.ruleName = ruleName;
  47. rule.messages = messages;
  48. rule.meta = meta;
  49. export default rule;