UNPKG

eslint-plugin-budapestian

Version:
87 lines (79 loc) 2.14 kB
const { getParameterDeclaratorName } = require("./ast-utl"); const { reportProblemIdentifiers } = require("./prefixed-pascalcase-utl"); const { isNotAnException, isValidPrefixedIdentifier, } = require("./pattern-utl"); /** * @fileoverview Enforce function parameters to adhere to a pattern * @author sverweij */ function parameterNameIsValid(pString, pPrefix) { return isValidPrefixedIdentifier(pString, pPrefix) || pString.startsWith("_"); } function getProblemParameterNames(pNode, pExceptions, pPrefix) { /* c8 ignore start */ return ( (pNode?.params ?? []) /* c8 ignore stop */ .map(getParameterDeclaratorName) .filter( (pParameterName) => !parameterNameIsValid(pParameterName, pPrefix), ) .filter(isNotAnException(pExceptions)) ); } module.exports = { meta: { type: "suggestion", docs: { description: "Enforce function parameters to adhere to a pattern", category: "Stylistic Issues", recommended: false, url: "https://sverweij.github.io/eslint-plugin-budapestian/rules/parameter-pattern", }, schema: { type: "array", items: { type: "object", properties: { exceptions: { type: "array", items: { type: "string", }, }, }, additionalProperties: false, }, minItems: 0, maxItems: 1, }, fixable: "code", }, create: (pContext) => { const lExceptions = pContext?.options[0]?.exceptions ?? []; const lPrefix = "p"; function checkParameters(pNode, pContext, pExceptions, pPrefix) { const lProblemParameterNames = getProblemParameterNames( pNode, pExceptions, pPrefix, ); reportProblemIdentifiers( pNode, pContext, lProblemParameterNames, pPrefix, ); } return { FunctionDeclaration(pNode) { checkParameters(pNode, pContext, lExceptions, lPrefix); }, ArrowFunctionExpression(pNode) { checkParameters(pNode, pContext, lExceptions, lPrefix); }, }; }, };