UNPKG

1.53 kBJavaScriptView Raw
1/**
2 * Disallows an extra comma following the final element of an array or object literal.
3 *
4 * Type: `Boolean`
5 *
6 * Value: `true`
7 *
8 * JSHint: [`es3`](http://jshint.com/docs/options/#es3)
9 *
10 * #### Example
11 *
12 * ```js
13 * "disallowTrailingComma": true
14 * ```
15 *
16 * ##### Valid
17 *
18 * ```js
19 * var foo = [1, 2, 3];
20 * var bar = {a: "a", b: "b"}
21 * const [1, 2, 3];
22 * const {a: "a", b: "b"}
23 * ```
24 *
25 * ##### Invalid
26 *
27 * ```js
28 * var foo = [1, 2, 3, ];
29 * var bar = {a: "a", b: "b", }
30 * ```
31 */
32
33var assert = require('assert');
34
35module.exports = function() {};
36
37module.exports.prototype = {
38 configure: function(options) {
39 assert(
40 options === true,
41 this.getOptionName() + ' option requires a true value or should be removed'
42 );
43 },
44
45 getOptionName: function() {
46 return 'disallowTrailingComma';
47 },
48
49 check: function(file, errors) {
50 file.iterateNodesByType([
51 'ObjectExpression', 'ArrayExpression',
52 'ObjectPattern', 'ArrayPattern'
53 ], function(node) {
54 var closingToken = file.getLastNodeToken(node);
55 var comma = closingToken.getPreviousCodeToken();
56
57 if (comma.type === 'Punctuator' && comma.value === ',') {
58 errors.add(
59 'Extra comma following the final element of an array or object literal',
60 comma
61 );
62 }
63 });
64 },
65
66 _fix: function(file, error) {
67 error.element.remove();
68 }
69
70};