租房小程序前端代码
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.

56 lines
1.5 KiB

3 months ago
  1. /**
  2. * @fileoverview A rule to disallow modifying variables that are declared using `const`
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. const astUtils = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "problem",
  14. docs: {
  15. description: "Disallow reassigning `const` variables",
  16. recommended: true,
  17. url: "https://eslint.org/docs/latest/rules/no-const-assign"
  18. },
  19. schema: [],
  20. messages: {
  21. const: "'{{name}}' is constant."
  22. }
  23. },
  24. create(context) {
  25. const sourceCode = context.sourceCode;
  26. /**
  27. * Finds and reports references that are non initializer and writable.
  28. * @param {Variable} variable A variable to check.
  29. * @returns {void}
  30. */
  31. function checkVariable(variable) {
  32. astUtils.getModifyingReferences(variable.references).forEach(reference => {
  33. context.report({ node: reference.identifier, messageId: "const", data: { name: reference.identifier.name } });
  34. });
  35. }
  36. return {
  37. VariableDeclaration(node) {
  38. if (node.kind === "const") {
  39. sourceCode.getDeclaredVariables(node).forEach(checkVariable);
  40. }
  41. }
  42. };
  43. }
  44. };