UNPKG

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