consistent-this.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @fileoverview Rule to enforce consistent naming of "this" context variables
  3. * @author Raphael Pigulla
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. docs: {
  14. description: "Enforce consistent naming when capturing the current execution context",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/consistent-this"
  17. },
  18. schema: {
  19. type: "array",
  20. items: {
  21. type: "string",
  22. minLength: 1
  23. },
  24. uniqueItems: true
  25. },
  26. messages: {
  27. aliasNotAssignedToThis: "Designated alias '{{name}}' is not assigned to 'this'.",
  28. unexpectedAlias: "Unexpected alias '{{name}}' for 'this'."
  29. }
  30. },
  31. create(context) {
  32. let aliases = [];
  33. if (context.options.length === 0) {
  34. aliases.push("that");
  35. } else {
  36. aliases = context.options;
  37. }
  38. /**
  39. * Reports that a variable declarator or assignment expression is assigning
  40. * a non-'this' value to the specified alias.
  41. * @param {ASTNode} node The assigning node.
  42. * @param {string} name the name of the alias that was incorrectly used.
  43. * @returns {void}
  44. */
  45. function reportBadAssignment(node, name) {
  46. context.report({ node, messageId: "aliasNotAssignedToThis", data: { name } });
  47. }
  48. /**
  49. * Checks that an assignment to an identifier only assigns 'this' to the
  50. * appropriate alias, and the alias is only assigned to 'this'.
  51. * @param {ASTNode} node The assigning node.
  52. * @param {Identifier} name The name of the variable assigned to.
  53. * @param {Expression} value The value of the assignment.
  54. * @returns {void}
  55. */
  56. function checkAssignment(node, name, value) {
  57. const isThis = value.type === "ThisExpression";
  58. if (aliases.includes(name)) {
  59. if (!isThis || node.operator && node.operator !== "=") {
  60. reportBadAssignment(node, name);
  61. }
  62. } else if (isThis) {
  63. context.report({ node, messageId: "unexpectedAlias", data: { name } });
  64. }
  65. }
  66. /**
  67. * Ensures that a variable declaration of the alias in a program or function
  68. * is assigned to the correct value.
  69. * @param {string} alias alias the check the assignment of.
  70. * @param {Object} scope scope of the current code we are checking.
  71. * @private
  72. * @returns {void}
  73. */
  74. function checkWasAssigned(alias, scope) {
  75. const variable = scope.set.get(alias);
  76. if (!variable) {
  77. return;
  78. }
  79. if (variable.defs.some(def => def.node.type === "VariableDeclarator" &&
  80. def.node.init !== null)) {
  81. return;
  82. }
  83. /*
  84. * The alias has been declared and not assigned: check it was
  85. * assigned later in the same scope.
  86. */
  87. if (!variable.references.some(reference => {
  88. const write = reference.writeExpr;
  89. return (
  90. reference.from === scope &&
  91. write && write.type === "ThisExpression" &&
  92. write.parent.operator === "="
  93. );
  94. })) {
  95. variable.defs.map(def => def.node).forEach(node => {
  96. reportBadAssignment(node, alias);
  97. });
  98. }
  99. }
  100. /**
  101. * Check each alias to ensure that is was assigned to the correct value.
  102. * @returns {void}
  103. */
  104. function ensureWasAssigned() {
  105. const scope = context.getScope();
  106. aliases.forEach(alias => {
  107. checkWasAssigned(alias, scope);
  108. });
  109. }
  110. return {
  111. "Program:exit": ensureWasAssigned,
  112. "FunctionExpression:exit": ensureWasAssigned,
  113. "FunctionDeclaration:exit": ensureWasAssigned,
  114. VariableDeclarator(node) {
  115. const id = node.id;
  116. const isDestructuring =
  117. id.type === "ArrayPattern" || id.type === "ObjectPattern";
  118. if (node.init !== null && !isDestructuring) {
  119. checkAssignment(node, id.name, node.init);
  120. }
  121. },
  122. AssignmentExpression(node) {
  123. if (node.left.type === "Identifier") {
  124. checkAssignment(node, node.left.name, node.right);
  125. }
  126. }
  127. };
  128. }
  129. };