2 * @fileoverview Rule to enforce consistent naming of "this" context variables
3 * @author Raphael Pigulla
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "enforce consistent naming when capturing the current execution context",
17 category: "Stylistic Issues",
19 url: "https://eslint.org/docs/rules/consistent-this"
32 aliasNotAssignedToThis: "Designated alias '{{name}}' is not assigned to 'this'.",
33 unexpectedAlias: "Unexpected alias '{{name}}' for 'this'."
40 if (context.options.length === 0) {
43 aliases = context.options;
47 * Reports that a variable declarator or assignment expression is assigning
48 * a non-'this' value to the specified alias.
49 * @param {ASTNode} node The assigning node.
50 * @param {string} name the name of the alias that was incorrectly used.
53 function reportBadAssignment(node, name) {
54 context.report({ node, messageId: "aliasNotAssignedToThis", data: { name } });
58 * Checks that an assignment to an identifier only assigns 'this' to the
59 * appropriate alias, and the alias is only assigned to 'this'.
60 * @param {ASTNode} node The assigning node.
61 * @param {Identifier} name The name of the variable assigned to.
62 * @param {Expression} value The value of the assignment.
65 function checkAssignment(node, name, value) {
66 const isThis = value.type === "ThisExpression";
68 if (aliases.indexOf(name) !== -1) {
69 if (!isThis || node.operator && node.operator !== "=") {
70 reportBadAssignment(node, name);
73 context.report({ node, messageId: "unexpectedAlias", data: { name } });
78 * Ensures that a variable declaration of the alias in a program or function
79 * is assigned to the correct value.
80 * @param {string} alias alias the check the assignment of.
81 * @param {Object} scope scope of the current code we are checking.
85 function checkWasAssigned(alias, scope) {
86 const variable = scope.set.get(alias);
92 if (variable.defs.some(def => def.node.type === "VariableDeclarator" &&
93 def.node.init !== null)) {
98 * The alias has been declared and not assigned: check it was
99 * assigned later in the same scope.
101 if (!variable.references.some(reference => {
102 const write = reference.writeExpr;
105 reference.from === scope &&
106 write && write.type === "ThisExpression" &&
107 write.parent.operator === "="
110 variable.defs.map(def => def.node).forEach(node => {
111 reportBadAssignment(node, alias);
117 * Check each alias to ensure that is was assigned to the correct value.
120 function ensureWasAssigned() {
121 const scope = context.getScope();
123 aliases.forEach(alias => {
124 checkWasAssigned(alias, scope);
129 "Program:exit": ensureWasAssigned,
130 "FunctionExpression:exit": ensureWasAssigned,
131 "FunctionDeclaration:exit": ensureWasAssigned,
133 VariableDeclarator(node) {
135 const isDestructuring =
136 id.type === "ArrayPattern" || id.type === "ObjectPattern";
138 if (node.init !== null && !isDestructuring) {
139 checkAssignment(node, id.name, node.init);
143 AssignmentExpression(node) {
144 if (node.left.type === "Identifier") {
145 checkAssignment(node, node.left.name, node.right);