UNPKG

1.98 kBJavaScriptView Raw
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("../ast-utils");
9
10//------------------------------------------------------------------------------
11// Rule Definition
12//------------------------------------------------------------------------------
13
14module.exports = {
15 meta: {
16 docs: {
17 description: "disallow reassigning `function` declarations",
18 category: "Possible Errors",
19 recommended: true,
20 url: "https://eslint.org/docs/rules/no-func-assign"
21 },
22
23 schema: []
24 },
25
26 create(context) {
27
28 /**
29 * Reports a reference if is non initializer and writable.
30 * @param {References} references - Collection of reference to check.
31 * @returns {void}
32 */
33 function checkReference(references) {
34 astUtils.getModifyingReferences(references).forEach(reference => {
35 context.report({ node: reference.identifier, message: "'{{name}}' is a function.", data: { name: reference.identifier.name } });
36 });
37 }
38
39 /**
40 * Finds and reports references that are non initializer and writable.
41 * @param {Variable} variable - A variable to check.
42 * @returns {void}
43 */
44 function checkVariable(variable) {
45 if (variable.defs[0].type === "FunctionName") {
46 checkReference(variable.references);
47 }
48 }
49
50 /**
51 * Checks parameters of a given function node.
52 * @param {ASTNode} node - A function node to check.
53 * @returns {void}
54 */
55 function checkForFunction(node) {
56 context.getDeclaredVariables(node).forEach(checkVariable);
57 }
58
59 return {
60 FunctionDeclaration: checkForFunction,
61 FunctionExpression: checkForFunction
62 };
63 }
64};