2 * @fileoverview Rule to flag trailing underscores in variable declarations.
3 * @author Matt DuVall <http://www.mattduvall.com>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 module.exports = function(context) {
14 //-------------------------------------------------------------------------
16 //-------------------------------------------------------------------------
18 function hasTrailingUnderscore(identifier) {
19 var len = identifier.length;
21 return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
24 function isSpecialCaseIdentifierForMemberExpression(identifier) {
25 return identifier === "__proto__";
28 function isSpecialCaseIdentifierInVariableExpression(identifier) {
29 // Checks for the underscore library usage here
30 return identifier === "_";
33 function checkForTrailingUnderscoreInFunctionDeclaration(node) {
35 var identifier = node.id.name;
37 if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier)) {
38 context.report(node, "Unexpected dangling '_' in '" + identifier + "'.");
43 function checkForTrailingUnderscoreInVariableExpression(node) {
44 var identifier = node.id.name;
46 if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
47 !isSpecialCaseIdentifierInVariableExpression(identifier)) {
48 context.report(node, "Unexpected dangling '_' in '" + identifier + "'.");
52 function checkForTrailingUnderscoreInMemberExpression(node) {
53 var identifier = node.property.name;
55 if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
56 !isSpecialCaseIdentifierForMemberExpression(identifier)) {
57 context.report(node, "Unexpected dangling '_' in '" + identifier + "'.");
61 //--------------------------------------------------------------------------
63 //--------------------------------------------------------------------------
66 "FunctionDeclaration": checkForTrailingUnderscoreInFunctionDeclaration,
67 "VariableDeclarator": checkForTrailingUnderscoreInVariableExpression,
68 "MemberExpression": checkForTrailingUnderscoreInMemberExpression