no-sparse-arrays.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * @fileoverview Disallow sparse arrays
  3. * @author Nicholas C. Zakas
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "problem",
  13. docs: {
  14. description: "Disallow sparse arrays",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/no-sparse-arrays"
  17. },
  18. schema: [],
  19. messages: {
  20. unexpectedSparseArray: "Unexpected comma in middle of array."
  21. }
  22. },
  23. create(context) {
  24. //--------------------------------------------------------------------------
  25. // Public
  26. //--------------------------------------------------------------------------
  27. return {
  28. ArrayExpression(node) {
  29. const emptySpot = node.elements.includes(null);
  30. if (emptySpot) {
  31. context.report({ node, messageId: "unexpectedSparseArray" });
  32. }
  33. }
  34. };
  35. }
  36. };