UNPKG

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