Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-func-assign.js 2.0KB

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