Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to flag use of function declaration identifiers as variables. |
| 3 | * @author Ian Christian Myers |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | const astUtils = require("./utils/ast-utils"); |
| 9 | |
| 10 | //------------------------------------------------------------------------------ |
| 11 | // Rule Definition |
| 12 | //------------------------------------------------------------------------------ |
| 13 | |
| 14 | module.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 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 25 | schema: [], |
| 26 | |
| 27 | messages: { |
| 28 | isAFunction: "'{{name}}' is a function." |
| 29 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 30 | }, |
| 31 | |
| 32 | create(context) { |
| 33 | |
| 34 | /** |
| 35 | * Reports a reference if is non initializer and writable. |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 36 | * @param {References} references Collection of reference to check. |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 37 | * @returns {void} |
| 38 | */ |
| 39 | function checkReference(references) { |
| 40 | astUtils.getModifyingReferences(references).forEach(reference => { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame^] | 41 | context.report({ |
| 42 | node: reference.identifier, |
| 43 | messageId: "isAFunction", |
| 44 | data: { |
| 45 | name: reference.identifier.name |
| 46 | } |
| 47 | }); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 48 | }); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Finds and reports references that are non initializer and writable. |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 53 | * @param {Variable} variable A variable to check. |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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. |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 64 | * @param {ASTNode} node A function node to check. |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | }; |