项目原始demo,不改动
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

no-func-assign.js 1.9 KiB

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