index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 'use strict';
  2. const atRuleParamIndex = require('../../utils/atRuleParamIndex');
  3. const report = require('../../utils/report');
  4. const ruleMessages = require('../../utils/ruleMessages');
  5. const validateOptions = require('../../utils/validateOptions');
  6. const valueParser = require('postcss-value-parser');
  7. const ruleName = 'media-feature-parentheses-space-inside';
  8. const messages = ruleMessages(ruleName, {
  9. expectedOpening: 'Expected single space after "("',
  10. rejectedOpening: 'Unexpected whitespace after "("',
  11. expectedClosing: 'Expected single space before ")"',
  12. rejectedClosing: 'Unexpected whitespace before ")"',
  13. });
  14. const meta = {
  15. url: 'https://stylelint.io/user-guide/rules/list/media-feature-parentheses-space-inside',
  16. };
  17. /** @type {import('stylelint').Rule} */
  18. const rule = (primary, _secondaryOptions, context) => {
  19. return (root, result) => {
  20. const validOptions = validateOptions(result, ruleName, {
  21. actual: primary,
  22. possible: ['always', 'never'],
  23. });
  24. if (!validOptions) {
  25. return;
  26. }
  27. root.walkAtRules(/^media$/i, (atRule) => {
  28. // If there are comments in the params, the complete string
  29. // will be at atRule.raws.params.raw
  30. const params = (atRule.raws.params && atRule.raws.params.raw) || atRule.params;
  31. const indexBoost = atRuleParamIndex(atRule);
  32. /** @type {Array<{ message: string, index: number }>} */
  33. const problems = [];
  34. const parsedParams = valueParser(params).walk((node) => {
  35. if (node.type === 'function') {
  36. const len = valueParser.stringify(node).length;
  37. if (primary === 'never') {
  38. if (/[ \t]/.test(node.before)) {
  39. if (context.fix) node.before = '';
  40. problems.push({
  41. message: messages.rejectedOpening,
  42. index: node.sourceIndex + 1 + indexBoost,
  43. });
  44. }
  45. if (/[ \t]/.test(node.after)) {
  46. if (context.fix) node.after = '';
  47. problems.push({
  48. message: messages.rejectedClosing,
  49. index: node.sourceIndex - 2 + len + indexBoost,
  50. });
  51. }
  52. } else if (primary === 'always') {
  53. if (node.before === '') {
  54. if (context.fix) node.before = ' ';
  55. problems.push({
  56. message: messages.expectedOpening,
  57. index: node.sourceIndex + 1 + indexBoost,
  58. });
  59. }
  60. if (node.after === '') {
  61. if (context.fix) node.after = ' ';
  62. problems.push({
  63. message: messages.expectedClosing,
  64. index: node.sourceIndex - 2 + len + indexBoost,
  65. });
  66. }
  67. }
  68. }
  69. });
  70. if (problems.length) {
  71. if (context.fix) {
  72. atRule.params = parsedParams.toString();
  73. return;
  74. }
  75. for (const err of problems) {
  76. report({
  77. message: err.message,
  78. node: atRule,
  79. index: err.index,
  80. result,
  81. ruleName,
  82. });
  83. }
  84. }
  85. });
  86. };
  87. };
  88. rule.ruleName = ruleName;
  89. rule.messages = messages;
  90. rule.meta = meta;
  91. module.exports = rule;