.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / stylelint / lib / rules / function-name-case / index.js
1 "use strict";
2
3 const _ = require("lodash");
4 const declarationValueIndex = require("../../utils/declarationValueIndex");
5 const isStandardSyntaxFunction = require("../../utils/isStandardSyntaxFunction");
6 const keywordSets = require("../../reference/keywordSets");
7 const matchesStringOrRegExp = require("../../utils/matchesStringOrRegExp");
8 const report = require("../../utils/report");
9 const ruleMessages = require("../../utils/ruleMessages");
10 const validateOptions = require("../../utils/validateOptions");
11 const valueParser = require("postcss-value-parser");
12
13 const ruleName = "function-name-case";
14
15 const messages = ruleMessages(ruleName, {
16   expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`
17 });
18
19 const mapLowercaseFunctionNamesToCamelCase = new Map();
20 keywordSets.camelCaseFunctionNames.forEach(func => {
21   mapLowercaseFunctionNamesToCamelCase.set(func.toLowerCase(), func);
22 });
23
24 const rule = function(expectation, options) {
25   return (root, result) => {
26     const validOptions = validateOptions(
27       result,
28       ruleName,
29       {
30         actual: expectation,
31         possible: ["lower", "upper"]
32       },
33       {
34         actual: options,
35         possible: {
36           ignoreFunctions: [_.isString]
37         },
38         optional: true
39       }
40     );
41     if (!validOptions) {
42       return;
43     }
44
45     root.walkDecls(decl => {
46       const value = decl.value;
47
48       valueParser(value).walk(function(node) {
49         if (node.type !== "function" || !isStandardSyntaxFunction(node)) {
50           return;
51         }
52
53         const functionName = node.value;
54         const functionNameLowerCase = functionName.toLowerCase();
55
56         const ignoreFunctions = (options && options.ignoreFunctions) || [];
57
58         if (
59           ignoreFunctions.length > 0 &&
60           matchesStringOrRegExp(functionName, ignoreFunctions)
61         ) {
62           return;
63         }
64
65         let expectedFunctionName = null;
66
67         if (
68           expectation === "lower" &&
69           mapLowercaseFunctionNamesToCamelCase.has(functionNameLowerCase)
70         ) {
71           expectedFunctionName = mapLowercaseFunctionNamesToCamelCase.get(
72             functionNameLowerCase
73           );
74         } else if (expectation === "lower") {
75           expectedFunctionName = functionNameLowerCase;
76         } else {
77           expectedFunctionName = functionName.toUpperCase();
78         }
79
80         if (functionName === expectedFunctionName) {
81           return;
82         }
83
84         report({
85           message: messages.expected(functionName, expectedFunctionName),
86           node: decl,
87           index: declarationValueIndex(decl) + node.sourceIndex,
88           result,
89           ruleName
90         });
91       });
92     });
93   };
94 };
95
96 rule.ruleName = ruleName;
97 rule.messages = messages;
98 module.exports = rule;