.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / function-parentheses-space-inside / index.js
1 "use strict";
2
3 const declarationValueIndex = require("../../utils/declarationValueIndex");
4 const isSingleLineString = require("../../utils/isSingleLineString");
5 const isStandardSyntaxFunction = require("../../utils/isStandardSyntaxFunction");
6 const report = require("../../utils/report");
7 const ruleMessages = require("../../utils/ruleMessages");
8 const validateOptions = require("../../utils/validateOptions");
9 const valueParser = require("postcss-value-parser");
10
11 const ruleName = "function-parentheses-space-inside";
12
13 const messages = ruleMessages(ruleName, {
14   expectedOpening: 'Expected single space after "("',
15   rejectedOpening: 'Unexpected whitespace after "("',
16   expectedClosing: 'Expected single space before ")"',
17   rejectedClosing: 'Unexpected whitespace before ")"',
18   expectedOpeningSingleLine:
19     'Expected single space after "(" in a single-line function',
20   rejectedOpeningSingleLine:
21     'Unexpected whitespace after "(" in a single-line function',
22   expectedClosingSingleLine:
23     'Expected single space before ")" in a single-line function',
24   rejectedClosingSingleLine:
25     'Unexpected whitespace before ")" in a single-line function'
26 });
27
28 const rule = function(expectation) {
29   return (root, result) => {
30     const validOptions = validateOptions(result, ruleName, {
31       actual: expectation,
32       possible: ["always", "never", "always-single-line", "never-single-line"]
33     });
34     if (!validOptions) {
35       return;
36     }
37
38     root.walkDecls(decl => {
39       if (decl.value.indexOf("(") === -1) {
40         return;
41       }
42
43       valueParser(decl.value).walk(valueNode => {
44         if (valueNode.type !== "function") {
45           return;
46         }
47
48         if (!isStandardSyntaxFunction(valueNode)) {
49           return;
50         }
51
52         // Ignore function without parameters
53         if (!valueNode.nodes.length) {
54           return;
55         }
56
57         const functionString = valueParser.stringify(valueNode);
58         const isSingleLine = isSingleLineString(functionString);
59
60         // Check opening ...
61
62         const openingIndex = valueNode.sourceIndex + valueNode.value.length + 1;
63
64         if (expectation === "always" && valueNode.before !== " ") {
65           complain(messages.expectedOpening, openingIndex);
66         }
67
68         if (expectation === "never" && valueNode.before !== "") {
69           complain(messages.rejectedOpening, openingIndex);
70         }
71
72         if (
73           isSingleLine &&
74           expectation === "always-single-line" &&
75           valueNode.before !== " "
76         ) {
77           complain(messages.expectedOpeningSingleLine, openingIndex);
78         }
79
80         if (
81           isSingleLine &&
82           expectation === "never-single-line" &&
83           valueNode.before !== ""
84         ) {
85           complain(messages.rejectedOpeningSingleLine, openingIndex);
86         }
87
88         // Check closing ...
89
90         const closingIndex = valueNode.sourceIndex + functionString.length - 2;
91
92         if (expectation === "always" && valueNode.after !== " ") {
93           complain(messages.expectedClosing, closingIndex);
94         }
95
96         if (expectation === "never" && valueNode.after !== "") {
97           complain(messages.rejectedClosing, closingIndex);
98         }
99
100         if (
101           isSingleLine &&
102           expectation === "always-single-line" &&
103           valueNode.after !== " "
104         ) {
105           complain(messages.expectedClosingSingleLine, closingIndex);
106         }
107
108         if (
109           isSingleLine &&
110           expectation === "never-single-line" &&
111           valueNode.after !== ""
112         ) {
113           complain(messages.rejectedClosingSingleLine, closingIndex);
114         }
115       });
116
117       function complain(message, offset) {
118         report({
119           ruleName,
120           result,
121           message,
122           node: decl,
123           index: declarationValueIndex(decl) + offset
124         });
125       }
126     });
127   };
128 };
129
130 rule.ruleName = ruleName;
131 rule.messages = messages;
132 module.exports = rule;