blob: c95bc203c4a96ca895506f2a77c7c6233f5ea8ce [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
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010011/** @type {import('../shared/types').Rule} */
Yang Guo4fd355c2019-09-19 10:59:03 +020012module.exports = {
13 meta: {
14 type: "problem",
15
16 docs: {
17 description: "disallow sparse arrays",
Yang Guo4fd355c2019-09-19 10:59:03 +020018 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};