Skip to content

Commit 4b42d75

Browse files
committed
refactor(linter): improve support for const/using declarations
1 parent 2f68b7b commit 4b42d75

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

crates/oxc_linter/src/rules/eslint/no_const_assign.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ declare_oxc_lint!(
5353
impl Rule for NoConstAssign {
5454
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
5555
match node.kind() {
56-
AstKind::VariableDeclaration(var_decl) if var_decl.kind.is_const() => {
56+
AstKind::VariableDeclaration(var_decl)
57+
if var_decl.kind.is_const() || var_decl.kind.is_using() =>
58+
{
5759
for decl in &var_decl.declarations {
5860
for ident in decl.id.get_binding_identifiers() {
5961
check_symbol(ident.symbol_id(), ctx);
@@ -122,6 +124,10 @@ fn test() {
122124
("const a = 1; { let a = 2; { a += 1; } }", None),
123125
("const foo = 1;let bar;bar[foo ?? foo] = 42;", None),
124126
("const FOO = 1; ({ files = FOO } = arg1); ", None),
127+
("using x = foo();", None),
128+
("await using x = foo();", None),
129+
("using x = foo(); bar(x);", None),
130+
("await using x = foo(); bar(x);", None),
125131
];
126132

127133
let fail = vec![
@@ -143,6 +149,12 @@ fn test() {
143149
("const [a, b, ...[c, ...d]] = [1, 2, 3, 4, 5]; d = 123", None),
144150
("const d = 123; [a, b, ...[c, ...d]] = [1, 2, 3, 4, 5]", None),
145151
("const b = 0; ({a, ...b} = {a: 1, c: 2, d: 3})", None),
152+
("using x = foo(); x = 1;", None),
153+
("await using x = foo(); x = 1;", None),
154+
("using x = foo(); x ??= bar();", None),
155+
("await using x = foo(); x ||= bar();", None),
156+
("using x = foo(); [x, y] = bar();", None),
157+
("await using x = foo(); [x = baz, y] = bar();", None),
146158
];
147159

148160
Tester::new(NoConstAssign::NAME, NoConstAssign::PLUGIN, pass, fail).test_and_snapshot();

crates/oxc_linter/src/snapshots/eslint_no_const_assign.snap

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,51 @@ source: crates/oxc_linter/src/tester.rs
136136
· │ ╰── b is re-assigned here
137137
· ╰── b is declared here as const
138138
╰────
139+
140+
eslint(no-const-assign): Unexpected re-assignment of const variable x
141+
╭─[no_const_assign.tsx:1:7]
142+
1using x = foo(); x = 1;
143+
· ┬ ┬
144+
· │ ╰── x is re-assigned here
145+
· ╰── x is declared here as const
146+
╰────
147+
148+
eslint(no-const-assign): Unexpected re-assignment of const variable x
149+
╭─[no_const_assign.tsx:1:13]
150+
1await using x = foo(); x = 1;
151+
· ┬ ┬
152+
· │ ╰── x is re-assigned here
153+
· ╰── x is declared here as const
154+
╰────
155+
156+
eslint(no-const-assign): Unexpected re-assignment of const variable x
157+
╭─[no_const_assign.tsx:1:7]
158+
1using x = foo(); x ??= bar();
159+
· ┬ ┬
160+
· │ ╰── x is re-assigned here
161+
· ╰── x is declared here as const
162+
╰────
163+
164+
eslint(no-const-assign): Unexpected re-assignment of const variable x
165+
╭─[no_const_assign.tsx:1:13]
166+
1await using x = foo(); x ||= bar();
167+
· ┬ ┬
168+
· │ ╰── x is re-assigned here
169+
· ╰── x is declared here as const
170+
╰────
171+
172+
eslint(no-const-assign): Unexpected re-assignment of const variable x
173+
╭─[no_const_assign.tsx:1:7]
174+
1using x = foo(); [x, y] = bar();
175+
· ┬ ┬
176+
· │ ╰── x is re-assigned here
177+
· ╰── x is declared here as const
178+
╰────
179+
180+
eslint(no-const-assign): Unexpected re-assignment of const variable x
181+
╭─[no_const_assign.tsx:1:13]
182+
1await using x = foo(); [x = baz, y] = bar();
183+
· ┬ ┬
184+
· │ ╰── x is re-assigned here
185+
· ╰── x is declared here as const
186+
╰────

0 commit comments

Comments
 (0)