blob: 56ce5dcc871b1ef99f704718ca18a6c28f6b52b9 [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview Disallow sparse arrays
3 * @author Nicholas C. Zakas
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = {
12 meta: {
13 type: "problem",
14
15 docs: {
16 description: "disallow sparse arrays",
Yang Guo4fd355c2019-09-19 10:59:03 +020017 recommended: true,
18 url: "https://eslint.org/docs/rules/no-sparse-arrays"
19 },
20
Tim van der Lippe16aca392020-11-13 11:37:13 +000021 schema: [],
22
23 messages: {
24 unexpectedSparseArray: "Unexpected comma in middle of array."
25 }
Yang Guo4fd355c2019-09-19 10:59:03 +020026 },
27
28 create(context) {
29
30
31 //--------------------------------------------------------------------------
32 // Public
33 //--------------------------------------------------------------------------
34
35 return {
36
37 ArrayExpression(node) {
38
39 const emptySpot = node.elements.indexOf(null) > -1;
40
41 if (emptySpot) {
Tim van der Lippe16aca392020-11-13 11:37:13 +000042 context.report({ node, messageId: "unexpectedSparseArray" });
Yang Guo4fd355c2019-09-19 10:59:03 +020043 }
44 }
45
46 };
47
48 }
49};