Skip to content

noSparseArray

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"suspicious": {
"noSparseArray": "error"
}
}
}
}

¥Description

防止使用稀疏数组(带空位的数组)。

¥Prevents 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];

包含空洞的数组可能会产生错误信息。例如,前面的代码片段中,items 的长度为 4,但用户真的想要一个包含四个元素的数组吗?或者只是打错了字。

¥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.

此规则强制用户在存在漏洞的地方显式使用 undefined

¥This rule enforce the user to explicitly an undefined in places where there’s a hole.

¥Examples

¥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

[1, undefined, 2]

¥Related links