.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / function-whitespace-after / index.js
1 "use strict";
2
3 const isWhitespace = require("../../utils/isWhitespace");
4 const report = require("../../utils/report");
5 const ruleMessages = require("../../utils/ruleMessages");
6 const styleSearch = require("style-search");
7 const validateOptions = require("../../utils/validateOptions");
8
9 const ruleName = "function-whitespace-after";
10
11 const messages = ruleMessages(ruleName, {
12   expected: 'Expected whitespace after ")"',
13   rejected: 'Unexpected whitespace after ")"'
14 });
15
16 const ACCEPTABLE_AFTER_CLOSING_PAREN = new Set([")", ",", "}", ":", undefined]);
17
18 const rule = function(expectation) {
19   return (root, result) => {
20     const validOptions = validateOptions(result, ruleName, {
21       actual: expectation,
22       possible: ["always", "never"]
23     });
24     if (!validOptions) {
25       return;
26     }
27
28     root.walkDecls(decl => {
29       const declString = decl.toString();
30
31       styleSearch(
32         {
33           source: declString,
34           target: ")",
35           functionArguments: "only"
36         },
37         match => {
38           checkClosingParen(declString, match.startIndex, decl);
39         }
40       );
41     });
42
43     function checkClosingParen(source, index, node) {
44       const nextChar = source[index + 1];
45       if (expectation === "always") {
46         // Allow for the next character to be a single empty space,
47         // another closing parenthesis, a comma, or the end of the value
48         if (nextChar === " ") {
49           return;
50         }
51         if (nextChar === "\n") {
52           return;
53         }
54         if (source.substr(index + 1, 2) === "\r\n") {
55           return;
56         }
57         if (ACCEPTABLE_AFTER_CLOSING_PAREN.has(nextChar)) {
58           return;
59         }
60         report({
61           message: messages.expected,
62           node,
63           index: index + 1,
64           result,
65           ruleName
66         });
67       } else if (expectation === "never") {
68         if (isWhitespace(nextChar)) {
69           report({
70             message: messages.rejected,
71             node,
72             index: index + 1,
73             result,
74             ruleName
75           });
76         }
77       }
78     }
79   };
80 };
81
82 rule.ruleName = ruleName;
83 rule.messages = messages;
84 module.exports = rule;