mirror of
https://github.com/react/react-native-devtools-frontend.git
synced 2026-10-07 04:26:32 +08:00
R=jacktfranklin@chromium.org Bug: none Change-Id: I961be18530d3ccbc4efed0c2cda9bd603b14e27b Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3372931 Reviewed-by: Jack Franklin <jacktfranklin@chromium.org> Commit-Queue: Tim Van der Lippe <tvanderlippe@chromium.org>
111 lines
2.9 KiB
JavaScript
111 lines
2.9 KiB
JavaScript
'use strict';
|
|
|
|
const declarationValueIndex = require('../../utils/declarationValueIndex');
|
|
const getDeclarationValue = require('../../utils/getDeclarationValue');
|
|
const isStandardSyntaxFunction = require('../../utils/isStandardSyntaxFunction');
|
|
const keywordSets = require('../../reference/keywordSets');
|
|
const matchesStringOrRegExp = require('../../utils/matchesStringOrRegExp');
|
|
const report = require('../../utils/report');
|
|
const ruleMessages = require('../../utils/ruleMessages');
|
|
const setDeclarationValue = require('../../utils/setDeclarationValue');
|
|
const validateOptions = require('../../utils/validateOptions');
|
|
const valueParser = require('postcss-value-parser');
|
|
const { isRegExp, isString } = require('../../utils/validateTypes');
|
|
|
|
const ruleName = 'function-name-case';
|
|
|
|
const messages = ruleMessages(ruleName, {
|
|
expected: (actual, expected) => `Expected "${actual}" to be "${expected}"`,
|
|
});
|
|
|
|
const mapLowercaseFunctionNamesToCamelCase = new Map();
|
|
|
|
for (const func of keywordSets.camelCaseFunctionNames) {
|
|
mapLowercaseFunctionNamesToCamelCase.set(func.toLowerCase(), func);
|
|
}
|
|
|
|
/** @type {import('stylelint').Rule} */
|
|
const rule = (primary, secondaryOptions, context) => {
|
|
return (root, result) => {
|
|
const validOptions = validateOptions(
|
|
result,
|
|
ruleName,
|
|
{
|
|
actual: primary,
|
|
possible: ['lower', 'upper'],
|
|
},
|
|
{
|
|
actual: secondaryOptions,
|
|
possible: {
|
|
ignoreFunctions: [isString, isRegExp],
|
|
},
|
|
optional: true,
|
|
},
|
|
);
|
|
|
|
if (!validOptions) {
|
|
return;
|
|
}
|
|
|
|
root.walkDecls((decl) => {
|
|
let needFix = false;
|
|
const parsed = valueParser(getDeclarationValue(decl));
|
|
|
|
parsed.walk((node) => {
|
|
if (node.type !== 'function' || !isStandardSyntaxFunction(node)) {
|
|
return;
|
|
}
|
|
|
|
const functionName = node.value;
|
|
const functionNameLowerCase = functionName.toLowerCase();
|
|
|
|
const ignoreFunctions = (secondaryOptions && secondaryOptions.ignoreFunctions) || [];
|
|
|
|
if (ignoreFunctions.length > 0 && matchesStringOrRegExp(functionName, ignoreFunctions)) {
|
|
return;
|
|
}
|
|
|
|
let expectedFunctionName = null;
|
|
|
|
if (
|
|
primary === 'lower' &&
|
|
mapLowercaseFunctionNamesToCamelCase.has(functionNameLowerCase)
|
|
) {
|
|
expectedFunctionName = mapLowercaseFunctionNamesToCamelCase.get(functionNameLowerCase);
|
|
} else if (primary === 'lower') {
|
|
expectedFunctionName = functionNameLowerCase;
|
|
} else {
|
|
expectedFunctionName = functionName.toUpperCase();
|
|
}
|
|
|
|
if (functionName === expectedFunctionName) {
|
|
return;
|
|
}
|
|
|
|
if (context.fix) {
|
|
needFix = true;
|
|
node.value = expectedFunctionName;
|
|
|
|
return;
|
|
}
|
|
|
|
report({
|
|
message: messages.expected(functionName, expectedFunctionName),
|
|
node: decl,
|
|
index: declarationValueIndex(decl) + node.sourceIndex,
|
|
result,
|
|
ruleName,
|
|
});
|
|
});
|
|
|
|
if (context.fix && needFix) {
|
|
setDeclarationValue(decl, parsed.toString());
|
|
}
|
|
});
|
|
};
|
|
};
|
|
|
|
rule.ruleName = ruleName;
|
|
rule.messages = messages;
|
|
module.exports = rule;
|