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");
13 const ruleName = "function-name-case";
15 const messages = ruleMessages(ruleName, {
16 expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`
19 const mapLowercaseFunctionNamesToCamelCase = new Map();
20 keywordSets.camelCaseFunctionNames.forEach(func => {
21 mapLowercaseFunctionNamesToCamelCase.set(func.toLowerCase(), func);
24 const rule = function(expectation, options) {
25 return (root, result) => {
26 const validOptions = validateOptions(
31 possible: ["lower", "upper"]
36 ignoreFunctions: [_.isString]
45 root.walkDecls(decl => {
46 const value = decl.value;
48 valueParser(value).walk(function(node) {
49 if (node.type !== "function" || !isStandardSyntaxFunction(node)) {
53 const functionName = node.value;
54 const functionNameLowerCase = functionName.toLowerCase();
56 const ignoreFunctions = (options && options.ignoreFunctions) || [];
59 ignoreFunctions.length > 0 &&
60 matchesStringOrRegExp(functionName, ignoreFunctions)
65 let expectedFunctionName = null;
68 expectation === "lower" &&
69 mapLowercaseFunctionNamesToCamelCase.has(functionNameLowerCase)
71 expectedFunctionName = mapLowercaseFunctionNamesToCamelCase.get(
74 } else if (expectation === "lower") {
75 expectedFunctionName = functionNameLowerCase;
77 expectedFunctionName = functionName.toUpperCase();
80 if (functionName === expectedFunctionName) {
85 message: messages.expected(functionName, expectedFunctionName),
87 index: declarationValueIndex(decl) + node.sourceIndex,
96 rule.ruleName = ruleName;
97 rule.messages = messages;
98 module.exports = rule;