forked from eslint/eslint
/
no-dangle.js
36 lines (28 loc) · 1.22 KB
/
no-dangle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @fileoverview Rule to flag trailing commas in object literals.
* @author Ian Christian Myers
*/
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
//-------------------------------------------------------------------------
// Helpers
//-------------------------------------------------------------------------
function checkForTrailingComma(node) {
var tokens = context.getTokens(node),
secondToLastToken = tokens[tokens.length - 2];
// The last token in an object/array literal will always be a closing
// curly, so we check the second to last token for a comma.
if (secondToLastToken.value === ",") {
context.report(node, "Found trailing comma in object literal.");
}
}
//--------------------------------------------------------------------------
// Public API
//--------------------------------------------------------------------------
return {
"ObjectExpression": checkForTrailingComma,
"ArrayExpression": checkForTrailingComma
};
};