Skip to content

Commit

Permalink
Chore: Add metadata to existing rules - Batch 6 (refs eslint#5417)
Browse files Browse the repository at this point in the history
Chore: Add metadata to existing rules - Batch 6 of 7 (refs eslint#5417)
  • Loading branch information
vitorbal committed Apr 25, 2016
1 parent 1e7a3ef commit 2492bac
Show file tree
Hide file tree
Showing 20 changed files with 1,759 additions and 1,556 deletions.
46 changes: 28 additions & 18 deletions lib/rules/no-useless-call.js
Expand Up @@ -71,25 +71,35 @@ function isValidThisArg(expectedThis, thisArg, context) {
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
return {
CallExpression: function(node) {
if (!isCallOrNonVariadicApply(node)) {
return;
}
module.exports = {
meta: {
docs: {
description: "disallow unnecessary calls to `.call()` and `.apply()`",
category: "Best Practices",
recommended: false
},

schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (!isCallOrNonVariadicApply(node)) {
return;
}

var applied = node.callee.object;
var expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
var thisArg = node.arguments[0];
var applied = node.callee.object;
var expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
var thisArg = node.arguments[0];

if (isValidThisArg(expectedThis, thisArg, context)) {
context.report(
node,
"unnecessary '.{{name}}()'.",
{name: node.callee.property.name});
if (isValidThisArg(expectedThis, thisArg, context)) {
context.report(
node,
"unnecessary '.{{name}}()'.",
{name: node.callee.property.name});
}
}
}
};
};
}
};

module.exports.schema = [];
64 changes: 37 additions & 27 deletions lib/rules/no-useless-concat.js
Expand Up @@ -55,38 +55,48 @@ function getRight(node) {
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
return {
BinaryExpression: function(node) {
module.exports = {
meta: {
docs: {
description: "disallow unnecessary concatenation of literals or template literals",
category: "Best Practices",
recommended: false
},

schema: []
},

create: function(context) {
return {
BinaryExpression: function(node) {

// check if not concatenation
if (node.operator !== "+") {
return;
}

// check if not concatenation
if (node.operator !== "+") {
return;
}
// account for the `foo + "a" + "b"` case
var left = getLeft(node);
var right = getRight(node);

// account for the `foo + "a" + "b"` case
var left = getLeft(node);
var right = getRight(node);
if (astUtils.isStringLiteral(left) &&
astUtils.isStringLiteral(right) &&
astUtils.isTokenOnSameLine(left, right)
) {

if (astUtils.isStringLiteral(left) &&
astUtils.isStringLiteral(right) &&
astUtils.isTokenOnSameLine(left, right)
) {
// move warning location to operator
var operatorToken = context.getTokenAfter(left);

// move warning location to operator
var operatorToken = context.getTokenAfter(left);
while (operatorToken.value !== "+") {
operatorToken = context.getTokenAfter(operatorToken);
}

while (operatorToken.value !== "+") {
operatorToken = context.getTokenAfter(operatorToken);
context.report(
node,
operatorToken.loc.start,
"Unexpected string concatenation of literals.");
}

context.report(
node,
operatorToken.loc.start,
"Unexpected string concatenation of literals.");
}
}
};
};
}
};

module.exports.schema = [];
62 changes: 36 additions & 26 deletions lib/rules/no-useless-constructor.js
Expand Up @@ -140,33 +140,43 @@ function isRedundantSuperCall(body, ctorParams) {
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {

/**
* Checks whether a node is a redundant constructor
* @param {ASTNode} node - node to check
* @returns {void}
*/
function checkForConstructor(node) {
if (node.kind !== "constructor") {
return;
module.exports = {
meta: {
docs: {
description: "disallow unnecessary constructors",
category: "ECMAScript 6",
recommended: false
},

schema: []
},

create: function(context) {

/**
* Checks whether a node is a redundant constructor
* @param {ASTNode} node - node to check
* @returns {void}
*/
function checkForConstructor(node) {
if (node.kind !== "constructor") {
return;
}

var body = node.value.body.body;
var ctorParams = node.value.params;
var superClass = node.parent.parent.superClass;

if (superClass ? isRedundantSuperCall(body, ctorParams) : (body.length === 0)) {
context.report({
node: node,
message: "Useless constructor."
});
}
}

var body = node.value.body.body;
var ctorParams = node.value.params;
var superClass = node.parent.parent.superClass;

if (superClass ? isRedundantSuperCall(body, ctorParams) : (body.length === 0)) {
context.report({
node: node,
message: "Useless constructor."
});
}
return {
MethodDefinition: checkForConstructor
};
}

return {
MethodDefinition: checkForConstructor
};
};

module.exports.schema = [];
108 changes: 59 additions & 49 deletions lib/rules/no-useless-escape.js
Expand Up @@ -57,64 +57,74 @@ var VALID_REGEX_ESCAPES = [
"u"
];

module.exports = function(context) {
module.exports = {
meta: {
docs: {
description: "disallow unnecessary escape characters",
category: "Best Practices",
recommended: false
},

/**
* Checks if the escape character in given slice is unnecessary.
*
* @private
* @param {string[]} escapes - list of valid escapes
* @param {ASTNode} node - node to validate.
* @param {string} elm - string slice to validate.
* @returns {void}
*/
function validate(escapes, node, elm) {
var escapeNotFound = escapes.indexOf(elm[0][1]) === -1;
var isQuoteEscape = elm[0][1] === node.raw[0];
schema: []
},

if (escapeNotFound && !isQuoteEscape) {
context.report({
node: node,
loc: {
line: node.loc.start.line,
column: node.loc.start.column + elm.index
},
message: "Unnecessary escape character: " + elm[0]
});
create: function(context) {

/**
* Checks if the escape character in given slice is unnecessary.
*
* @private
* @param {string[]} escapes - list of valid escapes
* @param {ASTNode} node - node to validate.
* @param {string} elm - string slice to validate.
* @returns {void}
*/
function validate(escapes, node, elm) {
var escapeNotFound = escapes.indexOf(elm[0][1]) === -1;
var isQuoteEscape = elm[0][1] === node.raw[0];

if (escapeNotFound && !isQuoteEscape) {
context.report({
node: node,
loc: {
line: node.loc.start.line,
column: node.loc.start.column + elm.index
},
message: "Unnecessary escape character: " + elm[0]
});
}
}
}

/**
* Checks if a node has an escape.
*
* @param {ASTNode} node - node to check.
* @returns {void}
*/
function check(node) {
var nodeEscapes, match;
var pattern = /\\[^\d]/g;
/**
* Checks if a node has an escape.
*
* @param {ASTNode} node - node to check.
* @returns {void}
*/
function check(node) {
var nodeEscapes, match;
var pattern = /\\[^\d]/g;

if (typeof node.value === "string") {

if (typeof node.value === "string") {
// JSXAttribute doesn't have any escape sequence: https://facebook.github.io/jsx/
if (node.parent.type === "JSXAttribute") {
return;
}

// JSXAttribute doesn't have any escape sequence: https://facebook.github.io/jsx/
if (node.parent.type === "JSXAttribute") {
nodeEscapes = VALID_STRING_ESCAPES;
} else if (node.regex) {
nodeEscapes = VALID_REGEX_ESCAPES;
} else {
return;
}

nodeEscapes = VALID_STRING_ESCAPES;
} else if (node.regex) {
nodeEscapes = VALID_REGEX_ESCAPES;
} else {
return;
}

while ((match = pattern.exec(node.raw))) {
validate(nodeEscapes, node, match);
while ((match = pattern.exec(node.raw))) {
validate(nodeEscapes, node, match);
}
}
return {
Literal: check
};
}
return {
Literal: check
};
};

module.exports.schema = [];
28 changes: 19 additions & 9 deletions lib/rules/no-var.js
Expand Up @@ -9,17 +9,27 @@
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
module.exports = {
meta: {
docs: {
description: "require `let` or `const` instead of `var`",
category: "ECMAScript 6",
recommended: false
},

return {
VariableDeclaration: function(node) {
if (node.kind === "var") {
context.report(node, "Unexpected var, use let or const instead.");
schema: []
},

create: function(context) {

return {
VariableDeclaration: function(node) {
if (node.kind === "var") {
context.report(node, "Unexpected var, use let or const instead.");
}
}
}

};
};

}
};

module.exports.schema = [];

0 comments on commit 2492bac

Please sign in to comment.