blob: a4a237035b5937208550a6c3efc253001276a29e [file] [log] [blame]
Tim van der Lippe2b4a9df2021-11-08 12:58:12 +00001// @ts-nocheck
2
Mathias Bynens79e2cf02020-05-29 16:46:17 +02003'use strict';
4
Mathias Bynens79e2cf02020-05-29 16:46:17 +02005const declarationValueIndex = require('../utils/declarationValueIndex');
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +00006const getDeclarationValue = require('../utils/getDeclarationValue');
Mathias Bynens79e2cf02020-05-29 16:46:17 +02007const isStandardSyntaxFunction = require('../utils/isStandardSyntaxFunction');
8const report = require('../utils/report');
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +00009const setDeclarationValue = require('../utils/setDeclarationValue');
Mathias Bynens79e2cf02020-05-29 16:46:17 +020010const valueParser = require('postcss-value-parser');
11
Tim van der Lippe2b4a9df2021-11-08 12:58:12 +000012module.exports = function (opts) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020013 opts.root.walkDecls((decl) => {
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +000014 const declValue = getDeclarationValue(decl);
Mathias Bynens79e2cf02020-05-29 16:46:17 +020015
16 let hasFixed;
17 const parsedValue = valueParser(declValue);
18
19 parsedValue.walk((valueNode) => {
20 if (valueNode.type !== 'function') {
21 return;
22 }
23
24 if (!isStandardSyntaxFunction(valueNode)) {
25 return;
26 }
27
28 // Ignore `url()` arguments, which may contain data URIs or other funky stuff
29 if (valueNode.value.toLowerCase() === 'url') {
30 return;
31 }
32
33 const argumentStrings = valueNode.nodes.map((node) => valueParser.stringify(node));
34
35 const functionArguments = (() => {
36 // Remove function name and parens
37 let result = valueNode.before + argumentStrings.join('') + valueNode.after;
38
39 // 1. Remove comments including preceding whitespace (when only succeeded by whitespace)
40 // 2. Remove all other comments, but leave adjacent whitespace intact
41 result = result.replace(/( *\/(\*.*\*\/(?!\S)|\/.*)|(\/(\*.*\*\/|\/.*)))/, '');
42
43 return result;
44 })();
45
46 /**
47 * Gets the index of the comma for checking.
Tim van der Lippe2b4a9df2021-11-08 12:58:12 +000048 * @param {Node} commaNode The comma node
Mathias Bynens79e2cf02020-05-29 16:46:17 +020049 * @param {number} nodeIndex The index of the comma node
50 * @returns {number} The index of the comma for checking
51 */
Tim van der Lippe2b4a9df2021-11-08 12:58:12 +000052 function getCommaCheckIndex(commaNode, nodeIndex) {
Mathias Bynens79e2cf02020-05-29 16:46:17 +020053 let commaBefore =
54 valueNode.before + argumentStrings.slice(0, nodeIndex).join('') + commaNode.before;
55
56 // 1. Remove comments including preceding whitespace (when only succeeded by whitespace)
57 // 2. Remove all other comments, but leave adjacent whitespace intact
58 commaBefore = commaBefore.replace(/( *\/(\*.*\*\/(?!\S)|\/.*)|(\/(\*.*\*\/|\/.*)))/, '');
59
60 return commaBefore.length;
Tim van der Lippe2b4a9df2021-11-08 12:58:12 +000061 }
Mathias Bynens79e2cf02020-05-29 16:46:17 +020062
63 const commaDataList = [];
64
65 valueNode.nodes.forEach((node, nodeIndex) => {
66 if (node.type !== 'div' || node.value !== ',') {
67 return;
68 }
69
70 const checkIndex = getCommaCheckIndex(node, nodeIndex);
71
72 commaDataList.push({
73 commaNode: node,
74 checkIndex,
75 nodeIndex,
76 });
77 });
78
79 for (const { commaNode, checkIndex, nodeIndex } of commaDataList) {
80 opts.locationChecker({
81 source: functionArguments,
82 index: checkIndex,
83 err: (message) => {
84 const index =
85 declarationValueIndex(decl) + commaNode.sourceIndex + commaNode.before.length;
86
87 if (opts.fix && opts.fix(commaNode, nodeIndex, valueNode.nodes)) {
88 hasFixed = true;
89
90 return;
91 }
92
93 report({
94 index,
95 message,
96 node: decl,
97 result: opts.result,
98 ruleName: opts.checkedRuleName,
99 });
100 },
101 });
102 }
103 });
104
105 if (hasFixed) {
Tim van der Lippe0a9b84d2021-03-24 11:53:15 +0000106 setDeclarationValue(decl, parsedValue.toString());
Mathias Bynens79e2cf02020-05-29 16:46:17 +0200107 }
108 });
109};