| |
| |
| |
| |
| |
| "use strict"; |
|
|
| |
| |
| |
|
|
| const astUtils = require("./utils/ast-utils"); |
|
|
| |
| |
| |
|
|
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| function isFunctionName(variable) { |
| return variable && variable.defs[0].type === "FunctionName"; |
| } |
|
|
| |
| |
| |
|
|
| |
| module.exports = { |
| meta: { |
| type: "suggestion", |
|
|
| defaultOptions: ["always", {}], |
|
|
| docs: { |
| description: "Require or disallow named `function` expressions", |
| recommended: false, |
| url: "https://eslint.org/docs/latest/rules/func-names", |
| }, |
|
|
| schema: { |
| definitions: { |
| value: { |
| enum: ["always", "as-needed", "never"], |
| }, |
| }, |
| type: "array", |
| items: [ |
| { |
| $ref: "#/definitions/value", |
| }, |
| { |
| type: "object", |
| properties: { |
| generators: { |
| $ref: "#/definitions/value", |
| }, |
| }, |
| additionalProperties: false, |
| }, |
| ], |
| additionalItems: false, |
| }, |
|
|
| messages: { |
| unnamed: "Unexpected unnamed {{name}}.", |
| named: "Unexpected named {{name}}.", |
| }, |
| }, |
|
|
| create(context) { |
| const sourceCode = context.sourceCode; |
|
|
| |
| |
| |
| |
| |
| function getConfigForNode(node) { |
| if (node.generator && context.options[1].generators) { |
| return context.options[1].generators; |
| } |
|
|
| return context.options[0]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function isObjectOrClassMethod(node) { |
| const parent = node.parent; |
|
|
| return ( |
| parent.type === "MethodDefinition" || |
| (parent.type === "Property" && |
| (parent.method || |
| parent.kind === "get" || |
| parent.kind === "set")) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function hasInferredName(node) { |
| const parent = node.parent; |
|
|
| return ( |
| isObjectOrClassMethod(node) || |
| (parent.type === "VariableDeclarator" && |
| parent.id.type === "Identifier" && |
| parent.init === node) || |
| (parent.type === "Property" && parent.value === node) || |
| (parent.type === "PropertyDefinition" && |
| parent.value === node) || |
| (parent.type === "AssignmentExpression" && |
| parent.left.type === "Identifier" && |
| parent.right === node) || |
| (parent.type === "AssignmentPattern" && |
| parent.left.type === "Identifier" && |
| parent.right === node) |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| function reportUnexpectedUnnamedFunction(node) { |
| context.report({ |
| node, |
| messageId: "unnamed", |
| loc: astUtils.getFunctionHeadLoc(node, sourceCode), |
| data: { name: astUtils.getFunctionNameWithKind(node) }, |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| function reportUnexpectedNamedFunction(node) { |
| context.report({ |
| node, |
| messageId: "named", |
| loc: astUtils.getFunctionHeadLoc(node, sourceCode), |
| data: { name: astUtils.getFunctionNameWithKind(node) }, |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| function handleFunction(node) { |
| |
| const nameVar = sourceCode.getDeclaredVariables(node)[0]; |
|
|
| if (isFunctionName(nameVar) && nameVar.references.length > 0) { |
| return; |
| } |
|
|
| const hasName = Boolean(node.id && node.id.name); |
| const config = getConfigForNode(node); |
|
|
| if (config === "never") { |
| if (hasName && node.type !== "FunctionDeclaration") { |
| reportUnexpectedNamedFunction(node); |
| } |
| } else if (config === "as-needed") { |
| if (!hasName && !hasInferredName(node)) { |
| reportUnexpectedUnnamedFunction(node); |
| } |
| } else { |
| if (!hasName && !isObjectOrClassMethod(node)) { |
| reportUnexpectedUnnamedFunction(node); |
| } |
| } |
| } |
|
|
| return { |
| "FunctionExpression:exit": handleFunction, |
| "ExportDefaultDeclaration > FunctionDeclaration": handleFunction, |
| }; |
| }, |
| }; |
|
|