Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Add metadata to existing rules - Batch 6 (refs #5417) #5966

Merged
merged 1 commit into from Apr 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: []
Copy link

@qfox qfox Apr 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, if I'm late, but diff should looks much better with fixes like:

exports.meta = {...};
exports.create = function(context) { ...

instead of assigning new object:
module.exports = { meta: ..., create: ... };.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah a bit late. We decided on this format a while back and have already converted a lot of rules to this format. So, we need to continue along this path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one more batch left before the pain is done :)

},

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 = [];