diff options
Diffstat (limited to 'tools/eslint/lib/rules/comma-dangle.js')
-rw-r--r-- | tools/eslint/lib/rules/comma-dangle.js | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/comma-dangle.js b/tools/eslint/lib/rules/comma-dangle.js new file mode 100644 index 0000000000..da3776d424 --- /dev/null +++ b/tools/eslint/lib/rules/comma-dangle.js @@ -0,0 +1,58 @@ +/** + * @fileoverview Rule to forbid or enforce dangling commas. + * @author Ian Christian Myers + * @copyright 2015 Mathias Schreck + * @copyright 2013 Ian Christian Myers + */ + +"use strict"; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = function (context) { + var allowDangle = context.options[0]; + var forbidDangle = allowDangle !== "always-multiline" && allowDangle !== "always"; + var UNEXPECTED_MESSAGE = "Unexpected trailing comma."; + var MISSING_MESSAGE = "Missing trailing comma."; + + /** + * Checks the given node for trailing comma and reports violations. + * @param {ASTNode} node The node of an ObjectExpression or ArrayExpression + * @returns {void} + */ + function checkForTrailingComma(node) { + var items = node.properties || node.elements, + length = items.length, + nodeIsMultiLine = node.loc.start.line !== node.loc.end.line, + lastItem, + penultimateToken, + hasDanglingComma; + + if (length) { + lastItem = items[length - 1]; + if (lastItem) { + penultimateToken = context.getLastToken(node, 1); + hasDanglingComma = penultimateToken.value === ","; + + if (forbidDangle && hasDanglingComma) { + context.report(lastItem, penultimateToken.loc.start, UNEXPECTED_MESSAGE); + } else if (allowDangle === "always-multiline") { + if (hasDanglingComma && !nodeIsMultiLine) { + context.report(lastItem, penultimateToken.loc.start, UNEXPECTED_MESSAGE); + } else if (!hasDanglingComma && nodeIsMultiLine) { + context.report(lastItem, penultimateToken.loc.end, MISSING_MESSAGE); + } + } else if (allowDangle === "always" && !hasDanglingComma) { + context.report(lastItem, lastItem.loc.end, MISSING_MESSAGE); + } + } + } + } + + return { + "ObjectExpression": checkForTrailingComma, + "ArrayExpression": checkForTrailingComma + }; +}; |