configurationComment.cjs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // NOTICE: This file is generated by Rollup. To modify it,
  2. // please instead edit the ESM counterpart and rebuild with Rollup (npm run build).
  3. 'use strict';
  4. const validateTypes = require('./validateTypes.cjs');
  5. const typeGuards = require('./typeGuards.cjs');
  6. const DISABLE_COMMAND = '-disable';
  7. const DISABLE_LINE_COMMAND = '-disable-line';
  8. const DISABLE_NEXT_LINE_COMMAND = '-disable-next-line';
  9. const ENABLE_COMMAND = '-enable';
  10. const ALL_COMMANDS = new Set([
  11. DISABLE_COMMAND,
  12. DISABLE_LINE_COMMAND,
  13. DISABLE_NEXT_LINE_COMMAND,
  14. ENABLE_COMMAND,
  15. ]);
  16. const DEFAULT_CONFIGURATION_COMMENT = 'stylelint';
  17. /**
  18. * Extract a command from a given comment.
  19. *
  20. * @param {string} commentText
  21. * @param {string} [configurationComment]
  22. * @returns {string}
  23. */
  24. function extractConfigurationComment(
  25. commentText,
  26. configurationComment = DEFAULT_CONFIGURATION_COMMENT,
  27. ) {
  28. if (!commentText) return commentText;
  29. const [command] = commentText.split(/\s/, 1);
  30. validateTypes.assertString(command);
  31. return command.replace(configurationComment, '');
  32. }
  33. /**
  34. * Tests if the given comment is a Stylelint command.
  35. *
  36. * @param {string | import('postcss').Node} textOrNode
  37. * @param {string} [configurationComment]
  38. * @returns {boolean}
  39. */
  40. function isConfigurationComment(
  41. textOrNode,
  42. configurationComment = DEFAULT_CONFIGURATION_COMMENT,
  43. ) {
  44. const commentText = validateTypes.isString(textOrNode)
  45. ? textOrNode
  46. : typeGuards.isComment(textOrNode)
  47. ? textOrNode.text
  48. : undefined;
  49. if (!commentText) return false;
  50. const command = extractConfigurationComment(commentText, configurationComment);
  51. return command !== undefined && ALL_COMMANDS.has(command);
  52. }
  53. /**
  54. * Get full stylelint command
  55. *
  56. * @param {string} command
  57. * @param {string} [configurationComment]
  58. * @returns {string}
  59. */
  60. function getConfigurationComment(
  61. command,
  62. configurationComment = DEFAULT_CONFIGURATION_COMMENT,
  63. ) {
  64. return `${configurationComment}${command}`;
  65. }
  66. exports.DEFAULT_CONFIGURATION_COMMENT = DEFAULT_CONFIGURATION_COMMENT;
  67. exports.DISABLE_COMMAND = DISABLE_COMMAND;
  68. exports.DISABLE_LINE_COMMAND = DISABLE_LINE_COMMAND;
  69. exports.DISABLE_NEXT_LINE_COMMAND = DISABLE_NEXT_LINE_COMMAND;
  70. exports.ENABLE_COMMAND = ENABLE_COMMAND;
  71. exports.extractConfigurationComment = extractConfigurationComment;
  72. exports.getConfigurationComment = getConfigurationComment;
  73. exports.isConfigurationComment = isConfigurationComment;