mirror of
https://github.com/nodejs/node.git
synced 2025-05-19 08:24:38 +00:00

PR-URL: https://github.com/iojs/io.js/pull/1539 Fixes: https://github.com/iojs/io.js/issues/1253 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Chris Dickinson <christopher.s.dickinson@gmail.com> Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
95 lines
3.0 KiB
JavaScript
95 lines
3.0 KiB
JavaScript
/**
|
|
* @fileoverview Require spaces around infix operators
|
|
* @author Michael Ficarra
|
|
*/
|
|
"use strict";
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
module.exports = function(context) {
|
|
var int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
|
|
|
|
var OPERATORS = [
|
|
"*", "/", "%", "+", "-", "<<", ">>", ">>>", "<", "<=", ">", ">=", "in",
|
|
"instanceof", "==", "!=", "===", "!==", "&", "^", "|", "&&", "||", "=",
|
|
"+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "&=", "^=", "|=",
|
|
"?", ":", ","
|
|
];
|
|
|
|
/**
|
|
* Returns the first token which violates the rule
|
|
* @param {ASTNode} left - The left node of the main node
|
|
* @param {ASTNode} right - The right node of the main node
|
|
* @returns {object} The violator token or null
|
|
* @private
|
|
*/
|
|
function getFirstNonSpacedToken(left, right) {
|
|
var op, tokens = context.getTokensBetween(left, right, 1);
|
|
for (var i = 1, l = tokens.length - 1; i < l; ++i) {
|
|
op = tokens[i];
|
|
if (
|
|
op.type === "Punctuator" &&
|
|
OPERATORS.indexOf(op.value) >= 0 &&
|
|
(tokens[i - 1].range[1] >= op.range[0] || op.range[1] >= tokens[i + 1].range[0])
|
|
) {
|
|
return op;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Reports an AST node as a rule violation
|
|
* @param {ASTNode} mainNode - The node to report
|
|
* @param {object} culpritToken - The token which has a problem
|
|
* @returns {void}
|
|
* @private
|
|
*/
|
|
function report(mainNode, culpritToken) {
|
|
context.report(mainNode, culpritToken.loc.start, "Infix operators must be spaced.");
|
|
}
|
|
|
|
function checkBinary(node) {
|
|
var nonSpacedNode = getFirstNonSpacedToken(node.left, node.right);
|
|
|
|
if (nonSpacedNode) {
|
|
if (!(int32Hint && context.getSource(node).substr(-2) === "|0")) {
|
|
report(node, nonSpacedNode);
|
|
}
|
|
}
|
|
}
|
|
|
|
function checkConditional(node) {
|
|
var nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent);
|
|
var nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate);
|
|
|
|
if (nonSpacedConsequesntNode) {
|
|
report(node, nonSpacedConsequesntNode);
|
|
} else if (nonSpacedAlternateNode) {
|
|
report(node, nonSpacedAlternateNode);
|
|
}
|
|
}
|
|
|
|
function checkVar(node) {
|
|
var nonSpacedNode;
|
|
|
|
if (node.init) {
|
|
nonSpacedNode = getFirstNonSpacedToken(node.id, node.init);
|
|
if (nonSpacedNode) {
|
|
report(node, nonSpacedNode);
|
|
}
|
|
}
|
|
}
|
|
|
|
return {
|
|
"AssignmentExpression": checkBinary,
|
|
"BinaryExpression": checkBinary,
|
|
"LogicalExpression": checkBinary,
|
|
"ConditionalExpression": checkConditional,
|
|
"VariableDeclarator": checkVar
|
|
};
|
|
|
|
};
|