Skip to content

Commit

Permalink
Chore: Add metadata to existing rules - Batch 7 (refs #5417) (#5969)
Browse files Browse the repository at this point in the history
Chore: Add metadata to existing rules - Batch 7 of 7 (refs #5417)
  • Loading branch information
vitorbal authored and nzakas committed Apr 26, 2016
1 parent e2ad1ec commit 42d1ecc
Show file tree
Hide file tree
Showing 27 changed files with 3,156 additions and 2,864 deletions.
40 changes: 25 additions & 15 deletions lib/rules/prefer-spread.js
Expand Up @@ -70,22 +70,32 @@ function isValidThisArg(expectedThis, thisArg, context) {
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
return {
CallExpression: function(node) {
if (!isVariadicApplyCalling(node)) {
return;
}
module.exports = {
meta: {
docs: {
description: "require spread operators instead of `.apply()`",
category: "ECMAScript 6",
recommended: false
},

schema: []
},

create: function(context) {
return {
CallExpression: function(node) {
if (!isVariadicApplyCalling(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, "use the spread operator instead of the '.apply()'.");
if (isValidThisArg(expectedThis, thisArg, context)) {
context.report(node, "use the spread operator instead of the '.apply()'.");
}
}
}
};
};
}
};

module.exports.schema = [];
80 changes: 45 additions & 35 deletions lib/rules/prefer-template.js
Expand Up @@ -54,43 +54,53 @@ function hasNonStringLiteral(node) {
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function(context) {
var done = Object.create(null);

/**
* Reports if a given node is string concatenation with non string literals.
*
* @param {ASTNode} node - A node to check.
* @returns {void}
*/
function checkForStringConcat(node) {
if (!astUtils.isStringLiteral(node) || !isConcatenation(node.parent)) {
return;
}

var topBinaryExpr = getTopConcatBinaryExpression(node.parent);
module.exports = {
meta: {
docs: {
description: "require template literals instead of string concatenation",
category: "ECMAScript 6",
recommended: false
},

// Checks whether or not this node had been checked already.
if (done[topBinaryExpr.range[0]]) {
return;
schema: []
},

create: function(context) {
var done = Object.create(null);

/**
* Reports if a given node is string concatenation with non string literals.
*
* @param {ASTNode} node - A node to check.
* @returns {void}
*/
function checkForStringConcat(node) {
if (!astUtils.isStringLiteral(node) || !isConcatenation(node.parent)) {
return;
}

var topBinaryExpr = getTopConcatBinaryExpression(node.parent);

// Checks whether or not this node had been checked already.
if (done[topBinaryExpr.range[0]]) {
return;
}
done[topBinaryExpr.range[0]] = true;

if (hasNonStringLiteral(topBinaryExpr)) {
context.report(
topBinaryExpr,
"Unexpected string concatenation.");
}
}
done[topBinaryExpr.range[0]] = true;

if (hasNonStringLiteral(topBinaryExpr)) {
context.report(
topBinaryExpr,
"Unexpected string concatenation.");
}
}
return {
Program: function() {
done = Object.create(null);
},

return {
Program: function() {
done = Object.create(null);
},

Literal: checkForStringConcat,
TemplateLiteral: checkForStringConcat
};
Literal: checkForStringConcat,
TemplateLiteral: checkForStringConcat
};
}
};

module.exports.schema = [];

0 comments on commit 42d1ecc

Please sign in to comment.