UNPKG

1.49 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 (
19 (execResult = /^[^\S\r\n]*\/\*([\s\S]*?)\*\//.exec(
20 params.slice(index + 1)
21 ))
22 ) {
23 index += execResult[0].length;
24 }
25
26 if (
27 (execResult = /^([^\S\r\n]*\/\/([\s\S]*?))\r?\n/.exec(
28 params.slice(index + 1)
29 ))
30 ) {
31 index += execResult[1].length;
32 }
33 }
34
35 checkComma(params, index, atRule);
36 });
37 });
38
39 function checkComma(source, index, node) {
40 opts.locationChecker({
41 source,
42 index,
43 err: m => {
44 const commaIndex = index + atRuleParamIndex(node);
45
46 if (opts.fix && opts.fix(node, commaIndex)) {
47 return;
48 }
49
50 report({
51 message: m,
52 node,
53 index: commaIndex,
54 result: opts.result,
55 ruleName: opts.checkedRuleName
56 });
57 }
58 });
59 }
60};