index.js 2.8 KB

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