noSparseArray
¥Summary
-
规则生效日期:
v1.0.0¥Rule available since:
v1.0.0 -
诊断类别:
lint/suspicious/noSparseArray¥Diagnostic Category:
lint/suspicious/noSparseArray -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 error。
¥The default severity of this rule is error.
-
来源:
¥Sources:
-
与
no-sparse-arrays相同¥Same as
no-sparse-arrays
-
¥How to configure
{ "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
Biome v2.1 中文网 - 粤ICP备13048890号