1 | /**
|
2 | * @fileoverview Rule to enforce consistent naming of "this" context variables
|
3 | * @author Raphael Pigulla
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Rule Definition
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | module.exports = function(context) {
|
12 |
|
13 | return {
|
14 |
|
15 | "VariableDeclaration": function(node) {
|
16 | var alias = context.options[0];
|
17 |
|
18 | node.declarations.forEach(function(declaration) {
|
19 | if (declaration.id.name === alias &&
|
20 | !(declaration.init && declaration.init.type === "ThisExpression")) {
|
21 |
|
22 | context.report(
|
23 | node,
|
24 | "Designated 'this' alias '{{alias}}' is not assigned " +
|
25 | "to the current execution context.",
|
26 | { alias: declaration.id.name }
|
27 | );
|
28 | }
|
29 |
|
30 | if (declaration.init &&
|
31 | declaration.init.type === "ThisExpression" &&
|
32 | declaration.id.name !== alias) {
|
33 |
|
34 | context.report(
|
35 | node,
|
36 | "Unexpected alias '{{alias}}' for 'this'.",
|
37 | { alias: declaration.id.name }
|
38 | );
|
39 | }
|
40 | });
|
41 | }
|
42 | };
|
43 |
|
44 | };
|