useThrowNewError
¥Summary
-
规则生效日期:
v1.8.0¥Rule available since:
v1.8.0 -
诊断类别:
lint/style/useThrowNewError¥Diagnostic Category:
lint/style/useThrowNewError -
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
¥Same as
unicorn/throw-new-error
-
¥How to configure
{ "linter": { "rules": { "style": { "useThrowNewError": "error" } } }}¥Description
抛出错误时需要 new。
¥Require new when throwing an error.
虽然可以在不使用 new 关键字的情况下实例化 Error,但最好保持一致:现代内置函数需要实例化 new。
¥While it’s possible to instantiate Error without using the new keyword, it’s better to be consistent: modern builtins require new to be instantiated.
当规则名称以单词 “错误” 结尾且第一个字符为大写时,规则会匹配错误。
¥Rule matches errors when their name ends with the word “Error” and the first character is uppercase.
¥Examples
¥Invalid
throw Error();code-block.js:1:7 lint/style/useThrowNewError FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use new Error() instead of Error() when throwing an error.
> 1 │ throw Error();
│ ^^^^^^^
2 │
ℹ Instantiate Error with new keyword for consistency with modern builtins.
ℹ Unsafe fix: Add new keyword.
1 │ throw·new·Error();
│ ++++
throw TypeError('biome');code-block.js:1:7 lint/style/useThrowNewError FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use new TypeError() instead of TypeError() when throwing an error.
> 1 │ throw TypeError(‘biome’);
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Instantiate Error with new keyword for consistency with modern builtins.
ℹ Unsafe fix: Add new keyword.
1 │ throw·new·TypeError(‘biome’);
│ ++++
throw lib.TypeError();code-block.js:1:7 lint/style/useThrowNewError FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Use new TypeError() instead of TypeError() when throwing an error.
> 1 │ throw lib.TypeError();
│ ^^^^^^^^^^^^^^^
2 │
ℹ Instantiate Error with new keyword for consistency with modern builtins.
ℹ Unsafe fix: Add new keyword.
1 │ throw·new·lib.TypeError();
│ ++++
¥Valid
throw new Error();throw new TypeError('biome');throw new lib.TypeError();¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号