UNPKG

1.31 kBJavaScriptView Raw
1// @ts-nocheck
2
3'use strict';
4
5const atRuleParamIndex = require('../utils/atRuleParamIndex');
6const report = require('../utils/report');
7const styleSearch = require('style-search');
8
9module.exports = function (opts) {
10 opts.root.walkAtRules(/^media$/i, (atRule) => {
11 const params = atRule.raws.params ? atRule.raws.params.raw : atRule.params;
12
13 styleSearch({ source: params, target: ',' }, (match) => {
14 let index = match.startIndex;
15
16 if (opts.allowTrailingComments) {
17 // if there is a comment on the same line at after the comma, check the space after the comment.
18 let execResult;
19
20 while ((execResult = /^[^\S\r\n]*\/\*([\s\S]*?)\*\//.exec(params.slice(index + 1)))) {
21 index += execResult[0].length;
22 }
23
24 if ((execResult = /^([^\S\r\n]*\/\/([\s\S]*?))\r?\n/.exec(params.slice(index + 1)))) {
25 index += execResult[1].length;
26 }
27 }
28
29 checkComma(params, index, atRule);
30 });
31 });
32
33 function checkComma(source, index, node) {
34 opts.locationChecker({
35 source,
36 index,
37 err: (m) => {
38 const commaIndex = index + atRuleParamIndex(node);
39
40 if (opts.fix && opts.fix(node, commaIndex)) {
41 return;
42 }
43
44 report({
45 message: m,
46 node,
47 index: commaIndex,
48 result: opts.result,
49 ruleName: opts.checkedRuleName,
50 });
51 },
52 });
53 }
54};