no-unassigned-vars.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /**
  2. * @fileoverview Rule to flag variables that are never assigned
  3. * @author Jacob Bandes-Storch <https://github.com/jtbandes>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "problem",
  13. dialects: ["typescript", "javascript"],
  14. language: "javascript",
  15. docs: {
  16. description:
  17. "Disallow `let` or `var` variables that are read but never assigned",
  18. recommended: false,
  19. url: "https://eslint.org/docs/latest/rules/no-unassigned-vars",
  20. },
  21. schema: [],
  22. messages: {
  23. unassigned:
  24. "'{{name}}' is always 'undefined' because it's never assigned.",
  25. },
  26. },
  27. create(context) {
  28. const sourceCode = context.sourceCode;
  29. let insideDeclareModule = false;
  30. return {
  31. "TSModuleDeclaration[declare=true]"() {
  32. insideDeclareModule = true;
  33. },
  34. "TSModuleDeclaration[declare=true]:exit"() {
  35. insideDeclareModule = false;
  36. },
  37. VariableDeclarator(node) {
  38. /** @type {import('estree').VariableDeclaration} */
  39. const declaration = node.parent;
  40. const shouldSkip =
  41. node.init ||
  42. node.id.type !== "Identifier" ||
  43. declaration.kind === "const" ||
  44. declaration.declare ||
  45. insideDeclareModule;
  46. if (shouldSkip) {
  47. return;
  48. }
  49. const [variable] = sourceCode.getDeclaredVariables(node);
  50. if (!variable) {
  51. return;
  52. }
  53. let hasRead = false;
  54. for (const reference of variable.references) {
  55. if (reference.isWrite()) {
  56. return;
  57. }
  58. if (reference.isRead()) {
  59. hasRead = true;
  60. }
  61. }
  62. if (!hasRead) {
  63. // Variables that are never read should be flagged by no-unused-vars instead
  64. return;
  65. }
  66. context.report({
  67. node,
  68. messageId: "unassigned",
  69. data: { name: node.id.name },
  70. });
  71. },
  72. };
  73. },
  74. };