github-pages-deploy-action/node_modules/eslint/lib/rules/no-func-assign.js

77 lines
2.1 KiB
JavaScript
Raw Permalink Normal View History

2020-03-07 11:45:40 +08:00
/**
* @fileoverview Rule to flag use of function declaration identifiers as variables.
* @author Ian Christian Myers
*/
"use strict";
2020-03-31 20:40:00 +08:00
const astUtils = require("./utils/ast-utils");
2020-03-07 11:45:40 +08:00
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow reassigning `function` declarations",
category: "Possible Errors",
recommended: true,
url: "https://eslint.org/docs/rules/no-func-assign"
},
2020-05-15 05:33:08 +08:00
schema: [],
messages: {
isAFunction: "'{{name}}' is a function."
}
2020-03-07 11:45:40 +08:00
},
create(context) {
/**
* Reports a reference if is non initializer and writable.
2020-03-31 20:40:00 +08:00
* @param {References} references Collection of reference to check.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function checkReference(references) {
astUtils.getModifyingReferences(references).forEach(reference => {
2020-05-15 05:33:08 +08:00
context.report({
node: reference.identifier,
messageId: "isAFunction",
data: {
name: reference.identifier.name
}
});
2020-03-07 11:45:40 +08:00
});
}
/**
* Finds and reports references that are non initializer and writable.
2020-03-31 20:40:00 +08:00
* @param {Variable} variable A variable to check.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function checkVariable(variable) {
if (variable.defs[0].type === "FunctionName") {
checkReference(variable.references);
}
}
/**
* Checks parameters of a given function node.
2020-03-31 20:40:00 +08:00
* @param {ASTNode} node A function node to check.
2020-03-07 11:45:40 +08:00
* @returns {void}
*/
function checkForFunction(node) {
context.getDeclaredVariables(node).forEach(checkVariable);
}
return {
FunctionDeclaration: checkForFunction,
FunctionExpression: checkForFunction
};
}
};