Skip to content

Commit d133385

Browse files
ota-meshi9romise
andauthored
feat: add support for using (#804)
Co-authored-by: Vida Xie <vida_2020@163.com>
1 parent b30a26b commit d133385

16 files changed

Lines changed: 878 additions & 146 deletions

packages/eslint-plugin/rules/indent/README.md

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ Level of indentation denotes the multiple of the indent specified. Example:
110110

111111
- Indent of 4 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 8 spaces.
112112
- Indent of 2 spaces with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 4 spaces.
113-
- Indent of 2 spaces with `VariableDeclarator` set to `{"var": 2, "let": 2, "const": 3}` will indent the multi-line variable declarations with 4 spaces for `var` and `let`, 6 spaces for `const` statements.
113+
- Indent of 2 spaces with `VariableDeclarator` set to `{"var": 2, "let": 2, "const": 3, "using": 3}` will indent the multi-line variable declarations with 4 spaces for `var` and `let`, 6 spaces for `const`, `using` and `await using` statements.
114114
- Indent of tab with `VariableDeclarator` set to `2` will indent the multi-line variable declarations with 2 tabs.
115115
- Indent of 2 spaces with `SwitchCase` set to `0` will not indent `case` clauses with respect to `switch` statements.
116116
- Indent of 2 spaces with `SwitchCase` set to `1` will indent `case` clauses with 2 spaces with respect to `switch` statements.
@@ -256,6 +256,12 @@ let d,
256256
const g = 1,
257257
h = 2,
258258
i = 3;
259+
using j = foo(),
260+
k = bar(),
261+
l = baz();
262+
await using j = foo(),
263+
k = bar(),
264+
l = baz();
259265
```
260266

261267
:::
@@ -276,6 +282,12 @@ let d,
276282
const g = 1,
277283
h = 2,
278284
i = 3;
285+
using j = foo(),
286+
k = bar(),
287+
l = baz();
288+
await using j = foo(),
289+
k = bar(),
290+
l = baz();
279291
```
280292

281293
:::
@@ -296,6 +308,12 @@ let d,
296308
const g = 1,
297309
h = 2,
298310
i = 3;
311+
using j = foo(),
312+
k = bar(),
313+
l = baz();
314+
await using j = foo(),
315+
k = bar(),
316+
l = baz();
299317
```
300318

301319
:::
@@ -316,6 +334,12 @@ let d,
316334
const g = 1,
317335
h = 2,
318336
i = 3;
337+
using j = foo(),
338+
k = bar(),
339+
l = baz();
340+
await using j = foo(),
341+
k = bar(),
342+
l = baz();
319343
```
320344

321345
:::
@@ -336,16 +360,22 @@ let d,
336360
const g = 1,
337361
h = 2,
338362
i = 3;
363+
using j = foo(),
364+
k = bar(),
365+
l = baz();
366+
await using j = foo(),
367+
k = bar(),
368+
l = baz();
339369
```
340370

341371
:::
342372

343-
Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }` options:
373+
Examples of **correct** code for this rule with the `2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3, "using": "first" } }` options:
344374

345375
::: correct
346376

347377
```js
348-
/* eslint @stylistic/indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3 } }] */
378+
/* eslint @stylistic/indent: ["error", 2, { "VariableDeclarator": { "var": 2, "let": 2, "const": 3, "using": "first" } }] */
349379

350380
var a,
351381
b,
@@ -356,6 +386,12 @@ let d,
356386
const g = 1,
357387
h = 2,
358388
i = 3;
389+
using j = foo(),
390+
k = bar(),
391+
l = baz();
392+
await using j = foo(),
393+
k = bar(),
394+
l = baz();
359395
```
360396

361397
:::

packages/eslint-plugin/rules/indent/indent._ts_.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,24 @@ const map2 = Object.keys(map)
827827
`,
828828
options: [2],
829829
},
830+
{
831+
code: $`
832+
using a = foo(),
833+
b = bar();
834+
await using c = baz(),
835+
d = qux();
836+
`,
837+
options: [2, { VariableDeclarator: 1 }],
838+
},
839+
{
840+
code: $`
841+
using a = foo(),
842+
b = bar();
843+
await using c = baz(),
844+
d = qux();
845+
`,
846+
options: [2, { VariableDeclarator: { using: 'first' } }],
847+
},
830848
],
831849
invalid: [
832850
...individualNodeTests.invalid!,
@@ -2039,5 +2057,43 @@ class Foo {
20392057
},
20402058
],
20412059
},
2060+
{
2061+
code: $`
2062+
using a = foo(),
2063+
b = bar();
2064+
await using c = baz(),
2065+
d = qux();
2066+
`,
2067+
output: $`
2068+
using a = foo(),
2069+
b = bar();
2070+
await using c = baz(),
2071+
d = qux();
2072+
`,
2073+
options: [2, { VariableDeclarator: 'first' }],
2074+
errors: [
2075+
{ messageId: 'wrongIndentation', data: { expected: '6 spaces', actual: 2 } },
2076+
{ messageId: 'wrongIndentation', data: { expected: '12 spaces', actual: 2 } },
2077+
],
2078+
},
2079+
{
2080+
code: $`
2081+
using a = foo(),
2082+
b = bar();
2083+
await using c = baz(),
2084+
d = qux();
2085+
`,
2086+
output: $`
2087+
using a = foo(),
2088+
b = bar();
2089+
await using c = baz(),
2090+
d = qux();
2091+
`,
2092+
options: [2, { VariableDeclarator: { using: 1 } }],
2093+
errors: [
2094+
{ messageId: 'wrongIndentation', data: { expected: '2 spaces', actual: 6 } },
2095+
{ messageId: 'wrongIndentation', data: { expected: '2 spaces', actual: 12 } },
2096+
],
2097+
},
20422098
],
20432099
})

packages/eslint-plugin/rules/indent/indent.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ export default createRule<RuleOptions, MessageIds>({
578578
var: ELEMENT_LIST_SCHEMA,
579579
let: ELEMENT_LIST_SCHEMA,
580580
const: ELEMENT_LIST_SCHEMA,
581+
using: ELEMENT_LIST_SCHEMA,
581582
},
582583
additionalProperties: false,
583584
},
@@ -711,6 +712,7 @@ export default createRule<RuleOptions, MessageIds>({
711712
var: DEFAULT_VARIABLE_INDENT as number | 'first',
712713
let: DEFAULT_VARIABLE_INDENT as number | 'first',
713714
const: DEFAULT_VARIABLE_INDENT as number | 'first',
715+
using: DEFAULT_VARIABLE_INDENT as number | 'first',
714716
},
715717
outerIIFEBody: 1,
716718
FunctionDeclaration: {
@@ -758,6 +760,7 @@ export default createRule<RuleOptions, MessageIds>({
758760
var: userOptions.VariableDeclarator,
759761
let: userOptions.VariableDeclarator,
760762
const: userOptions.VariableDeclarator,
763+
using: userOptions.VariableDeclarator,
761764
}
762765
}
763766
}
@@ -1749,14 +1752,15 @@ export default createRule<RuleOptions, MessageIds>({
17491752
if (node.declarations.length === 0)
17501753
return
17511754

1752-
let variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, node.kind)
1753-
? options.VariableDeclarator[node.kind as keyof typeof options.VariableDeclarator]
1755+
const kind = node.kind === 'await using' ? 'using' : node.kind
1756+
let variableIndent = Object.prototype.hasOwnProperty.call(options.VariableDeclarator, kind)
1757+
? options.VariableDeclarator[kind]
17541758
: DEFAULT_VARIABLE_INDENT
17551759

17561760
const firstToken = sourceCode.getFirstToken(node)!
17571761
const lastToken = sourceCode.getLastToken(node)!
17581762

1759-
if (options.VariableDeclarator[node.kind as keyof typeof options.VariableDeclarator] === 'first') {
1763+
if (options.VariableDeclarator[kind] === 'first') {
17601764
if (node.declarations.length > 1) {
17611765
addElementListIndent(
17621766
node.declarations,

packages/eslint-plugin/rules/indent/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* GENERATED, DO NOT EDIT DIRECTLY */
22

3-
/* @checksum: D7D9ke3d40qr6hi89XWsahsr6YW8HCiN2-GNt5pD-lA */
3+
/* @checksum: OXKn3_mfEuRvCVJ6qv5__ZiBZDQpamanEmwuqMCGavo */
44

55
export type IndentSchema0 = 'tab' | number
66

@@ -12,6 +12,7 @@ export interface IndentSchema1 {
1212
var?: number | ('first' | 'off')
1313
let?: number | ('first' | 'off')
1414
const?: number | ('first' | 'off')
15+
using?: number | ('first' | 'off')
1516
}
1617
outerIIFEBody?: number | 'off'
1718
MemberExpression?: number | 'off'

packages/eslint-plugin/rules/keyword-spacing/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ However, if you want to enforce the style of following spacing cases, please ref
2929

3030
## Rule Details
3131

32-
This rule enforces consistent spacing around keywords and keyword-like tokens: `as` (in module declarations), `async` (of async functions), `await` (of await expressions), `break`, `case`, `catch`, `class`, `const`, `continue`, `debugger`, `default`, `delete`, `do`, `else`, `export`, `extends`, `finally`, `for`, `from` (in module declarations), `function`, `get` (of getters), `if`, `import`, `in` (in for-in statements), `let`, `new`, `of` (in for-of statements), `return`, `set` (of setters), `static`, `super`, `switch`, `this`, `throw`, `try`, `typeof`, `var`, `void`, `while`, `with`, and `yield`. This rule is designed carefully not to conflict with other spacing rules: it does not apply to spacing where other rules report problems.
32+
This rule enforces consistent spacing around keywords and keyword-like tokens: `as` (in module declarations), `async` (of async functions), `await` (of await expressions), `break`, `case`, `catch`, `class`, `const`, `continue`, `debugger`, `default`, `delete`, `do`, `else`, `export`, `extends`, `finally`, `for`, `from` (in module declarations), `function`, `get` (of getters), `if`, `import`, `in` (in for-in statements), `let`, `new`, `of` (in for-of statements), `return`, `set` (of setters), `static`, `super`, `switch`, `this`, `throw`, `try`, `typeof`, `using`, `var`, `void`, `while`, `with`, and `yield`. This rule is designed carefully not to conflict with other spacing rules: it does not apply to spacing where other rules report problems.
3333

3434
## Options
3535

packages/eslint-plugin/rules/keyword-spacing/keyword-spacing._js_.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,65 @@ run<RuleOptions, MessageIds>({
21352135
errors: unexpectedBeforeAndAfter('const'),
21362136
},
21372137

2138+
// ----------------------------------------------------------------------
2139+
// using
2140+
// ----------------------------------------------------------------------
2141+
2142+
{
2143+
code: '{}using a = b',
2144+
output: '{} using a = b',
2145+
parserOptions: { ecmaVersion: 2026 },
2146+
errors: expectedBefore('using'),
2147+
},
2148+
{
2149+
code: '{} using a = b',
2150+
output: '{}using a = b',
2151+
options: [NEITHER],
2152+
parserOptions: { ecmaVersion: 2026 },
2153+
errors: unexpectedBefore('using'),
2154+
},
2155+
{
2156+
code: '{}using a = b',
2157+
output: '{} using a = b',
2158+
options: [override('using', BOTH)],
2159+
parserOptions: { ecmaVersion: 2026 },
2160+
errors: expectedBefore('using'),
2161+
},
2162+
{
2163+
code: '{} using a = b',
2164+
output: '{}using a = b',
2165+
options: [override('using', NEITHER)],
2166+
parserOptions: { ecmaVersion: 2026 },
2167+
errors: unexpectedBefore('using'),
2168+
},
2169+
{
2170+
code: '{}await using a = b',
2171+
output: '{} await using a = b',
2172+
parserOptions: { ecmaVersion: 2026 },
2173+
errors: expectedBefore('await'),
2174+
},
2175+
{
2176+
code: '{} await using a = b',
2177+
output: '{}await using a = b',
2178+
options: [NEITHER],
2179+
parserOptions: { ecmaVersion: 2026 },
2180+
errors: unexpectedBefore('await'),
2181+
},
2182+
{
2183+
code: '{}await using a = b',
2184+
output: '{} await using a = b',
2185+
options: [override('await', BOTH)],
2186+
parserOptions: { ecmaVersion: 2026 },
2187+
errors: expectedBefore('await'),
2188+
},
2189+
{
2190+
code: '{} await using a = b',
2191+
output: '{}await using a = b',
2192+
options: [override('await', NEITHER)],
2193+
parserOptions: { ecmaVersion: 2026 },
2194+
errors: unexpectedBefore('await'),
2195+
},
2196+
21382197
// ----------------------------------------------------------------------
21392198
// continue
21402199
// ----------------------------------------------------------------------

packages/eslint-plugin/rules/keyword-spacing/keyword-spacing._ts_.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,5 +579,60 @@ run<RuleOptions, MessageIds>({
579579
{ messageId: 'unexpectedAfter', data: { value: 'from' } },
580580
],
581581
},
582+
583+
// ----------------------------------------------------------------------
584+
// using
585+
// ----------------------------------------------------------------------
586+
587+
{
588+
code: '{}using a = b',
589+
output: '{} using a = b',
590+
errors: expectedBefore('using'),
591+
},
592+
{
593+
code: '{} using a = b',
594+
output: '{}using a = b',
595+
options: [NEITHER],
596+
errors: unexpectedBefore('using'),
597+
},
598+
{
599+
code: '{}using a = b',
600+
output: '{} using a = b',
601+
options: [overrides('using', BOTH)],
602+
errors: expectedBefore('using'),
603+
},
604+
{
605+
code: '{} using a = b',
606+
output: '{}using a = b',
607+
options: [overrides('using', NEITHER)],
608+
errors: unexpectedBefore('using'),
609+
},
610+
{
611+
code: '{}await using a = b',
612+
output: '{} await using a = b',
613+
parserOptions: { ecmaVersion: 2026 },
614+
errors: expectedBefore('await'),
615+
},
616+
{
617+
code: '{} await using a = b',
618+
output: '{}await using a = b',
619+
options: [NEITHER],
620+
parserOptions: { ecmaVersion: 2026 },
621+
errors: unexpectedBefore('await'),
622+
},
623+
{
624+
code: '{}await using a = b',
625+
output: '{} await using a = b',
626+
options: [overrides('await', BOTH)],
627+
parserOptions: { ecmaVersion: 2026 },
628+
errors: expectedBefore('await'),
629+
},
630+
{
631+
code: '{} await using a = b',
632+
output: '{}await using a = b',
633+
options: [overrides('await', NEITHER)],
634+
parserOptions: { ecmaVersion: 2026 },
635+
errors: unexpectedBefore('await'),
636+
},
582637
],
583638
})

0 commit comments

Comments
 (0)