1/** 2 * @fileoverview Rule to flag use of function declaration identifiers as variables. 3 * @author Ian Christian Myers 4 */ 5 6"use strict"; 7 8const astUtils = require("./utils/ast-utils"); 9 10//------------------------------------------------------------------------------ 11// Rule Definition 12//------------------------------------------------------------------------------ 13 14module.exports = { 15 meta: { 16 type: "problem", 17 18 docs: { 19 description: "disallow reassigning `function` declarations", 20 category: "Possible Errors", 21 recommended: true, 22 url: "https://eslint.org/docs/rules/no-func-assign" 23 }, 24 25 schema: [], 26 27 messages: { 28 isAFunction: "'{{name}}' is a function." 29 } 30 }, 31 32 create(context) { 33 34 /** 35 * Reports a reference if is non initializer and writable. 36 * @param {References} references Collection of reference to check. 37 * @returns {void} 38 */ 39 function checkReference(references) { 40 astUtils.getModifyingReferences(references).forEach(reference => { 41 context.report({ 42 node: reference.identifier, 43 messageId: "isAFunction", 44 data: { 45 name: reference.identifier.name 46 } 47 }); 48 }); 49 } 50 51 /** 52 * Finds and reports references that are non initializer and writable. 53 * @param {Variable} variable A variable to check. 54 * @returns {void} 55 */ 56 function checkVariable(variable) { 57 if (variable.defs[0].type === "FunctionName") { 58 checkReference(variable.references); 59 } 60 } 61 62 /** 63 * Checks parameters of a given function node. 64 * @param {ASTNode} node A function node to check. 65 * @returns {void} 66 */ 67 function checkForFunction(node) { 68 context.getDeclaredVariables(node).forEach(checkVariable); 69 } 70 71 return { 72 FunctionDeclaration: checkForFunction, 73 FunctionExpression: checkForFunction 74 }; 75 } 76}; 77