Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Disallow sparse arrays |
| 3 | * @author Nicholas C. Zakas |
| 4 | */ |
| 5 | "use strict"; |
| 6 | |
| 7 | //------------------------------------------------------------------------------ |
| 8 | // Rule Definition |
| 9 | //------------------------------------------------------------------------------ |
| 10 | |
| 11 | module.exports = { |
| 12 | meta: { |
| 13 | type: "problem", |
| 14 | |
| 15 | docs: { |
| 16 | description: "disallow sparse arrays", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 17 | recommended: true, |
| 18 | url: "https://eslint.org/docs/rules/no-sparse-arrays" |
| 19 | }, |
| 20 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 21 | schema: [], |
| 22 | |
| 23 | messages: { |
| 24 | unexpectedSparseArray: "Unexpected comma in middle of array." |
| 25 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 26 | }, |
| 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 Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 42 | context.report({ node, messageId: "unexpectedSparseArray" }); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 43 | } |
| 44 | } |
| 45 | |
| 46 | }; |
| 47 | |
| 48 | } |
| 49 | }; |