summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/comma-spacing.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/comma-spacing.js')
-rw-r--r--tools/eslint/lib/rules/comma-spacing.js182
1 files changed, 87 insertions, 95 deletions
diff --git a/tools/eslint/lib/rules/comma-spacing.js b/tools/eslint/lib/rules/comma-spacing.js
index 5204801504..de7da6aff6 100644
--- a/tools/eslint/lib/rules/comma-spacing.js
+++ b/tools/eslint/lib/rules/comma-spacing.js
@@ -5,12 +5,17 @@
*/
"use strict";
+var astUtils = require("../ast-utils");
+
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
+ var sourceCode = context.getSourceCode();
+ var tokensAndComments = sourceCode.tokensAndComments;
+
var options = {
before: context.options[0] ? !!context.options[0].before : false,
after: context.options[0] ? !!context.options[0].after : true
@@ -20,47 +25,8 @@ module.exports = function(context) {
// Helpers
//--------------------------------------------------------------------------
- // the index of the last comment that was checked
- var lastCommentIndex = 0;
- var allComments;
-
- /**
- * Determines the length of comment between 2 tokens
- * @param {Object} left - The left token object.
- * @param {Object} right - The right token object.
- * @returns {number} Length of comment in between tokens
- */
- function getCommentLengthBetweenTokens(left, right) {
- return allComments.reduce(function(val, comment) {
- if (left.range[1] <= comment.range[0] && comment.range[1] <= right.range[0]) {
- val = val + comment.range[1] - comment.range[0];
- }
- return val;
- }, 0);
- }
-
- /**
- * Determines whether two adjacent tokens have whitespace between them.
- * @param {Object} left - The left token object.
- * @param {Object} right - The right token object.
- * @returns {boolean} Whether or not there is space between the tokens.
- */
- function isSpaced(left, right) {
- var punctuationLength = context.getTokensBetween(left, right).length; // the length of any parenthesis
- var commentLenth = getCommentLengthBetweenTokens(left, right);
- return (left.range[1] + punctuationLength + commentLenth) < right.range[0];
- }
-
- /**
- * Checks whether two tokens are on the same line.
- * @param {ASTNode} left The leftmost token.
- * @param {ASTNode} right The rightmost token.
- * @returns {boolean} True if the tokens are on the same line, false if not.
- * @private
- */
- function isSameLine(left, right) {
- return left.loc.end.line === right.loc.start.line;
- }
+ // list of comma tokens to ignore for the check of leading whitespace
+ var commaTokensToIgnore = [];
/**
* Determines if a given token is a comma operator.
@@ -76,13 +42,39 @@ module.exports = function(context) {
* Reports a spacing error with an appropriate message.
* @param {ASTNode} node The binary expression node to report.
* @param {string} dir Is the error "before" or "after" the comma?
+ * @param {ASTNode} otherNode The node at the left or right of `node`
* @returns {void}
* @private
*/
- function report(node, dir) {
- context.report(node, options[dir] ?
- "A space is required " + dir + " ','." :
- "There should be no space " + dir + " ','.");
+ function report(node, dir, otherNode) {
+ context.report({
+ node: node,
+ fix: function(fixer) {
+ if (options[dir]) {
+ if (dir === "before") {
+ return fixer.insertTextBefore(node, " ");
+ } else {
+ return fixer.insertTextAfter(node, " ");
+ }
+ } else {
+ var start, end;
+ var newText = "";
+
+ if (dir === "before") {
+ start = otherNode.range[1];
+ end = node.range[0];
+ } else {
+ start = node.range[1];
+ end = otherNode.range[0];
+ }
+
+ return fixer.replaceTextRange([start, end], newText);
+ }
+ },
+ message: options[dir] ?
+ "A space is required " + dir + " ','." :
+ "There should be no space " + dir + " ','."
+ });
}
/**
@@ -96,47 +88,46 @@ module.exports = function(context) {
* @private
*/
function validateCommaItemSpacing(tokens, reportItem) {
- if (tokens.left && isSameLine(tokens.left, tokens.comma) &&
- (options.before !== isSpaced(tokens.left, tokens.comma))
+ if (tokens.left && astUtils.isTokenOnSameLine(tokens.left, tokens.comma) &&
+ (options.before !== sourceCode.isSpaceBetweenTokens(tokens.left, tokens.comma))
) {
- report(reportItem, "before");
+ report(reportItem, "before", tokens.left);
+ }
+
+ if (tokens.right && !options.after && tokens.right.type === "Line") {
+ return false;
}
- if (tokens.right && isSameLine(tokens.comma, tokens.right) &&
- (options.after !== isSpaced(tokens.comma, tokens.right))
+
+ if (tokens.right && astUtils.isTokenOnSameLine(tokens.comma, tokens.right) &&
+ (options.after !== sourceCode.isSpaceBetweenTokens(tokens.comma, tokens.right))
) {
- report(reportItem, "after");
+ report(reportItem, "after", tokens.right);
}
}
/**
- * Determines if a given source index is in a comment or not by checking
- * the index against the comment range. Since the check goes straight
- * through the file, once an index is passed a certain comment, we can
- * go to the next comment to check that.
- * @param {int} index The source index to check.
- * @param {ASTNode[]} comments An array of comment nodes.
- * @returns {boolean} True if the index is within a comment, false if not.
- * @private
+ * Adds null elements of the given ArrayExpression or ArrayPattern node to the ignore list.
+ * @param {ASTNode} node An ArrayExpression or ArrayPattern node.
+ * @returns {void}
*/
- function isIndexInComment(index, comments) {
-
- var comment;
+ function addNullElementsToIgnoreList(node) {
+ var previousToken = context.getFirstToken(node);
- while (lastCommentIndex < comments.length) {
+ node.elements.forEach(function(element) {
+ var token;
- comment = comments[lastCommentIndex];
+ if (element === null) {
+ token = context.getTokenAfter(previousToken);
- if (comment.range[0] <= index && index < comment.range[1]) {
- return true;
- } else if (index > comment.range[1]) {
- lastCommentIndex++;
+ if (isComma(token)) {
+ commaTokensToIgnore.push(token);
+ }
} else {
- break;
+ token = context.getTokenAfter(element);
}
- }
-
- return false;
+ previousToken = token;
+ });
}
//--------------------------------------------------------------------------
@@ -144,33 +135,34 @@ module.exports = function(context) {
//--------------------------------------------------------------------------
return {
- "Program": function() {
+ "Program:exit": function() {
- var source = context.getSource(),
- pattern = /,/g,
- commaToken,
- previousToken,
+ var previousToken,
nextToken;
- allComments = context.getAllComments();
- while (pattern.test(source)) {
-
- // do not flag anything inside of comments
- if (!isIndexInComment(pattern.lastIndex, allComments)) {
- commaToken = context.getTokenByRangeStart(pattern.lastIndex - 1);
-
- if (commaToken && commaToken.type !== "JSXText") {
- previousToken = context.getTokenBefore(commaToken);
- nextToken = context.getTokenAfter(commaToken);
- validateCommaItemSpacing({
- comma: commaToken,
- left: isComma(previousToken) ? null : previousToken,
- right: isComma(nextToken) ? null : nextToken
- }, commaToken);
- }
+ tokensAndComments.forEach(function(token, i) {
+
+ if (!isComma(token)) {
+ return;
}
- }
- }
+
+ if (token && token.type === "JSXText") {
+ return;
+ }
+
+ previousToken = tokensAndComments[i - 1];
+ nextToken = tokensAndComments[i + 1];
+
+ validateCommaItemSpacing({
+ comma: token,
+ left: isComma(previousToken) || commaTokensToIgnore.indexOf(token) > -1 ? null : previousToken,
+ right: isComma(nextToken) ? null : nextToken
+ }, token);
+ });
+ },
+ "ArrayExpression": addNullElementsToIgnoreList,
+ "ArrayPattern": addNullElementsToIgnoreList
+
};
};