From aaee07e170bf7eb2334ad0c2b43574539ab9526f Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Thu, 25 Jul 2024 09:32:33 +0000 Subject: [PATCH] feat(ast): add `AstKind::AssignmentTargetPattern`, `AstKind::ArrayAssignmentTarget` and `AstKind::ObjectAssignmentTarget` (#4456) close: #4435 --- crates/oxc_ast/src/ast_kind_impl.rs | 3 +++ crates/oxc_ast/src/generated/ast_kind.rs | 9 +++++++++ crates/oxc_ast/src/generated/visit.rs | 11 +++++++++-- crates/oxc_ast/src/generated/visit_mut.rs | 11 +++++++++-- .../eslint/no_unused_private_class_members.rs | 4 +++- .../unicorn/prefer_dom_node_text_content.rs | 16 +++++++++++++--- .../destructuring/array-assignment.snap | 6 +++--- .../destructuring/object-assignment.snap | 2 +- tasks/ast_codegen/src/generators/ast_kind.rs | 5 +---- 9 files changed, 51 insertions(+), 16 deletions(-) diff --git a/crates/oxc_ast/src/ast_kind_impl.rs b/crates/oxc_ast/src/ast_kind_impl.rs index 57763e9015b5a..7e86afa0f1e0d 100644 --- a/crates/oxc_ast/src/ast_kind_impl.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -261,6 +261,9 @@ impl<'a> AstKind<'a> { Self::ArrayExpressionElement(_) => "ArrayExpressionElement".into(), Self::AssignmentTarget(_) => "AssignmentTarget".into(), Self::SimpleAssignmentTarget(_) => "SimpleAssignmentTarget".into(), + Self::AssignmentTargetPattern(_) => "AssignmentTargetPattern".into(), + Self::ArrayAssignmentTarget(_) => "ArrayAssignmentTarget".into(), + Self::ObjectAssignmentTarget(_) => "ObjectAssignmentTarget".into(), Self::AssignmentTargetWithDefault(_) => "AssignmentTargetWithDefault".into(), Self::SpreadElement(_) => "SpreadElement".into(), Self::Elision(_) => "Elision".into(), diff --git a/crates/oxc_ast/src/generated/ast_kind.rs b/crates/oxc_ast/src/generated/ast_kind.rs index d89ca1e512e3d..c1f2493413200 100644 --- a/crates/oxc_ast/src/generated/ast_kind.rs +++ b/crates/oxc_ast/src/generated/ast_kind.rs @@ -41,6 +41,9 @@ pub enum AstType { AssignmentExpression, AssignmentTarget, SimpleAssignmentTarget, + AssignmentTargetPattern, + ArrayAssignmentTarget, + ObjectAssignmentTarget, AssignmentTargetWithDefault, SequenceExpression, Super, @@ -211,6 +214,9 @@ pub enum AstKind<'a> { AssignmentExpression(&'a AssignmentExpression<'a>), AssignmentTarget(&'a AssignmentTarget<'a>), SimpleAssignmentTarget(&'a SimpleAssignmentTarget<'a>), + AssignmentTargetPattern(&'a AssignmentTargetPattern<'a>), + ArrayAssignmentTarget(&'a ArrayAssignmentTarget<'a>), + ObjectAssignmentTarget(&'a ObjectAssignmentTarget<'a>), AssignmentTargetWithDefault(&'a AssignmentTargetWithDefault<'a>), SequenceExpression(&'a SequenceExpression<'a>), Super(&'a Super), @@ -382,6 +388,9 @@ impl<'a> GetSpan for AstKind<'a> { Self::AssignmentExpression(it) => it.span(), Self::AssignmentTarget(it) => it.span(), Self::SimpleAssignmentTarget(it) => it.span(), + Self::AssignmentTargetPattern(it) => it.span(), + Self::ArrayAssignmentTarget(it) => it.span(), + Self::ObjectAssignmentTarget(it) => it.span(), Self::AssignmentTargetWithDefault(it) => it.span(), Self::SequenceExpression(it) => it.span(), Self::Super(it) => it.span(), diff --git a/crates/oxc_ast/src/generated/visit.rs b/crates/oxc_ast/src/generated/visit.rs index 3953493e781eb..d54a0dcebe359 100644 --- a/crates/oxc_ast/src/generated/visit.rs +++ b/crates/oxc_ast/src/generated/visit.rs @@ -2750,6 +2750,8 @@ pub mod walk { visitor: &mut V, it: &AssignmentTargetPattern<'a>, ) { + let kind = AstKind::AssignmentTargetPattern(visitor.alloc(it)); + visitor.enter_node(kind); match it { AssignmentTargetPattern::ArrayAssignmentTarget(it) => { visitor.visit_array_assignment_target(it) @@ -2758,6 +2760,7 @@ pub mod walk { visitor.visit_object_assignment_target(it) } } + visitor.leave_node(kind); } #[inline] @@ -2765,13 +2768,15 @@ pub mod walk { visitor: &mut V, it: &ArrayAssignmentTarget<'a>, ) { - // NOTE: AstKind doesn't exists! + let kind = AstKind::ArrayAssignmentTarget(visitor.alloc(it)); + visitor.enter_node(kind); for elements in it.elements.iter().flatten() { visitor.visit_assignment_target_maybe_default(elements); } if let Some(rest) = &it.rest { visitor.visit_assignment_target_rest(rest); } + visitor.leave_node(kind); } #[inline] @@ -2815,11 +2820,13 @@ pub mod walk { visitor: &mut V, it: &ObjectAssignmentTarget<'a>, ) { - // NOTE: AstKind doesn't exists! + let kind = AstKind::ObjectAssignmentTarget(visitor.alloc(it)); + visitor.enter_node(kind); visitor.visit_assignment_target_properties(&it.properties); if let Some(rest) = &it.rest { visitor.visit_assignment_target_rest(rest); } + visitor.leave_node(kind); } #[inline] diff --git a/crates/oxc_ast/src/generated/visit_mut.rs b/crates/oxc_ast/src/generated/visit_mut.rs index 73aa5d0dd0ec6..08d69375134d4 100644 --- a/crates/oxc_ast/src/generated/visit_mut.rs +++ b/crates/oxc_ast/src/generated/visit_mut.rs @@ -2868,6 +2868,8 @@ pub mod walk_mut { visitor: &mut V, it: &mut AssignmentTargetPattern<'a>, ) { + let kind = AstType::AssignmentTargetPattern; + visitor.enter_node(kind); match it { AssignmentTargetPattern::ArrayAssignmentTarget(it) => { visitor.visit_array_assignment_target(it) @@ -2876,6 +2878,7 @@ pub mod walk_mut { visitor.visit_object_assignment_target(it) } } + visitor.leave_node(kind); } #[inline] @@ -2883,13 +2886,15 @@ pub mod walk_mut { visitor: &mut V, it: &mut ArrayAssignmentTarget<'a>, ) { - // NOTE: AstType doesn't exists! + let kind = AstType::ArrayAssignmentTarget; + visitor.enter_node(kind); for elements in it.elements.iter_mut().flatten() { visitor.visit_assignment_target_maybe_default(elements); } if let Some(rest) = &mut it.rest { visitor.visit_assignment_target_rest(rest); } + visitor.leave_node(kind); } #[inline] @@ -2933,11 +2938,13 @@ pub mod walk_mut { visitor: &mut V, it: &mut ObjectAssignmentTarget<'a>, ) { - // NOTE: AstType doesn't exists! + let kind = AstType::ObjectAssignmentTarget; + visitor.enter_node(kind); visitor.visit_assignment_target_properties(&mut it.properties); if let Some(rest) = &mut it.rest { visitor.visit_assignment_target_rest(rest); } + visitor.leave_node(kind); } #[inline] diff --git a/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs b/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs index bd76f684a1cbc..239d4ad97d741 100644 --- a/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs +++ b/crates/oxc_linter/src/rules/eslint/no_unused_private_class_members.rs @@ -129,7 +129,9 @@ fn is_read(current_node_id: AstNodeId, nodes: &AstNodes) -> bool { AstKind::ForInStatement(_) | AstKind::ForOfStatement(_) | AstKind::AssignmentTargetWithDefault(_) - | AstKind::AssignmentTarget(_), + | AstKind::AssignmentTarget(_) + | AstKind::ObjectAssignmentTarget(_) + | AstKind::ArrayAssignmentTarget(_), ) | (AstKind::SimpleAssignmentTarget(_), AstKind::AssignmentExpression(_)) => { return false; diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_dom_node_text_content.rs b/crates/oxc_linter/src/rules/unicorn/prefer_dom_node_text_content.rs index 3124dfec36a6b..cee899f4e7e4f 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_dom_node_text_content.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_dom_node_text_content.rs @@ -67,7 +67,12 @@ impl Rule for PreferDomNodeTextContent { if identifier.name == "innerText" && matches!(parent_node_kind, AstKind::PropertyKey(_)) && (matches!(grand_parent_node_kind, AstKind::ObjectPattern(_)) - || matches!(grand_parent_node_kind, AstKind::AssignmentTarget(_))) + || matches!( + grand_parent_node_kind, + AstKind::ObjectAssignmentTarget(_) + | AstKind::SimpleAssignmentTarget(_) + | AstKind::AssignmentTarget(_) + )) { ctx.diagnostic(prefer_dom_node_text_content_diagnostic(identifier.span)); return; @@ -77,8 +82,13 @@ impl Rule for PreferDomNodeTextContent { // `({innerText} = node)` if let AstKind::IdentifierReference(identifier_ref) = node.kind() { if identifier_ref.name == "innerText" - && matches!(parent_node_kind, AstKind::AssignmentTarget(_)) - && matches!(grand_parent_node_kind, AstKind::AssignmentExpression(_)) + && matches!( + parent_node_kind, + AstKind::ObjectAssignmentTarget(_) + | AstKind::AssignmentTarget(_) + | AstKind::SimpleAssignmentTarget(_) + ) + && matches!(grand_parent_node_kind, AstKind::AssignmentTargetPattern(_)) { ctx.diagnostic(prefer_dom_node_text_content_diagnostic(identifier_ref.span)); } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/array-assignment.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/array-assignment.snap index 9f5eaec38c78f..3acd1f8d60abd 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/array-assignment.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/array-assignment.snap @@ -19,7 +19,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/a "flag": "ReferenceFlag(Read)", "id": 0, "name": "obj", - "node_id": 16 + "node_id": 18 } ] }, @@ -33,7 +33,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/a "flag": "ReferenceFlag(Write)", "id": 1, "name": "b", - "node_id": 20 + "node_id": 22 } ] }, @@ -47,7 +47,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/a "flag": "ReferenceFlag(Write)", "id": 2, "name": "c", - "node_id": 24 + "node_id": 28 } ] } diff --git a/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/object-assignment.snap b/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/object-assignment.snap index 5e6324a5177b3..f8239b60eef77 100644 --- a/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/object-assignment.snap +++ b/crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/object-assignment.snap @@ -19,7 +19,7 @@ input_file: crates/oxc_semantic/tests/fixtures/typescript-eslint/destructuring/o "flag": "ReferenceFlag(Read)", "id": 2, "name": "obj", - "node_id": 23 + "node_id": 27 } ] } diff --git a/tasks/ast_codegen/src/generators/ast_kind.rs b/tasks/ast_codegen/src/generators/ast_kind.rs index 5966dc67735f0..2a014ce944b75 100644 --- a/tasks/ast_codegen/src/generators/ast_kind.rs +++ b/tasks/ast_codegen/src/generators/ast_kind.rs @@ -11,16 +11,13 @@ use super::generated_header; pub struct AstKindGenerator; -pub const BLACK_LIST: [&str; 65] = [ +pub const BLACK_LIST: [&str; 62] = [ "Expression", "ObjectPropertyKind", "TemplateElement", "ComputedMemberExpression", "StaticMemberExpression", "PrivateFieldExpression", - "AssignmentTargetPattern", - "ArrayAssignmentTarget", - "ObjectAssignmentTarget", "AssignmentTargetRest", "AssignmentTargetMaybeDefault", "AssignmentTargetProperty",