mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.3 KiB
44 lines
1.3 KiB
10 years ago
|
/**
|
||
|
* @fileoverview Rule to flag trailing commas in object literals.
|
||
|
* @author Ian Christian Myers
|
||
|
*/
|
||
|
|
||
|
"use strict";
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// Rule Definition
|
||
|
//------------------------------------------------------------------------------
|
||
|
|
||
|
module.exports = function(context) {
|
||
|
|
||
|
//-------------------------------------------------------------------------
|
||
|
// Helpers
|
||
|
//-------------------------------------------------------------------------
|
||
|
|
||
|
function checkForTrailingComma(node) {
|
||
|
var items = node.properties || node.elements,
|
||
|
length = items.length,
|
||
|
lastItem, penultimateToken;
|
||
|
|
||
|
if (length) {
|
||
|
lastItem = items[length - 1];
|
||
|
if (lastItem) {
|
||
|
penultimateToken = context.getLastToken(node, 1);
|
||
|
if (penultimateToken.value === ",") {
|
||
|
context.report(lastItem, penultimateToken.loc.start, "Trailing comma.");
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------
|
||
|
// Public API
|
||
|
//--------------------------------------------------------------------------
|
||
|
|
||
|
return {
|
||
|
"ObjectExpression": checkForTrailingComma,
|
||
|
"ArrayExpression": checkForTrailingComma
|
||
|
};
|
||
|
|
||
|
};
|