blob: e8407c3faede2d5a1983ea9ff011e3752701d5b7 [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",
17 category: "Possible Errors",
18 recommended: true,
19 url: "https://eslint.org/docs/rules/no-sparse-arrays"
20 },
21
Tim van der Lippe16aca392020-11-13 11:37:13 +000022 schema: [],
23
24 messages: {
25 unexpectedSparseArray: "Unexpected comma in middle of array."
26 }
Yang Guo4fd355c2019-09-19 10:59:03 +020027 },
28
29 create(context) {
30
31
32 //--------------------------------------------------------------------------
33 // Public
34 //--------------------------------------------------------------------------
35
36 return {
37
38 ArrayExpression(node) {
39
40 const emptySpot = node.elements.indexOf(null) > -1;
41
42 if (emptySpot) {
Tim van der Lippe16aca392020-11-13 11:37:13 +000043 context.report({ node, messageId: "unexpectedSparseArray" });
Yang Guo4fd355c2019-09-19 10:59:03 +020044 }
45 }
46
47 };
48
49 }
50};