noSparseArray
Summary
Section titled Summary- Rule available since:
v1.0.0
- Diagnostic Category:
lint/suspicious/noSparseArray
- This rule is recommended, which means is enabled by default.
- This rule has an unsafe fix.
- The default severity of this rule is error.
- Sources:
- Same as
no-sparse-arrays
- Same as
Description
Section titled DescriptionPrevents the use of sparse arrays (arrays with holes).
Sparse arrays may contain empty slots due to the use of multiple commas between two items, like the following:
const items = [a,,,b];
Arrays with holes might yield incorrect information. For example, the previous snippet, items
has a length of 4
, but did the user
really intended to have an array with four items? Or was it a typo.
This rule enforce the user to explicitly an undefined
in places where there’s a hole.
Examples
Section titled ExamplesInvalid
Section titled Invalid[1,,2]
code-block.js:1:1 lint/suspicious/noSparseArray FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This array contains an empty slots..
> 1 │ [1,,2]
│ ^^^^^^
2 │
ℹ The presences of empty slots may cause incorrect information and might be a typo.
ℹ Unsafe fix: Replace hole with undefined
1 │ [1,·undefined,2]
│ ++++++++++
Valid
Section titled Valid[1, undefined, 2]
How to configure
Section titled How to configure{ "linter": { "rules": { "suspicious": { "noSparseArray": "error" } } }}