index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const report = require('../../utils/report');
  3. const ruleMessages = require('../../utils/ruleMessages');
  4. const validateOptions = require('../../utils/validateOptions');
  5. const ruleName = 'unicode-bom';
  6. const messages = ruleMessages(ruleName, {
  7. expected: 'Expected Unicode BOM',
  8. rejected: 'Unexpected Unicode BOM',
  9. });
  10. const meta = {
  11. url: 'https://stylelint.io/user-guide/rules/unicode-bom',
  12. };
  13. /** @type {import('stylelint').Rule} */
  14. const rule = (primary) => {
  15. return (root, result) => {
  16. const validOptions = validateOptions(result, ruleName, {
  17. actual: primary,
  18. possible: ['always', 'never'],
  19. });
  20. if (
  21. !validOptions ||
  22. !root.source ||
  23. // @ts-expect-error -- TS2339: Property 'inline' does not exist on type 'Source'.
  24. root.source.inline ||
  25. // @ts-expect-error -- TS2339: Property 'lang' does not exist on type 'Source'.
  26. root.source.lang === 'object-literal' ||
  27. // Ignore HTML documents
  28. // @ts-expect-error -- TS2339: Property 'document' does not exist on type 'Root'.
  29. root.document !== undefined
  30. ) {
  31. return;
  32. }
  33. const { hasBOM } = root.source.input;
  34. if (primary === 'always' && !hasBOM) {
  35. report({
  36. result,
  37. ruleName,
  38. message: messages.expected,
  39. node: root,
  40. line: 1,
  41. });
  42. }
  43. if (primary === 'never' && hasBOM) {
  44. report({
  45. result,
  46. ruleName,
  47. message: messages.rejected,
  48. node: root,
  49. line: 1,
  50. });
  51. }
  52. };
  53. };
  54. rule.ruleName = ruleName;
  55. rule.messages = messages;
  56. rule.meta = meta;
  57. module.exports = rule;