Skip to content

Commit c9a33fa

Browse files
authored
Add no-error-iserror rule and no-nonstandard-error-properties rule (#250)
1 parent 52589de commit c9a33fa

13 files changed

+272
-1
lines changed

docs/rules/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
1111
| Rule ID | Description | |
1212
|:--------|:------------|:--:|
1313
| [es-x/no-array-fromasync](./no-array-fromasync.md) | disallow the `Array.fromAsync` method. | |
14+
| [es-x/no-error-iserror](./no-error-iserror.md) | disallow the `Error.isError` method. | |
1415

1516
## ES2025
1617

@@ -417,6 +418,7 @@ Rules in this category are not included in any preset.
417418
| [es-x/no-nonstandard-dataview-prototype-properties](./no-nonstandard-dataview-prototype-properties.md) | disallow non-standard properties on DataView instance. | |
418419
| [es-x/no-nonstandard-date-properties](./no-nonstandard-date-properties.md) | disallow non-standard static properties on `Date` class. | |
419420
| [es-x/no-nonstandard-date-prototype-properties](./no-nonstandard-date-prototype-properties.md) | disallow non-standard properties on Date instance. | |
421+
| [es-x/no-nonstandard-error-properties](./no-nonstandard-error-properties.md) | disallow non-standard static properties on `Error` class. | |
420422
| [es-x/no-nonstandard-finalizationregistry-properties](./no-nonstandard-finalizationregistry-properties.md) | disallow non-standard static properties on `FinalizationRegistry` class. | |
421423
| [es-x/no-nonstandard-finalizationregistry-prototype-properties](./no-nonstandard-finalizationregistry-prototype-properties.md) | disallow non-standard properties on FinalizationRegistry instance. | |
422424
| [es-x/no-nonstandard-function-properties](./no-nonstandard-function-properties.md) | disallow non-standard static properties on `Function` class. | |

docs/rules/no-error-iserror.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: "es-x/no-error-iserror"
3+
description: "disallow the `Error.isError` method"
4+
---
5+
6+
# es-x/no-error-iserror
7+
> disallow the `Error.isError` method
8+
9+
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
10+
- ✅ The following configurations enable this rule: [no-new-in-esnext]
11+
12+
This rule reports ES2026 [`Error.isError` method](https://github.com/tc39/proposal-is-error) as errors.
13+
14+
## 💡 Examples
15+
16+
⛔ Examples of **incorrect** code for this rule:
17+
18+
<eslint-playground type="bad">
19+
20+
```js
21+
/*eslint es-x/no-error-iserror: error */
22+
Error.isError(e)
23+
```
24+
25+
</eslint-playground>
26+
27+
## 🔧 Options
28+
29+
This rule has an option.
30+
31+
```jsonc
32+
{
33+
"rules": {
34+
"es-x/no-error-iserror": [
35+
"error",
36+
{
37+
"allowTestedProperty": false
38+
}
39+
]
40+
}
41+
}
42+
```
43+
44+
### allowTestedProperty: boolean
45+
46+
Configure the allowTestedProperty mode for only this rule.
47+
This is prior to the `settings['es-x'].allowTestedProperty` setting.
48+
49+
## 📚 References
50+
51+
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-error-iserror.js)
52+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-error-iserror.js)
53+
54+
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
title: "es-x/no-nonstandard-error-properties"
3+
description: "disallow non-standard static properties on `Error` class"
4+
---
5+
6+
# es-x/no-nonstandard-error-properties
7+
> disallow non-standard static properties on `Error` class
8+
9+
- ❗ <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
10+
11+
This rule reports non-standard static properties on `Error` class as errors.
12+
13+
## 💡 Examples
14+
15+
⛔ Examples of **incorrect** code for this rule:
16+
17+
<eslint-playground type="bad">
18+
19+
```js
20+
/*eslint es-x/no-nonstandard-error-properties: error */
21+
Error.unknown();
22+
```
23+
24+
</eslint-playground>
25+
26+
## 🔧 Options
27+
28+
This rule has an option.
29+
30+
```jsonc
31+
{
32+
"rules": {
33+
"es-x/no-nonstandard-error-properties": [
34+
"error",
35+
{
36+
"allow": [],
37+
"allowTestedProperty": false
38+
}
39+
]
40+
}
41+
}
42+
```
43+
44+
### allow: string[]
45+
46+
An array of non-standard property names to allow.
47+
48+
### allowTestedProperty: boolean
49+
50+
Configure the allowTestedProperty mode for only this rule.
51+
This is prior to the `settings['es-x'].allowTestedProperty` setting.
52+
53+
## 📚 References
54+
55+
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-nonstandard-error-properties.js)
56+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-nonstandard-error-properties.js)

lib/configs/flat/no-new-in-esnext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = {
1212
},
1313
rules: {
1414
"es-x/no-array-fromasync": "error",
15+
"es-x/no-error-iserror": "error",
1516
"es-x/no-dataview-prototype-getfloat16-setfloat16": "error",
1617
"es-x/no-dynamic-import-options": "error",
1718
"es-x/no-float16array": "error",

lib/configs/no-new-in-esnext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = {
88
plugins: ["es-x"],
99
rules: {
1010
"es-x/no-array-fromasync": "error",
11+
"es-x/no-error-iserror": "error",
1112
"es-x/no-dataview-prototype-getfloat16-setfloat16": "error",
1213
"es-x/no-dynamic-import-options": "error",
1314
"es-x/no-float16array": "error",

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ module.exports = {
202202
"no-dynamic-import": require("./rules/no-dynamic-import"),
203203
"no-dynamic-import-options": require("./rules/no-dynamic-import-options"),
204204
"no-error-cause": require("./rules/no-error-cause"),
205+
"no-error-iserror": require("./rules/no-error-iserror"),
205206
"no-escape-unescape": require("./rules/no-escape-unescape"),
206207
"no-exponential-operators": require("./rules/no-exponential-operators"),
207208
"no-export-ns-from": require("./rules/no-export-ns-from"),
@@ -285,6 +286,7 @@ module.exports = {
285286
"no-nonstandard-dataview-prototype-properties": require("./rules/no-nonstandard-dataview-prototype-properties"),
286287
"no-nonstandard-date-properties": require("./rules/no-nonstandard-date-properties"),
287288
"no-nonstandard-date-prototype-properties": require("./rules/no-nonstandard-date-prototype-properties"),
289+
"no-nonstandard-error-properties": require("./rules/no-nonstandard-error-properties"),
288290
"no-nonstandard-finalizationregistry-properties": require("./rules/no-nonstandard-finalizationregistry-properties"),
289291
"no-nonstandard-finalizationregistry-prototype-properties": require("./rules/no-nonstandard-finalizationregistry-prototype-properties"),
290292
"no-nonstandard-function-properties": require("./rules/no-nonstandard-function-properties"),

lib/rules/no-error-iserror.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"use strict"
2+
3+
const {
4+
defineStaticPropertiesHandler,
5+
} = require("../util/define-static-properties-handler")
6+
7+
module.exports = {
8+
meta: {
9+
docs: {
10+
description: "disallow the `Error.isError` method.",
11+
category: "ES2026",
12+
recommended: false,
13+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-error-iserror.html",
14+
},
15+
fixable: null,
16+
messages: {
17+
forbidden: "ES2026 '{{name}}' method is forbidden.",
18+
},
19+
schema: [
20+
{
21+
type: "object",
22+
properties: { allowTestedProperty: { type: "boolean" } },
23+
additionalProperties: false,
24+
},
25+
],
26+
type: "problem",
27+
},
28+
create(context) {
29+
return defineStaticPropertiesHandler(context, {
30+
Error: { isError: "function" },
31+
})
32+
},
33+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"use strict"
2+
3+
const {
4+
defineNonstandardStaticPropertiesHandler,
5+
} = require("../util/define-nonstandard-static-properties-handler")
6+
const { errorProperties } = require("../util/well-known-properties")
7+
8+
module.exports = {
9+
meta: {
10+
docs: {
11+
description:
12+
"disallow non-standard static properties on `Error` class",
13+
category: "nonstandard",
14+
recommended: false,
15+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-nonstandard-error-properties.html",
16+
},
17+
fixable: null,
18+
messages: {
19+
forbidden: "Non-standard '{{name}}' property is forbidden.",
20+
},
21+
schema: [
22+
{
23+
type: "object",
24+
properties: {
25+
allow: {
26+
type: "array",
27+
items: { type: "string" },
28+
uniqueItems: true,
29+
},
30+
allowTestedProperty: { type: "boolean" },
31+
},
32+
additionalProperties: false,
33+
},
34+
],
35+
type: "problem",
36+
},
37+
create(context) {
38+
/** @type {Set<string>} */
39+
const allows = new Set([
40+
...(context.options[0]?.allow || []),
41+
...errorProperties,
42+
])
43+
return defineNonstandardStaticPropertiesHandler(context, {
44+
Error: allows,
45+
})
46+
},
47+
}

lib/util/type-checker/es-types.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,17 @@ const WELLKNOWN_GLOBALS = {
434434
SQRT2: { type: "Number" },
435435
},
436436
},
437+
Error: {
438+
type: "Function",
439+
return: { type: "Error" },
440+
prototypeType: "Error",
441+
/** @type {Record<import('./types').ErrorProperty,TypeInfo|undefined>} */
442+
properties: {
443+
isError: { type: "Function", return: { type: "Boolean" } },
444+
captureStackTrace: { type: "Function" },
445+
stackTraceLimit: { type: "Number" },
446+
},
447+
},
437448
}
438449

439450
/** @type {Record<import('./types').ObjectPrototypeProperty, TypeInfo>} */

lib/util/type-checker/types.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export type TypeName =
4242
| "WeakSet"
4343
| "WeakRef"
4444
| "FinalizationRegistry"
45+
| "Error"
4546
| "null"
4647
| "undefined";
4748

@@ -127,7 +128,9 @@ export type SetPrototypeProperty = Exclude<
127128
keyof Set<any>,
128129
ExcludePrototypeProperty
129130
>;
130-
export type RegExpProperty = Exclude<keyof typeof RegExp, ExcludeProperty>;
131+
export type RegExpProperty =
132+
| Exclude<keyof typeof RegExp, ExcludeProperty>
133+
| "escape";
131134
export type RegExpPrototypeProperty = Exclude<
132135
keyof RegExp,
133136
ExcludePrototypeProperty
@@ -215,3 +218,6 @@ export type TypedArrayPrototypeProperty = Exclude<
215218
ExcludePrototypeProperty | "BYTES_PER_ELEMENT"
216219
>;
217220
export type MathProperty = Exclude<keyof typeof Math, ExcludeProperty>;
221+
export type ErrorProperty =
222+
| Exclude<keyof typeof Error, ExcludeProperty | "prepareStackTrace">
223+
| "isError";

0 commit comments

Comments
 (0)