Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/oxc_linter/src/rules/eslint/no_const_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ declare_oxc_lint!(
impl Rule for NoConstAssign {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
AstKind::VariableDeclaration(var_decl) if var_decl.kind.is_const() => {
AstKind::VariableDeclaration(var_decl)
if var_decl.kind.is_const() || var_decl.kind.is_using() =>
{
for decl in &var_decl.declarations {
for ident in decl.id.get_binding_identifiers() {
check_symbol(ident.symbol_id(), ctx);
Expand Down Expand Up @@ -122,6 +124,10 @@ fn test() {
("const a = 1; { let a = 2; { a += 1; } }", None),
("const foo = 1;let bar;bar[foo ?? foo] = 42;", None),
("const FOO = 1; ({ files = FOO } = arg1); ", None),
("using x = foo();", None),
("await using x = foo();", None),
("using x = foo(); bar(x);", None),
("await using x = foo(); bar(x);", None),
];

let fail = vec![
Expand All @@ -143,6 +149,12 @@ fn test() {
("const [a, b, ...[c, ...d]] = [1, 2, 3, 4, 5]; d = 123", None),
("const d = 123; [a, b, ...[c, ...d]] = [1, 2, 3, 4, 5]", None),
("const b = 0; ({a, ...b} = {a: 1, c: 2, d: 3})", None),
("using x = foo(); x = 1;", None),
("await using x = foo(); x = 1;", None),
("using x = foo(); x ??= bar();", None),
("await using x = foo(); x ||= bar();", None),
("using x = foo(); [x, y] = bar();", None),
("await using x = foo(); [x = baz, y] = bar();", None),
];

Tester::new(NoConstAssign::NAME, NoConstAssign::PLUGIN, pass, fail).test_and_snapshot();
Expand Down
48 changes: 48 additions & 0 deletions crates/oxc_linter/src/snapshots/eslint_no_const_assign.snap
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,51 @@ source: crates/oxc_linter/src/tester.rs
· │ ╰── b is re-assigned here
· ╰── b is declared here as const
╰────

⚠ eslint(no-const-assign): Unexpected re-assignment of const variable x
╭─[no_const_assign.tsx:1:7]
1 │ using x = foo(); x = 1;
· ┬ ┬
· │ ╰── x is re-assigned here
· ╰── x is declared here as const
╰────

⚠ eslint(no-const-assign): Unexpected re-assignment of const variable x
╭─[no_const_assign.tsx:1:13]
1 │ await using x = foo(); x = 1;
· ┬ ┬
· │ ╰── x is re-assigned here
· ╰── x is declared here as const
╰────

⚠ eslint(no-const-assign): Unexpected re-assignment of const variable x
╭─[no_const_assign.tsx:1:7]
1 │ using x = foo(); x ??= bar();
· ┬ ┬
· │ ╰── x is re-assigned here
· ╰── x is declared here as const
╰────

⚠ eslint(no-const-assign): Unexpected re-assignment of const variable x
╭─[no_const_assign.tsx:1:13]
1 │ await using x = foo(); x ||= bar();
· ┬ ┬
· │ ╰── x is re-assigned here
· ╰── x is declared here as const
╰────

⚠ eslint(no-const-assign): Unexpected re-assignment of const variable x
╭─[no_const_assign.tsx:1:7]
1 │ using x = foo(); [x, y] = bar();
· ┬ ┬
· │ ╰── x is re-assigned here
· ╰── x is declared here as const
╰────

⚠ eslint(no-const-assign): Unexpected re-assignment of const variable x
╭─[no_const_assign.tsx:1:13]
1 │ await using x = foo(); [x = baz, y] = bar();
· ┬ ┬
· │ ╰── x is re-assigned here
· ╰── x is declared here as const
╰────
Loading