stylelintCommand.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const { assertString } = require('./validateTypes');
  3. const DISABLE_COMMAND = 'stylelint-disable';
  4. const DISABLE_LINE_COMMAND = 'stylelint-disable-line';
  5. const DISABLE_NEXT_LINE_COMMAND = 'stylelint-disable-next-line';
  6. const ENABLE_COMMAND = 'stylelint-enable';
  7. const ALL_COMMANDS = new Set([
  8. DISABLE_COMMAND,
  9. DISABLE_LINE_COMMAND,
  10. DISABLE_NEXT_LINE_COMMAND,
  11. ENABLE_COMMAND,
  12. ]);
  13. /** @typedef {import('postcss').Comment} Comment */
  14. /**
  15. * Extract a command from a given comment.
  16. *
  17. * @param {Comment} comment
  18. * @returns {string}
  19. */
  20. function extractStylelintCommand(comment) {
  21. const [command] = comment.text.split(/\s/, 1);
  22. assertString(command);
  23. return command;
  24. }
  25. /**
  26. * Tests if the given comment is a Stylelint command.
  27. *
  28. * @param {Comment} comment
  29. * @returns {boolean}
  30. */
  31. function isStylelintCommand(comment) {
  32. const command = extractStylelintCommand(comment);
  33. return command !== undefined && ALL_COMMANDS.has(command);
  34. }
  35. module.exports = {
  36. DISABLE_COMMAND,
  37. DISABLE_LINE_COMMAND,
  38. DISABLE_NEXT_LINE_COMMAND,
  39. ENABLE_COMMAND,
  40. extractStylelintCommand,
  41. isStylelintCommand,
  42. };