12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- "use strict"
- module.exports = {
- meta: {
- type: "suggestion",
- docs: {
- description: "require error handling in callbacks",
- category: "Possible Errors",
- recommended: false,
- url:
- "https://github.com/mysticatea/eslint-plugin-node/blob/v11.1.0/docs/rules/handle-callback-err.md",
- },
- fixable: null,
- schema: [
- {
- type: "string",
- },
- ],
- messages: {
- expected: "Expected error to be handled.",
- },
- },
- create(context) {
- const errorArgument = context.options[0] || "err"
-
- function isPattern(stringToCheck) {
- const firstChar = stringToCheck[0]
- return firstChar === "^"
- }
-
- function matchesConfiguredErrorName(name) {
- if (isPattern(errorArgument)) {
- const regexp = new RegExp(errorArgument, "u")
- return regexp.test(name)
- }
- return name === errorArgument
- }
-
- function getParameters(scope) {
- return scope.variables.filter(
- variable =>
- variable.defs[0] && variable.defs[0].type === "Parameter"
- )
- }
-
- function checkForError(node) {
- const scope = context.getScope()
- const parameters = getParameters(scope)
- const firstParameter = parameters[0]
- if (
- firstParameter &&
- matchesConfiguredErrorName(firstParameter.name)
- ) {
- if (firstParameter.references.length === 0) {
- context.report({ node, messageId: "expected" })
- }
- }
- }
- return {
- FunctionDeclaration: checkForError,
- FunctionExpression: checkForError,
- ArrowFunctionExpression: checkForError,
- }
- },
- }
|