Skip to content

Commit

Permalink
eslint#12579 Update: Edit what was reviewed
Browse files Browse the repository at this point in the history
  • Loading branch information
sunghyun.cho committed Aug 4, 2020
1 parent 4f1b8cf commit 64911e9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
9 changes: 6 additions & 3 deletions docs/rules/no-underscore-dangle.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ var _ = require('underscore');
var obj = _.contains(items, item);
obj.__proto__ = {};
var file = __filename;
function foo(_bar) {};
const foo = { onClick(_bar) {} };
const foo = (_bar) => {};
```

## Options
Expand All @@ -44,7 +47,7 @@ This rule has an object option:
- `"allowAfterSuper": false` (default) disallows dangling underscores in members of the `super` object
- `"allowAfterThisConstructor": false` (default) disallows dangling underscores in members of the `this.constructor` object
- `"enforceInMethodNames": false` (default) allows dangling underscores in method names
- `"allowFunctionParams": false` (default) disallows dangling underscores in first letter of function parameter name
- `"allowFunctionParams": false` (default) disallows dangling underscores in function parameter names

### allow

Expand Down Expand Up @@ -116,10 +119,10 @@ const o = {

### allowFunctionParams

Examples of **correct** code for this rule with the `{ "allowFunctionParams": true }` option:
Examples of **incorrect** code for this rule with the `{ "allowFunctionParams": false }` option:

```js
/*eslint no-underscore-dangle: ["error", { "allowFunctionParams": true }]*/
/*eslint no-underscore-dangle: ["error", { "allowFunctionParams": false }]*/

function foo (_bar) {}

Expand Down
54 changes: 27 additions & 27 deletions lib/rules/no-underscore-dangle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @fileoverview Rule to flag trailing underscores in variable declarations.
* @fileoverview Rule to flag dangling underscores in variable declarations.
* @author Matt DuVall <http://www.mattduvall.com>
*/

Expand Down Expand Up @@ -85,12 +85,12 @@ module.exports = {
}

/**
* Check if identifier has a underscore at the end
* Check if identifier has a dangling underscore
* @param {string} identifier name of the node
* @returns {boolean} true if its is present
* @private
*/
function hasTrailingUnderscore(identifier) {
function hasDanglingUnderscore(identifier) {
const len = identifier.length;

return identifier !== "_" && (identifier[0] === "_" || identifier[len - 1] === "_");
Expand Down Expand Up @@ -131,17 +131,17 @@ module.exports = {
}

/**
* Check if function parameter has a underscore at the beginning.
* Check if function parameter has a dangling underscore.
* @param {ASTNode} node node to evaluate
* @returns {boolean} true if function parameter name has a underscore at the beginning.
* @returns {boolean} true if function parameter name has a dangling underscore.
* @private
*/
function checkForFollowingUnderscoreInFunctionParameter(node) {
function checkForDanglingUnderscoreInFunctionParameter(node) {
if (!allowFunctionParams) {
node.params.forEach(param => {
const identifier = param.name;

if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier)) {
if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier)) {
context.report({
node,
messageId: "unexpectedUnderscore",
Expand All @@ -155,16 +155,16 @@ module.exports = {
}

/**
* Check if function has a underscore at the end
* Check if function has a dangling underscore
* @param {ASTNode} node node to evaluate
* @returns {void}
* @private
*/
function checkForTrailingUnderscoreInFunctionDeclaration(node) {
function checkForDanglingUnderscoreInFunction(node) {
if (node.id) {
const identifier = node.id.name;

if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) && !isAllowed(identifier)) {
if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) && !isAllowed(identifier)) {
context.report({
node,
messageId: "unexpectedUnderscore",
Expand All @@ -174,19 +174,19 @@ module.exports = {
});
}
}
checkForFollowingUnderscoreInFunctionParameter(node);
checkForDanglingUnderscoreInFunctionParameter(node);
}

/**
* Check if variable expression has a underscore at the end
* Check if variable expression has a dangling underscore
* @param {ASTNode} node node to evaluate
* @returns {void}
* @private
*/
function checkForTrailingUnderscoreInVariableExpression(node) {
function checkForDanglingUnderscoreInVariableExpression(node) {
const identifier = node.id.name;

if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) &&
!isSpecialCaseIdentifierInVariableExpression(identifier) && !isAllowed(identifier)) {
context.report({
node,
Expand All @@ -199,18 +199,18 @@ module.exports = {
}

/**
* Check if member expression has a underscore at the end
* Check if member expression has a dangling underscore
* @param {ASTNode} node node to evaluate
* @returns {void}
* @private
*/
function checkForTrailingUnderscoreInMemberExpression(node) {
function checkForDanglingUnderscoreInMemberExpression(node) {
const identifier = node.property.name,
isMemberOfThis = node.object.type === "ThisExpression",
isMemberOfSuper = node.object.type === "Super",
isMemberOfThisConstructor = isThisConstructorReference(node);

if (typeof identifier !== "undefined" && hasTrailingUnderscore(identifier) &&
if (typeof identifier !== "undefined" && hasDanglingUnderscore(identifier) &&
!(isMemberOfThis && allowAfterThis) &&
!(isMemberOfSuper && allowAfterSuper) &&
!(isMemberOfThisConstructor && allowAfterThisConstructor) &&
Expand All @@ -226,16 +226,16 @@ module.exports = {
}

/**
* Check if method declaration or method property has a underscore at the end
* Check if method declaration or method property has a dangling underscore
* @param {ASTNode} node node to evaluate
* @returns {void}
* @private
*/
function checkForTrailingUnderscoreInMethod(node) {
function checkForDanglingUnderscoreInMethod(node) {
const identifier = node.key.name;
const isMethod = node.type === "MethodDefinition" || node.type === "Property" && node.method;

if (typeof identifier !== "undefined" && enforceInMethodNames && isMethod && hasTrailingUnderscore(identifier) && !isAllowed(identifier)) {
if (typeof identifier !== "undefined" && enforceInMethodNames && isMethod && hasDanglingUnderscore(identifier) && !isAllowed(identifier)) {
context.report({
node,
messageId: "unexpectedUnderscore",
Expand All @@ -251,13 +251,13 @@ module.exports = {
//--------------------------------------------------------------------------

return {
FunctionDeclaration: checkForTrailingUnderscoreInFunctionDeclaration,
VariableDeclarator: checkForTrailingUnderscoreInVariableExpression,
MemberExpression: checkForTrailingUnderscoreInMemberExpression,
MethodDefinition: checkForTrailingUnderscoreInMethod,
Property: checkForTrailingUnderscoreInMethod,
FunctionExpression: checkForFollowingUnderscoreInFunctionParameter,
ArrowFunctionExpression: checkForFollowingUnderscoreInFunctionParameter
FunctionDeclaration: checkForDanglingUnderscoreInFunction,
VariableDeclarator: checkForDanglingUnderscoreInVariableExpression,
MemberExpression: checkForDanglingUnderscoreInMemberExpression,
MethodDefinition: checkForDanglingUnderscoreInMethod,
Property: checkForDanglingUnderscoreInMethod,
FunctionExpression: checkForDanglingUnderscoreInFunction,
ArrowFunctionExpression: checkForDanglingUnderscoreInFunction
};

}
Expand Down

0 comments on commit 64911e9

Please sign in to comment.