node/tools/eslint/lib/rules/no-underscore-dangle.js
Roman Reiss d91e10b3bd tools: update eslint to 0.24.0
PR-URL: https://github.com/nodejs/io.js/pull/2072
Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
Reviewed-by: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Alex Kocharin <alex@kocharin.ru>
2015-06-29 19:02:17 +02:00

74 lines
2.5 KiB
JavaScript

/**
* @fileoverview Rule to flag trailing underscores in variable declarations.
* @author Matt DuVall <http://www.mattduvall.com>
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
//-------------------------------------------------------------------------
// Helpers
//-------------------------------------------------------------------------
function hasTrailingUnderscore(identifier) {
var len = identifier.length;
return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
}
function isSpecialCaseIdentifierForMemberExpression(identifier) {
return identifier === "__proto__";
}
function isSpecialCaseIdentifierInVariableExpression(identifier) {
// Checks for the underscore library usage here
return identifier === "_";
}
function checkForTrailingUnderscoreInFunctionDeclaration(node) {
if (node.id) {
var identifier = node.id.name;
if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier)) {
context.report(node, "Unexpected dangling \"_\" in \"" + identifier + "\".");
}
}
}
function checkForTrailingUnderscoreInVariableExpression(node) {
var identifier = node.id.name;
if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
!isSpecialCaseIdentifierInVariableExpression(identifier)) {
context.report(node, "Unexpected dangling \"_\" in \"" + identifier + "\".");
}
}
function checkForTrailingUnderscoreInMemberExpression(node) {
var identifier = node.property.name;
if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
!isSpecialCaseIdentifierForMemberExpression(identifier)) {
context.report(node, "Unexpected dangling \"_\" in \"" + identifier + "\".");
}
}
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
return {
"FunctionDeclaration": checkForTrailingUnderscoreInFunctionDeclaration,
"VariableDeclarator": checkForTrailingUnderscoreInVariableExpression,
"MemberExpression": checkForTrailingUnderscoreInMemberExpression
};
};
module.exports.schema = [];