index.js 2.8 KB

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