Skip to content

Commit fe21f80

Browse files
committed
add rule
1 parent f8d82fd commit fe21f80

File tree

10 files changed

+113
-1
lines changed

10 files changed

+113
-1
lines changed

docs/configs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,7 @@ export default [
10341034
disallow proposal ES2026 [Explicit Resource Management](https://github.com/tc39/proposal-explicit-resource-management)\
10351035
⚠️ This config will be changed in the minor versions of this plugin.
10361036

1037-
This configs includes rules for [es-x/no-asyncdisposablestack](../rules/no-asyncdisposablestack.md), [es-x/no-disposablestack](../rules/no-disposablestack.md), and [es-x/no-suppressederror](../rules/no-suppressederror.md).
1037+
This configs includes rules for [es-x/no-asyncdisposablestack](../rules/no-asyncdisposablestack.md), [es-x/no-disposablestack](../rules/no-disposablestack.md), [es-x/no-suppressederror](../rules/no-suppressederror.md), and [es-x/no-using-declarations](../rules/no-using-declarations.md).
10381038

10391039
### [Config (Flat Config)]
10401040

docs/rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ There is a config that enables the rules in this category: [`no-new-in-esnext`]
1414
| [es-x/no-asyncdisposablestack](./no-asyncdisposablestack.md) | disallow the `AsyncDisposableStack` class. | |
1515
| [es-x/no-disposablestack](./no-disposablestack.md) | disallow the `DisposableStack` class. | |
1616
| [es-x/no-suppressederror](./no-suppressederror.md) | disallow the `SuppressedError` class. | |
17+
| [es-x/no-using-declarations](./no-using-declarations.md) | disallow `using` and `await using` declarations. | |
1718

1819
## ES2025
1920

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: "es-x/no-using-declarations"
3+
description: "disallow `using` and `await using` declarations"
4+
---
5+
6+
# es-x/no-using-declarations
7+
> disallow `using` and `await using` declarations
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-explicit-resource-management] and [no-new-in-esnext]
11+
12+
This rule reports ES2026 [`using` and `await using` declarations](https://github.com/tc39/proposal-explicit-resource-management) 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-using-declarations: error */
22+
23+
// async disposal
24+
async function * g() {
25+
using stream = acquireStream(); // block-scoped critical resource
26+
// ...
27+
} // cleanup
28+
29+
{
30+
await using obj = g(); // block-scoped declaration
31+
const r = await obj.next();
32+
} // calls finally blocks in `g`
33+
```
34+
35+
</eslint-playground>
36+
37+
## 📚 References
38+
39+
- [Rule source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/lib/rules/no-using-declarations.js)
40+
- [Test source](https://github.com/eslint-community/eslint-plugin-es-x/blob/master/tests/lib/rules/no-using-declarations.js)
41+
42+
[no-explicit-resource-management]: ../configs/index.md#no-explicit-resource-management
43+
[no-new-in-esnext]: ../configs/index.md#no-new-in-esnext

lib/configs/flat/no-explicit-resource-management.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ module.exports = {
1414
"es-x/no-asyncdisposablestack": "error",
1515
"es-x/no-disposablestack": "error",
1616
"es-x/no-suppressederror": "error",
17+
"es-x/no-using-declarations": "error",
1718
},
1819
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = {
1515
"es-x/no-asyncdisposablestack": "error",
1616
"es-x/no-disposablestack": "error",
1717
"es-x/no-suppressederror": "error",
18+
"es-x/no-using-declarations": "error",
1819
"es-x/no-dataview-prototype-getfloat16-setfloat16": "error",
1920
"es-x/no-dynamic-import-options": "error",
2021
"es-x/no-float16array": "error",

lib/configs/no-explicit-resource-management.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ module.exports = {
1010
"es-x/no-asyncdisposablestack": "error",
1111
"es-x/no-disposablestack": "error",
1212
"es-x/no-suppressederror": "error",
13+
"es-x/no-using-declarations": "error",
1314
},
1415
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
"es-x/no-asyncdisposablestack": "error",
1212
"es-x/no-disposablestack": "error",
1313
"es-x/no-suppressederror": "error",
14+
"es-x/no-using-declarations": "error",
1415
"es-x/no-dataview-prototype-getfloat16-setfloat16": "error",
1516
"es-x/no-dynamic-import-options": "error",
1617
"es-x/no-float16array": "error",

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ module.exports = {
460460
"no-trailing-function-commas": require("./rules/no-trailing-function-commas"),
461461
"no-typed-arrays": require("./rules/no-typed-arrays"),
462462
"no-unicode-codepoint-escapes": require("./rules/no-unicode-codepoint-escapes"),
463+
"no-using-declarations": require("./rules/no-using-declarations"),
463464
"no-weak-map": require("./rules/no-weak-map"),
464465
"no-weak-set": require("./rules/no-weak-set"),
465466
"no-weakrefs": require("./rules/no-weakrefs"),

lib/rules/no-using-declarations.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"use strict"
2+
3+
module.exports = {
4+
meta: {
5+
docs: {
6+
description: "disallow `using` and `await using` declarations",
7+
category: "ES2026",
8+
proposal: "explicit-resource-management",
9+
recommended: false,
10+
url: "http://eslint-community.github.io/eslint-plugin-es-x/rules/no-using-declarations.html",
11+
},
12+
fixable: null,
13+
messages: {
14+
forbidden: "ES2026 '{{kind}}' declarations are forbidden.",
15+
},
16+
schema: [],
17+
type: "problem",
18+
},
19+
create(context) {
20+
return {
21+
VariableDeclaration(node) {
22+
if (node.kind === "using" || node.kind === "await using") {
23+
context.report({
24+
node,
25+
messageId: "forbidden",
26+
data: {
27+
kind: node.kind,
28+
},
29+
})
30+
}
31+
},
32+
}
33+
},
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"use strict"
2+
3+
const RuleTester = require("../../tester")
4+
const rule = require("../../../lib/rules/no-using-declarations.js")
5+
6+
// if (!RuleTester.isSupported(2026)) {
7+
// //eslint-disable-next-line no-console
8+
// console.log("Skip the tests of no-using-declarations.")
9+
// return
10+
// }
11+
12+
new RuleTester({
13+
languageOptions: {
14+
sourceType: "module",
15+
parser: require("@typescript-eslint/parser"), // espree does not support `using` yet.
16+
},
17+
}).run("no-using-declarations", rule, {
18+
valid: ["let x = y", "const x = y", "var x = y", "const x = await y"],
19+
invalid: [
20+
{
21+
code: "using x = y",
22+
errors: ["ES2026 'using' declarations are forbidden."],
23+
},
24+
{
25+
code: "await using x = y",
26+
errors: ["ES2026 'await using' declarations are forbidden."],
27+
},
28+
],
29+
})

0 commit comments

Comments
 (0)