Skip to content

Commit

Permalink
Chore: use astUtils.getStaticProperty for some places.
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jul 30, 2016
1 parent e6f8f9e commit 6fbe3ff
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 76 deletions.
36 changes: 1 addition & 35 deletions lib/rules/array-callback-return.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,38 +45,6 @@ function getLocation(node, sourceCode) {
return node.id || node;
}

/**
* Gets the name of a given node if the node is a Identifier node.
*
* @param {ASTNode} node - A node to get.
* @returns {string} The name of the node, or an empty string.
*/
function getIdentifierName(node) {
return node.type === "Identifier" ? node.name : "";
}

/**
* Gets the value of a given node if the node is a Literal node or a
* TemplateLiteral node.
*
* @param {ASTNode} node - A node to get.
* @returns {string} The value of the node, or an empty string.
*/
function getConstantStringValue(node) {
switch (node.type) {
case "Literal":
return String(node.value);

case "TemplateLiteral":
return node.expressions.length === 0
? node.quasis[0].value.cooked
: "";

default:
return "";
}
}

/**
* Checks a given node is a MemberExpression node which has the specified name's
* property.
Expand All @@ -88,9 +56,7 @@ function getConstantStringValue(node) {
function isTargetMethod(node) {
return (
node.type === "MemberExpression" &&
TARGET_METHODS.test(
(node.computed ? getConstantStringValue : getIdentifierName)(node.property)
)
TARGET_METHODS.test(astUtils.getStaticPropertyName(node) || "")
);
}

Expand Down
22 changes: 6 additions & 16 deletions lib/rules/no-alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const getPropertyName = require("../ast-utils").getStaticPropertyName;

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
Expand All @@ -28,22 +34,6 @@ function report(context, node, identifierName) {
context.report(node, "Unexpected {{name}}.", { name: identifierName });
}

/**
* Returns the property name of a MemberExpression.
* @param {ASTNode} memberExpressionNode The MemberExpression node.
* @returns {string|null} Returns the property name if available, null else.
*/
function getPropertyName(memberExpressionNode) {
if (memberExpressionNode.computed) {
if (memberExpressionNode.property.type === "Literal") {
return memberExpressionNode.property.value;
}
} else {
return memberExpressionNode.property.name;
}
return null;
}

/**
* Finds the escope reference in the given scope.
* @param {Object} scope The scope to search.
Expand Down
31 changes: 6 additions & 25 deletions lib/rules/no-extra-bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
*/
"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const getPropertyName = require("../ast-utils").getStaticPropertyName;

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -37,31 +43,6 @@ module.exports = {
});
}

/**
* Gets the property name of a given node.
* If the property name is dynamic, this returns an empty string.
*
* @param {ASTNode} node - A node to check. This is a MemberExpression.
* @returns {string} The property name of the node.
*/
function getPropertyName(node) {
if (node.computed) {
switch (node.property.type) {
case "Literal":
return String(node.property.value);
case "TemplateLiteral":
if (node.property.expressions.length === 0) {
return node.property.quasis[0].value.cooked;
}

// fallthrough
default:
return false;
}
}
return node.property.name;
}

/**
* Checks whether or not a given function node is the callee of `.bind()`
* method.
Expand Down

0 comments on commit 6fbe3ff

Please sign in to comment.