From 0e4adc15dd6413e30fec2f52618acb7fb25eb91c Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Sat, 9 Nov 2024 08:48:14 +0000 Subject: [PATCH] feat(ast)!: remove invalid expressions from `TSEnumMemberName` (#7219) --- crates/oxc_ast/src/ast/ts.rs | 8 +- crates/oxc_ast/src/ast_impl/ts.rs | 11 +- crates/oxc_ast/src/generated/ast_builder.rs | 47 ----- .../oxc_ast/src/generated/derive_clone_in.rs | 126 ------------- .../src/generated/derive_content_eq.rs | 176 ------------------ .../src/generated/derive_content_hash.rs | 44 ----- crates/oxc_ast/src/generated/derive_estree.rs | 44 ----- .../oxc_ast/src/generated/derive_get_span.rs | 44 ----- .../src/generated/derive_get_span_mut.rs | 44 ----- crates/oxc_ast/src/generated/visit.rs | 3 - crates/oxc_ast/src/generated/visit_mut.rs | 3 - crates/oxc_codegen/src/gen.rs | 9 - crates/oxc_isolated_declarations/src/enum.rs | 8 - .../typescript/no_duplicate_enum_values.rs | 2 +- crates/oxc_parser/src/ts/statement.rs | 55 ++---- crates/oxc_prettier/src/format/mod.rs | 2 - crates/oxc_semantic/src/binder.rs | 17 +- crates/oxc_semantic/src/stats.rs | 4 +- crates/oxc_transformer/src/typescript/enum.rs | 12 +- crates/oxc_traverse/src/generated/walk.rs | 50 ----- npm/oxc-types/types.d.ts | 48 +---- tasks/coverage/misc/fail/oxc-4449-1.ts | 1 + tasks/coverage/misc/fail/oxc-4449-2.ts | 1 + tasks/coverage/misc/fail/oxc-4449-3.ts | 1 + tasks/coverage/misc/fail/oxc-4449-4.ts | 1 + tasks/coverage/misc/fail/oxc-4449-5.ts | 1 + tasks/coverage/misc/fail/oxc-4449-6.ts | 1 + tasks/coverage/misc/fail/oxc-4449.ts | 7 - tasks/coverage/misc/pass/oxc-4449.ts | 17 +- tasks/coverage/snapshots/parser_misc.snap | 41 ++-- .../coverage/snapshots/parser_typescript.snap | 65 ------- tasks/coverage/snapshots/semantic_misc.snap | 28 ++- .../snapshots/prettier.ts.snap.md | 3 +- 33 files changed, 81 insertions(+), 843 deletions(-) create mode 100644 tasks/coverage/misc/fail/oxc-4449-1.ts create mode 100644 tasks/coverage/misc/fail/oxc-4449-2.ts create mode 100644 tasks/coverage/misc/fail/oxc-4449-3.ts create mode 100644 tasks/coverage/misc/fail/oxc-4449-4.ts create mode 100644 tasks/coverage/misc/fail/oxc-4449-5.ts create mode 100644 tasks/coverage/misc/fail/oxc-4449-6.ts delete mode 100644 tasks/coverage/misc/fail/oxc-4449.ts diff --git a/crates/oxc_ast/src/ast/ts.rs b/crates/oxc_ast/src/ast/ts.rs index 89364cdfd9bbf..208cb6cc24285 100644 --- a/crates/oxc_ast/src/ast/ts.rs +++ b/crates/oxc_ast/src/ast/ts.rs @@ -16,7 +16,7 @@ use oxc_estree::ESTree; use oxc_span::{cmp::ContentEq, hash::ContentHash, Atom, GetSpan, GetSpanMut, Span}; use oxc_syntax::scope::ScopeId; -use super::{inherit_variants, js::*, jsx::*, literal::*}; +use super::{inherit_variants, js::*, literal::*}; /// TypeScript `this` parameter /// @@ -115,12 +115,6 @@ inherit_variants! { pub enum TSEnumMemberName<'a> { StaticIdentifier(Box<'a, IdentifierName<'a>>) = 64, StaticStringLiteral(Box<'a, StringLiteral<'a>>) = 65, - StaticTemplateLiteral(Box<'a, TemplateLiteral<'a>>) = 66, - // Invalid Grammar `enum E { 1 }` - StaticNumericLiteral(Box<'a, NumericLiteral<'a>>) = 67, - // Invalid Grammar `enum E { [computed] }` - // `Expression` variants added here by `inherit_variants!` macro - @inherit Expression } } diff --git a/crates/oxc_ast/src/ast_impl/ts.rs b/crates/oxc_ast/src/ast_impl/ts.rs index e26e95c79c283..6a749430eb5ef 100644 --- a/crates/oxc_ast/src/ast_impl/ts.rs +++ b/crates/oxc_ast/src/ast_impl/ts.rs @@ -11,14 +11,11 @@ use oxc_span::Atom; use crate::ast::*; impl<'a> TSEnumMemberName<'a> { - /// Get the name of this enum member if it can be determined statically. - pub fn static_name(&self) -> Option<&'a str> { + /// Get the name of this enum member. + pub fn static_name(&self) -> Atom<'a> { match self { - Self::StaticIdentifier(ident) => Some(ident.name.as_str()), - Self::StaticStringLiteral(lit) => Some(lit.value.as_str()), - Self::NumericLiteral(lit) => Some(lit.raw), - Self::StaticTemplateLiteral(lit) => lit.quasi().map(Into::into), - _ => None, + Self::StaticIdentifier(ident) => ident.name.clone(), + Self::StaticStringLiteral(lit) => lit.value.clone(), } } } diff --git a/crates/oxc_ast/src/generated/ast_builder.rs b/crates/oxc_ast/src/generated/ast_builder.rs index 773ea68e86221..df455a42bb1c6 100644 --- a/crates/oxc_ast/src/generated/ast_builder.rs +++ b/crates/oxc_ast/src/generated/ast_builder.rs @@ -7783,53 +7783,6 @@ impl<'a> AstBuilder<'a> { TSEnumMemberName::StaticStringLiteral(self.alloc(self.string_literal(span, value))) } - /// Build a [`TSEnumMemberName::StaticTemplateLiteral`] - /// - /// This node contains a [`TemplateLiteral`] that will be stored in the memory arena. - /// - /// ## Parameters - /// - span: The [`Span`] covering this node - /// - quasis - /// - expressions - #[inline] - pub fn ts_enum_member_name_template_literal( - self, - span: Span, - quasis: Vec<'a, TemplateElement<'a>>, - expressions: Vec<'a, Expression<'a>>, - ) -> TSEnumMemberName<'a> { - TSEnumMemberName::StaticTemplateLiteral(self.alloc(self.template_literal( - span, - quasis, - expressions, - ))) - } - - /// Build a [`TSEnumMemberName::StaticNumericLiteral`] - /// - /// This node contains a [`NumericLiteral`] that will be stored in the memory arena. - /// - /// ## Parameters - /// - span: Node location in source code - /// - value: The value of the number, converted into base 10 - /// - raw: The number as it appears in source code - /// - base: The base representation used by the literal in source code - #[inline] - pub fn ts_enum_member_name_numeric_literal( - self, - span: Span, - value: f64, - raw: S, - base: NumberBase, - ) -> TSEnumMemberName<'a> - where - S: IntoIn<'a, &'a str>, - { - TSEnumMemberName::StaticNumericLiteral( - self.alloc(self.numeric_literal(span, value, raw, base)), - ) - } - /// Build a [`TSTypeAnnotation`]. /// /// If you want the built node to be allocated in the memory arena, use [`AstBuilder::alloc_ts_type_annotation`] instead. diff --git a/crates/oxc_ast/src/generated/derive_clone_in.rs b/crates/oxc_ast/src/generated/derive_clone_in.rs index 91082daf172d6..1f9fe794c2cb4 100644 --- a/crates/oxc_ast/src/generated/derive_clone_in.rs +++ b/crates/oxc_ast/src/generated/derive_clone_in.rs @@ -2600,132 +2600,6 @@ impl<'old_alloc, 'new_alloc> CloneIn<'new_alloc> for TSEnumMemberName<'old_alloc Self::StaticStringLiteral(it) => { TSEnumMemberName::StaticStringLiteral(CloneIn::clone_in(it, allocator)) } - Self::StaticTemplateLiteral(it) => { - TSEnumMemberName::StaticTemplateLiteral(CloneIn::clone_in(it, allocator)) - } - Self::StaticNumericLiteral(it) => { - TSEnumMemberName::StaticNumericLiteral(CloneIn::clone_in(it, allocator)) - } - Self::BooleanLiteral(it) => { - TSEnumMemberName::BooleanLiteral(CloneIn::clone_in(it, allocator)) - } - Self::NullLiteral(it) => { - TSEnumMemberName::NullLiteral(CloneIn::clone_in(it, allocator)) - } - Self::NumericLiteral(it) => { - TSEnumMemberName::NumericLiteral(CloneIn::clone_in(it, allocator)) - } - Self::BigIntLiteral(it) => { - TSEnumMemberName::BigIntLiteral(CloneIn::clone_in(it, allocator)) - } - Self::RegExpLiteral(it) => { - TSEnumMemberName::RegExpLiteral(CloneIn::clone_in(it, allocator)) - } - Self::StringLiteral(it) => { - TSEnumMemberName::StringLiteral(CloneIn::clone_in(it, allocator)) - } - Self::TemplateLiteral(it) => { - TSEnumMemberName::TemplateLiteral(CloneIn::clone_in(it, allocator)) - } - Self::Identifier(it) => TSEnumMemberName::Identifier(CloneIn::clone_in(it, allocator)), - Self::MetaProperty(it) => { - TSEnumMemberName::MetaProperty(CloneIn::clone_in(it, allocator)) - } - Self::Super(it) => TSEnumMemberName::Super(CloneIn::clone_in(it, allocator)), - Self::ArrayExpression(it) => { - TSEnumMemberName::ArrayExpression(CloneIn::clone_in(it, allocator)) - } - Self::ArrowFunctionExpression(it) => { - TSEnumMemberName::ArrowFunctionExpression(CloneIn::clone_in(it, allocator)) - } - Self::AssignmentExpression(it) => { - TSEnumMemberName::AssignmentExpression(CloneIn::clone_in(it, allocator)) - } - Self::AwaitExpression(it) => { - TSEnumMemberName::AwaitExpression(CloneIn::clone_in(it, allocator)) - } - Self::BinaryExpression(it) => { - TSEnumMemberName::BinaryExpression(CloneIn::clone_in(it, allocator)) - } - Self::CallExpression(it) => { - TSEnumMemberName::CallExpression(CloneIn::clone_in(it, allocator)) - } - Self::ChainExpression(it) => { - TSEnumMemberName::ChainExpression(CloneIn::clone_in(it, allocator)) - } - Self::ClassExpression(it) => { - TSEnumMemberName::ClassExpression(CloneIn::clone_in(it, allocator)) - } - Self::ConditionalExpression(it) => { - TSEnumMemberName::ConditionalExpression(CloneIn::clone_in(it, allocator)) - } - Self::FunctionExpression(it) => { - TSEnumMemberName::FunctionExpression(CloneIn::clone_in(it, allocator)) - } - Self::ImportExpression(it) => { - TSEnumMemberName::ImportExpression(CloneIn::clone_in(it, allocator)) - } - Self::LogicalExpression(it) => { - TSEnumMemberName::LogicalExpression(CloneIn::clone_in(it, allocator)) - } - Self::NewExpression(it) => { - TSEnumMemberName::NewExpression(CloneIn::clone_in(it, allocator)) - } - Self::ObjectExpression(it) => { - TSEnumMemberName::ObjectExpression(CloneIn::clone_in(it, allocator)) - } - Self::ParenthesizedExpression(it) => { - TSEnumMemberName::ParenthesizedExpression(CloneIn::clone_in(it, allocator)) - } - Self::SequenceExpression(it) => { - TSEnumMemberName::SequenceExpression(CloneIn::clone_in(it, allocator)) - } - Self::TaggedTemplateExpression(it) => { - TSEnumMemberName::TaggedTemplateExpression(CloneIn::clone_in(it, allocator)) - } - Self::ThisExpression(it) => { - TSEnumMemberName::ThisExpression(CloneIn::clone_in(it, allocator)) - } - Self::UnaryExpression(it) => { - TSEnumMemberName::UnaryExpression(CloneIn::clone_in(it, allocator)) - } - Self::UpdateExpression(it) => { - TSEnumMemberName::UpdateExpression(CloneIn::clone_in(it, allocator)) - } - Self::YieldExpression(it) => { - TSEnumMemberName::YieldExpression(CloneIn::clone_in(it, allocator)) - } - Self::PrivateInExpression(it) => { - TSEnumMemberName::PrivateInExpression(CloneIn::clone_in(it, allocator)) - } - Self::JSXElement(it) => TSEnumMemberName::JSXElement(CloneIn::clone_in(it, allocator)), - Self::JSXFragment(it) => { - TSEnumMemberName::JSXFragment(CloneIn::clone_in(it, allocator)) - } - Self::TSAsExpression(it) => { - TSEnumMemberName::TSAsExpression(CloneIn::clone_in(it, allocator)) - } - Self::TSSatisfiesExpression(it) => { - TSEnumMemberName::TSSatisfiesExpression(CloneIn::clone_in(it, allocator)) - } - Self::TSTypeAssertion(it) => { - TSEnumMemberName::TSTypeAssertion(CloneIn::clone_in(it, allocator)) - } - Self::TSNonNullExpression(it) => { - TSEnumMemberName::TSNonNullExpression(CloneIn::clone_in(it, allocator)) - } - Self::TSInstantiationExpression(it) => { - TSEnumMemberName::TSInstantiationExpression(CloneIn::clone_in(it, allocator)) - } - Self::ComputedMemberExpression(it) => { - TSEnumMemberName::ComputedMemberExpression(CloneIn::clone_in(it, allocator)) - } - Self::StaticMemberExpression(it) => { - TSEnumMemberName::StaticMemberExpression(CloneIn::clone_in(it, allocator)) - } - Self::PrivateFieldExpression(it) => { - TSEnumMemberName::PrivateFieldExpression(CloneIn::clone_in(it, allocator)) - } } } } diff --git a/crates/oxc_ast/src/generated/derive_content_eq.rs b/crates/oxc_ast/src/generated/derive_content_eq.rs index c05760395c05d..c3a26b5ce93af 100644 --- a/crates/oxc_ast/src/generated/derive_content_eq.rs +++ b/crates/oxc_ast/src/generated/derive_content_eq.rs @@ -2588,182 +2588,6 @@ impl<'a> ContentEq for TSEnumMemberName<'a> { Self::StaticStringLiteral(other) if ContentEq::content_eq(it, other) => true, _ => false, }, - Self::StaticTemplateLiteral(it) => match other { - Self::StaticTemplateLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::StaticNumericLiteral(it) => match other { - Self::StaticNumericLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::BooleanLiteral(it) => match other { - Self::BooleanLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::NullLiteral(it) => match other { - Self::NullLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::NumericLiteral(it) => match other { - Self::NumericLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::BigIntLiteral(it) => match other { - Self::BigIntLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::RegExpLiteral(it) => match other { - Self::RegExpLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::StringLiteral(it) => match other { - Self::StringLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TemplateLiteral(it) => match other { - Self::TemplateLiteral(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::Identifier(it) => match other { - Self::Identifier(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::MetaProperty(it) => match other { - Self::MetaProperty(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::Super(it) => match other { - Self::Super(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ArrayExpression(it) => match other { - Self::ArrayExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ArrowFunctionExpression(it) => match other { - Self::ArrowFunctionExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::AssignmentExpression(it) => match other { - Self::AssignmentExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::AwaitExpression(it) => match other { - Self::AwaitExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::BinaryExpression(it) => match other { - Self::BinaryExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::CallExpression(it) => match other { - Self::CallExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ChainExpression(it) => match other { - Self::ChainExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ClassExpression(it) => match other { - Self::ClassExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ConditionalExpression(it) => match other { - Self::ConditionalExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::FunctionExpression(it) => match other { - Self::FunctionExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ImportExpression(it) => match other { - Self::ImportExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::LogicalExpression(it) => match other { - Self::LogicalExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::NewExpression(it) => match other { - Self::NewExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ObjectExpression(it) => match other { - Self::ObjectExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ParenthesizedExpression(it) => match other { - Self::ParenthesizedExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::SequenceExpression(it) => match other { - Self::SequenceExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TaggedTemplateExpression(it) => match other { - Self::TaggedTemplateExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ThisExpression(it) => match other { - Self::ThisExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::UnaryExpression(it) => match other { - Self::UnaryExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::UpdateExpression(it) => match other { - Self::UpdateExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::YieldExpression(it) => match other { - Self::YieldExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::PrivateInExpression(it) => match other { - Self::PrivateInExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::JSXElement(it) => match other { - Self::JSXElement(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::JSXFragment(it) => match other { - Self::JSXFragment(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TSAsExpression(it) => match other { - Self::TSAsExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TSSatisfiesExpression(it) => match other { - Self::TSSatisfiesExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TSTypeAssertion(it) => match other { - Self::TSTypeAssertion(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TSNonNullExpression(it) => match other { - Self::TSNonNullExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::TSInstantiationExpression(it) => match other { - Self::TSInstantiationExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::ComputedMemberExpression(it) => match other { - Self::ComputedMemberExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::StaticMemberExpression(it) => match other { - Self::StaticMemberExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, - Self::PrivateFieldExpression(it) => match other { - Self::PrivateFieldExpression(other) if ContentEq::content_eq(it, other) => true, - _ => false, - }, } } } diff --git a/crates/oxc_ast/src/generated/derive_content_hash.rs b/crates/oxc_ast/src/generated/derive_content_hash.rs index 75f00965d3819..e2245d429aa73 100644 --- a/crates/oxc_ast/src/generated/derive_content_hash.rs +++ b/crates/oxc_ast/src/generated/derive_content_hash.rs @@ -1419,50 +1419,6 @@ impl<'a> ContentHash for TSEnumMemberName<'a> { match self { Self::StaticIdentifier(it) => ContentHash::content_hash(it, state), Self::StaticStringLiteral(it) => ContentHash::content_hash(it, state), - Self::StaticTemplateLiteral(it) => ContentHash::content_hash(it, state), - Self::StaticNumericLiteral(it) => ContentHash::content_hash(it, state), - Self::BooleanLiteral(it) => ContentHash::content_hash(it, state), - Self::NullLiteral(it) => ContentHash::content_hash(it, state), - Self::NumericLiteral(it) => ContentHash::content_hash(it, state), - Self::BigIntLiteral(it) => ContentHash::content_hash(it, state), - Self::RegExpLiteral(it) => ContentHash::content_hash(it, state), - Self::StringLiteral(it) => ContentHash::content_hash(it, state), - Self::TemplateLiteral(it) => ContentHash::content_hash(it, state), - Self::Identifier(it) => ContentHash::content_hash(it, state), - Self::MetaProperty(it) => ContentHash::content_hash(it, state), - Self::Super(it) => ContentHash::content_hash(it, state), - Self::ArrayExpression(it) => ContentHash::content_hash(it, state), - Self::ArrowFunctionExpression(it) => ContentHash::content_hash(it, state), - Self::AssignmentExpression(it) => ContentHash::content_hash(it, state), - Self::AwaitExpression(it) => ContentHash::content_hash(it, state), - Self::BinaryExpression(it) => ContentHash::content_hash(it, state), - Self::CallExpression(it) => ContentHash::content_hash(it, state), - Self::ChainExpression(it) => ContentHash::content_hash(it, state), - Self::ClassExpression(it) => ContentHash::content_hash(it, state), - Self::ConditionalExpression(it) => ContentHash::content_hash(it, state), - Self::FunctionExpression(it) => ContentHash::content_hash(it, state), - Self::ImportExpression(it) => ContentHash::content_hash(it, state), - Self::LogicalExpression(it) => ContentHash::content_hash(it, state), - Self::NewExpression(it) => ContentHash::content_hash(it, state), - Self::ObjectExpression(it) => ContentHash::content_hash(it, state), - Self::ParenthesizedExpression(it) => ContentHash::content_hash(it, state), - Self::SequenceExpression(it) => ContentHash::content_hash(it, state), - Self::TaggedTemplateExpression(it) => ContentHash::content_hash(it, state), - Self::ThisExpression(it) => ContentHash::content_hash(it, state), - Self::UnaryExpression(it) => ContentHash::content_hash(it, state), - Self::UpdateExpression(it) => ContentHash::content_hash(it, state), - Self::YieldExpression(it) => ContentHash::content_hash(it, state), - Self::PrivateInExpression(it) => ContentHash::content_hash(it, state), - Self::JSXElement(it) => ContentHash::content_hash(it, state), - Self::JSXFragment(it) => ContentHash::content_hash(it, state), - Self::TSAsExpression(it) => ContentHash::content_hash(it, state), - Self::TSSatisfiesExpression(it) => ContentHash::content_hash(it, state), - Self::TSTypeAssertion(it) => ContentHash::content_hash(it, state), - Self::TSNonNullExpression(it) => ContentHash::content_hash(it, state), - Self::TSInstantiationExpression(it) => ContentHash::content_hash(it, state), - Self::ComputedMemberExpression(it) => ContentHash::content_hash(it, state), - Self::StaticMemberExpression(it) => ContentHash::content_hash(it, state), - Self::PrivateFieldExpression(it) => ContentHash::content_hash(it, state), } } } diff --git a/crates/oxc_ast/src/generated/derive_estree.rs b/crates/oxc_ast/src/generated/derive_estree.rs index 6c43b3f7347d3..4bdedc6d61d97 100644 --- a/crates/oxc_ast/src/generated/derive_estree.rs +++ b/crates/oxc_ast/src/generated/derive_estree.rs @@ -1978,50 +1978,6 @@ impl<'a> Serialize for TSEnumMemberName<'a> { match self { TSEnumMemberName::StaticIdentifier(x) => Serialize::serialize(x, serializer), TSEnumMemberName::StaticStringLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::StaticTemplateLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::StaticNumericLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::BooleanLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::NullLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::NumericLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::BigIntLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::RegExpLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::StringLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TemplateLiteral(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::Identifier(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::MetaProperty(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::Super(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ArrayExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ArrowFunctionExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::AssignmentExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::AwaitExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::BinaryExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::CallExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ChainExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ClassExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ConditionalExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::FunctionExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ImportExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::LogicalExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::NewExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ObjectExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ParenthesizedExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::SequenceExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TaggedTemplateExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ThisExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::UnaryExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::UpdateExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::YieldExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::PrivateInExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::JSXElement(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::JSXFragment(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TSAsExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TSSatisfiesExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TSTypeAssertion(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TSNonNullExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::TSInstantiationExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::ComputedMemberExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::StaticMemberExpression(x) => Serialize::serialize(x, serializer), - TSEnumMemberName::PrivateFieldExpression(x) => Serialize::serialize(x, serializer), } } } diff --git a/crates/oxc_ast/src/generated/derive_get_span.rs b/crates/oxc_ast/src/generated/derive_get_span.rs index 9f2de7d1294db..eeb137c6ed617 100644 --- a/crates/oxc_ast/src/generated/derive_get_span.rs +++ b/crates/oxc_ast/src/generated/derive_get_span.rs @@ -1274,50 +1274,6 @@ impl<'a> GetSpan for TSEnumMemberName<'a> { match self { Self::StaticIdentifier(it) => GetSpan::span(it.as_ref()), Self::StaticStringLiteral(it) => GetSpan::span(it.as_ref()), - Self::StaticTemplateLiteral(it) => GetSpan::span(it.as_ref()), - Self::StaticNumericLiteral(it) => GetSpan::span(it.as_ref()), - Self::BooleanLiteral(it) => GetSpan::span(it.as_ref()), - Self::NullLiteral(it) => GetSpan::span(it.as_ref()), - Self::NumericLiteral(it) => GetSpan::span(it.as_ref()), - Self::BigIntLiteral(it) => GetSpan::span(it.as_ref()), - Self::RegExpLiteral(it) => GetSpan::span(it.as_ref()), - Self::StringLiteral(it) => GetSpan::span(it.as_ref()), - Self::TemplateLiteral(it) => GetSpan::span(it.as_ref()), - Self::Identifier(it) => GetSpan::span(it.as_ref()), - Self::MetaProperty(it) => GetSpan::span(it.as_ref()), - Self::Super(it) => GetSpan::span(it.as_ref()), - Self::ArrayExpression(it) => GetSpan::span(it.as_ref()), - Self::ArrowFunctionExpression(it) => GetSpan::span(it.as_ref()), - Self::AssignmentExpression(it) => GetSpan::span(it.as_ref()), - Self::AwaitExpression(it) => GetSpan::span(it.as_ref()), - Self::BinaryExpression(it) => GetSpan::span(it.as_ref()), - Self::CallExpression(it) => GetSpan::span(it.as_ref()), - Self::ChainExpression(it) => GetSpan::span(it.as_ref()), - Self::ClassExpression(it) => GetSpan::span(it.as_ref()), - Self::ConditionalExpression(it) => GetSpan::span(it.as_ref()), - Self::FunctionExpression(it) => GetSpan::span(it.as_ref()), - Self::ImportExpression(it) => GetSpan::span(it.as_ref()), - Self::LogicalExpression(it) => GetSpan::span(it.as_ref()), - Self::NewExpression(it) => GetSpan::span(it.as_ref()), - Self::ObjectExpression(it) => GetSpan::span(it.as_ref()), - Self::ParenthesizedExpression(it) => GetSpan::span(it.as_ref()), - Self::SequenceExpression(it) => GetSpan::span(it.as_ref()), - Self::TaggedTemplateExpression(it) => GetSpan::span(it.as_ref()), - Self::ThisExpression(it) => GetSpan::span(it.as_ref()), - Self::UnaryExpression(it) => GetSpan::span(it.as_ref()), - Self::UpdateExpression(it) => GetSpan::span(it.as_ref()), - Self::YieldExpression(it) => GetSpan::span(it.as_ref()), - Self::PrivateInExpression(it) => GetSpan::span(it.as_ref()), - Self::JSXElement(it) => GetSpan::span(it.as_ref()), - Self::JSXFragment(it) => GetSpan::span(it.as_ref()), - Self::TSAsExpression(it) => GetSpan::span(it.as_ref()), - Self::TSSatisfiesExpression(it) => GetSpan::span(it.as_ref()), - Self::TSTypeAssertion(it) => GetSpan::span(it.as_ref()), - Self::TSNonNullExpression(it) => GetSpan::span(it.as_ref()), - Self::TSInstantiationExpression(it) => GetSpan::span(it.as_ref()), - Self::ComputedMemberExpression(it) => GetSpan::span(it.as_ref()), - Self::StaticMemberExpression(it) => GetSpan::span(it.as_ref()), - Self::PrivateFieldExpression(it) => GetSpan::span(it.as_ref()), } } } diff --git a/crates/oxc_ast/src/generated/derive_get_span_mut.rs b/crates/oxc_ast/src/generated/derive_get_span_mut.rs index 38b0041f11ede..20d674124258a 100644 --- a/crates/oxc_ast/src/generated/derive_get_span_mut.rs +++ b/crates/oxc_ast/src/generated/derive_get_span_mut.rs @@ -1274,50 +1274,6 @@ impl<'a> GetSpanMut for TSEnumMemberName<'a> { match self { Self::StaticIdentifier(it) => GetSpanMut::span_mut(&mut **it), Self::StaticStringLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::StaticTemplateLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::StaticNumericLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::BooleanLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::NullLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::NumericLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::BigIntLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::RegExpLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::StringLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::TemplateLiteral(it) => GetSpanMut::span_mut(&mut **it), - Self::Identifier(it) => GetSpanMut::span_mut(&mut **it), - Self::MetaProperty(it) => GetSpanMut::span_mut(&mut **it), - Self::Super(it) => GetSpanMut::span_mut(&mut **it), - Self::ArrayExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ArrowFunctionExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::AssignmentExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::AwaitExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::BinaryExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::CallExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ChainExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ClassExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ConditionalExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::FunctionExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ImportExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::LogicalExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::NewExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ObjectExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ParenthesizedExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::SequenceExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::TaggedTemplateExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ThisExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::UnaryExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::UpdateExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::YieldExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::PrivateInExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::JSXElement(it) => GetSpanMut::span_mut(&mut **it), - Self::JSXFragment(it) => GetSpanMut::span_mut(&mut **it), - Self::TSAsExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::TSSatisfiesExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::TSTypeAssertion(it) => GetSpanMut::span_mut(&mut **it), - Self::TSNonNullExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::TSInstantiationExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::ComputedMemberExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::StaticMemberExpression(it) => GetSpanMut::span_mut(&mut **it), - Self::PrivateFieldExpression(it) => GetSpanMut::span_mut(&mut **it), } } } diff --git a/crates/oxc_ast/src/generated/visit.rs b/crates/oxc_ast/src/generated/visit.rs index 91e923be85be8..8fc63d5780d9e 100644 --- a/crates/oxc_ast/src/generated/visit.rs +++ b/crates/oxc_ast/src/generated/visit.rs @@ -3811,9 +3811,6 @@ pub mod walk { match it { TSEnumMemberName::StaticIdentifier(it) => visitor.visit_identifier_name(it), TSEnumMemberName::StaticStringLiteral(it) => visitor.visit_string_literal(it), - TSEnumMemberName::StaticTemplateLiteral(it) => visitor.visit_template_literal(it), - TSEnumMemberName::StaticNumericLiteral(it) => visitor.visit_numeric_literal(it), - match_expression!(TSEnumMemberName) => visitor.visit_expression(it.to_expression()), } } diff --git a/crates/oxc_ast/src/generated/visit_mut.rs b/crates/oxc_ast/src/generated/visit_mut.rs index c25b16321c3ef..182e7bb82e5b5 100644 --- a/crates/oxc_ast/src/generated/visit_mut.rs +++ b/crates/oxc_ast/src/generated/visit_mut.rs @@ -4029,9 +4029,6 @@ pub mod walk_mut { match it { TSEnumMemberName::StaticIdentifier(it) => visitor.visit_identifier_name(it), TSEnumMemberName::StaticStringLiteral(it) => visitor.visit_string_literal(it), - TSEnumMemberName::StaticTemplateLiteral(it) => visitor.visit_template_literal(it), - TSEnumMemberName::StaticNumericLiteral(it) => visitor.visit_numeric_literal(it), - match_expression!(TSEnumMemberName) => visitor.visit_expression(it.to_expression_mut()), } } diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 3d3bc68046a1d..7662a053639ef 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -3592,15 +3592,6 @@ impl<'a> Gen for TSEnumMember<'a> { match &self.id { TSEnumMemberName::StaticIdentifier(decl) => decl.print(p, ctx), TSEnumMemberName::StaticStringLiteral(decl) => decl.print(p, ctx), - TSEnumMemberName::StaticTemplateLiteral(decl) => decl.print(p, ctx), - TSEnumMemberName::StaticNumericLiteral(decl) => { - decl.print_expr(p, Precedence::Lowest, ctx); - } - decl @ match_expression!(TSEnumMemberName) => { - p.print_str("["); - decl.to_expression().print_expr(p, Precedence::Lowest, ctx); - p.print_str("]"); - } } if let Some(init) = &self.initializer { p.print_soft_space(); diff --git a/crates/oxc_isolated_declarations/src/enum.rs b/crates/oxc_isolated_declarations/src/enum.rs index fee3d84c910a9..15b48dc920986 100644 --- a/crates/oxc_isolated_declarations/src/enum.rs +++ b/crates/oxc_isolated_declarations/src/enum.rs @@ -48,14 +48,6 @@ impl<'a> IsolatedDeclarations<'a> { let member_name = match &member.id { TSEnumMemberName::StaticIdentifier(id) => &id.name, TSEnumMemberName::StaticStringLiteral(str) => &str.value, - TSEnumMemberName::StaticTemplateLiteral(template) => { - &template.quasi().expect("Template enum members cannot have substitutions.") - } - #[allow(clippy::unnested_or_patterns)] // Clippy is wrong - TSEnumMemberName::StaticNumericLiteral(_) - | match_expression!(TSEnumMemberName) => { - unreachable!() - } }; prev_members.insert(member_name.clone(), value.clone()); } diff --git a/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs b/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs index 1056bf7511b77..75a41f4e5d211 100644 --- a/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs +++ b/crates/oxc_linter/src/rules/typescript/no_duplicate_enum_values.rs @@ -18,7 +18,7 @@ fn no_duplicate_enum_values_diagnostic( second_member: &TSEnumMember, value: &str, ) -> OxcDiagnostic { - let second_name = second_member.id.static_name().unwrap_or("the second member"); + let second_name = second_member.id.static_name(); // Unwrap will never panic since violations are only reported for members // with initializers. let second_init_span = second_member.initializer.as_ref().map(GetSpan::span).unwrap(); diff --git a/crates/oxc_parser/src/ts/statement.rs b/crates/oxc_parser/src/ts/statement.rs index 4974dff1a9158..e6d2b508f459a 100644 --- a/crates/oxc_parser/src/ts/statement.rs +++ b/crates/oxc_parser/src/ts/statement.rs @@ -52,45 +52,40 @@ impl<'a> ParserImpl<'a> { pub(crate) fn parse_ts_enum_member(&mut self) -> Result> { let span = self.start_span(); let id = self.parse_ts_enum_member_name()?; - let initializer = if self.eat(Kind::Eq) { Some(self.parse_assignment_expression_or_higher()?) } else { None }; - - let span = self.end_span(span); - if initializer.is_some() && matches!(id, TSEnumMemberName::StaticTemplateLiteral(_)) { - self.error(diagnostics::invalid_assignment(span)); - } - - Ok(self.ast.ts_enum_member(span, id, initializer)) + Ok(self.ast.ts_enum_member(self.end_span(span), id, initializer)) } fn parse_ts_enum_member_name(&mut self) -> Result> { match self.cur_kind() { - Kind::LBrack => { - let node = self.parse_computed_property_name()?; - self.check_invalid_ts_enum_computed_property(&node); - Ok(TSEnumMemberName::from(node)) - } Kind::Str => { let literal = self.parse_literal_string()?; Ok(TSEnumMemberName::StaticStringLiteral(self.alloc(literal))) } - Kind::NoSubstitutionTemplate | Kind::TemplateHead => { - let literal = self.parse_template_literal(false)?; - if !literal.expressions.is_empty() { - self.error(diagnostics::computed_property_names_not_allowed_in_enums( - literal.span(), - )); + Kind::LBrack => match self.parse_computed_property_name()? { + Expression::StringLiteral(literal) => { + Ok(TSEnumMemberName::StaticStringLiteral(literal)) } - Ok(TSEnumMemberName::StaticTemplateLiteral(self.alloc(literal))) - } + Expression::TemplateLiteral(template) if template.is_no_substitution_template() => { + Ok(self.ast.ts_enum_member_name_string_literal( + template.span, + template.quasi().unwrap(), + )) + } + Expression::NumericLiteral(literal) => { + Err(diagnostics::enum_member_cannot_have_numeric_name(literal.span())) + } + expr => Err(diagnostics::computed_property_names_not_allowed_in_enums(expr.span())), + }, + Kind::NoSubstitutionTemplate | Kind::TemplateHead => Err( + diagnostics::computed_property_names_not_allowed_in_enums(self.cur_token().span()), + ), kind if kind.is_number() => { - let literal = self.parse_literal_number()?; - self.error(diagnostics::enum_member_cannot_have_numeric_name(literal.span())); - Ok(TSEnumMemberName::StaticNumericLiteral(self.alloc(literal))) + Err(diagnostics::enum_member_cannot_have_numeric_name(self.cur_token().span())) } _ => { let ident_name = self.parse_identifier_name()?; @@ -99,18 +94,6 @@ impl<'a> ParserImpl<'a> { } } - fn check_invalid_ts_enum_computed_property(&mut self, property: &Expression<'a>) { - match property { - Expression::StringLiteral(_) => {} - Expression::TemplateLiteral(template) if template.expressions.is_empty() => {} - Expression::NumericLiteral(_) => { - self.error(diagnostics::enum_member_cannot_have_numeric_name(property.span())); - } - _ => self - .error(diagnostics::computed_property_names_not_allowed_in_enums(property.span())), - } - } - /** ------------------- Annotation ----------------- */ pub(crate) fn parse_ts_type_annotation( diff --git a/crates/oxc_prettier/src/format/mod.rs b/crates/oxc_prettier/src/format/mod.rs index b50e8360f2209..b7ff5bb84c58e 100644 --- a/crates/oxc_prettier/src/format/mod.rs +++ b/crates/oxc_prettier/src/format/mod.rs @@ -1258,8 +1258,6 @@ impl<'a> Format<'a> for TSEnumMemberName<'a> { match self { TSEnumMemberName::StaticIdentifier(identifier) => identifier.format(p), TSEnumMemberName::StaticStringLiteral(string_literal) => string_literal.format(p), - TSEnumMemberName::StaticTemplateLiteral(template_literal) => template_literal.format(p), - name => array!(p, ss!("["), name.as_expression().unwrap().format(p), ss!("]")), } } } diff --git a/crates/oxc_semantic/src/binder.rs b/crates/oxc_semantic/src/binder.rs index 750e3442688e7..e8031622976ae 100644 --- a/crates/oxc_semantic/src/binder.rs +++ b/crates/oxc_semantic/src/binder.rs @@ -1,6 +1,6 @@ //! Declare symbol for `BindingIdentifier`s -use std::{borrow::Cow, ptr}; +use std::ptr; use oxc_ast::{ast::*, AstKind}; use oxc_ecmascript::{BoundNames, IsSimpleParameterList}; @@ -383,22 +383,9 @@ impl<'a> Binder<'a> for TSEnumDeclaration<'a> { impl<'a> Binder<'a> for TSEnumMember<'a> { fn bind(&self, builder: &mut SemanticBuilder) { - // TODO: Perf - if self.id.is_expression() { - return; - } - let name = match &self.id { - TSEnumMemberName::StaticIdentifier(id) => Cow::Borrowed(id.name.as_str()), - TSEnumMemberName::StaticStringLiteral(s) => Cow::Borrowed(s.value.as_str()), - TSEnumMemberName::StaticTemplateLiteral(s) => Cow::Borrowed( - s.quasi().expect("Template enum members must have no substitutions.").as_str(), - ), - TSEnumMemberName::StaticNumericLiteral(n) => Cow::Owned(n.value.to_string()), - match_expression!(TSEnumMemberName) => panic!("TODO: implement"), - }; builder.declare_symbol( self.span, - &name, + self.id.static_name().as_str(), SymbolFlags::EnumMember, SymbolFlags::EnumMemberExcludes, ); diff --git a/crates/oxc_semantic/src/stats.rs b/crates/oxc_semantic/src/stats.rs index 056a081d8a499..ac388e1b156db 100644 --- a/crates/oxc_semantic/src/stats.rs +++ b/crates/oxc_semantic/src/stats.rs @@ -162,9 +162,7 @@ impl<'a> Visit<'a> for Counter { #[inline] fn visit_ts_enum_member_name(&mut self, it: &TSEnumMemberName<'a>) { - if !it.is_expression() { - self.stats.symbols += 1; - } + self.stats.symbols += 1; walk_ts_enum_member_name(self, it); } diff --git a/crates/oxc_transformer/src/typescript/enum.rs b/crates/oxc_transformer/src/typescript/enum.rs index c6325c1b0cd7c..06827c8078310 100644 --- a/crates/oxc_transformer/src/typescript/enum.rs +++ b/crates/oxc_transformer/src/typescript/enum.rs @@ -194,17 +194,7 @@ impl<'a> TypeScriptEnum<'a> { for member in members.iter_mut() { let member_name: &Atom<'_> = match &member.id { TSEnumMemberName::StaticIdentifier(id) => &id.name, - TSEnumMemberName::StaticStringLiteral(str) - | TSEnumMemberName::StringLiteral(str) => &str.value, - TSEnumMemberName::StaticTemplateLiteral(template) - | TSEnumMemberName::TemplateLiteral(template) => { - &template.quasi().expect("Template enum members cannot have substitutions.") - } - // parse error, but better than a panic - TSEnumMemberName::StaticNumericLiteral(n) => &Atom::from(n.raw), - match_expression!(TSEnumMemberName) => { - unreachable!() - } + TSEnumMemberName::StaticStringLiteral(str) => &str.value, }; let init = if let Some(initializer) = &mut member.initializer { diff --git a/crates/oxc_traverse/src/generated/walk.rs b/crates/oxc_traverse/src/generated/walk.rs index 0e849819c0c6c..4546b8b3a56f1 100644 --- a/crates/oxc_traverse/src/generated/walk.rs +++ b/crates/oxc_traverse/src/generated/walk.rs @@ -3727,56 +3727,6 @@ pub(crate) unsafe fn walk_ts_enum_member_name<'a, Tr: Traverse<'a>>( TSEnumMemberName::StaticStringLiteral(node) => { walk_string_literal(traverser, (&mut **node) as *mut _, ctx) } - TSEnumMemberName::StaticTemplateLiteral(node) => { - walk_template_literal(traverser, (&mut **node) as *mut _, ctx) - } - TSEnumMemberName::StaticNumericLiteral(node) => { - walk_numeric_literal(traverser, (&mut **node) as *mut _, ctx) - } - TSEnumMemberName::BooleanLiteral(_) - | TSEnumMemberName::NullLiteral(_) - | TSEnumMemberName::NumericLiteral(_) - | TSEnumMemberName::BigIntLiteral(_) - | TSEnumMemberName::RegExpLiteral(_) - | TSEnumMemberName::StringLiteral(_) - | TSEnumMemberName::TemplateLiteral(_) - | TSEnumMemberName::Identifier(_) - | TSEnumMemberName::MetaProperty(_) - | TSEnumMemberName::Super(_) - | TSEnumMemberName::ArrayExpression(_) - | TSEnumMemberName::ArrowFunctionExpression(_) - | TSEnumMemberName::AssignmentExpression(_) - | TSEnumMemberName::AwaitExpression(_) - | TSEnumMemberName::BinaryExpression(_) - | TSEnumMemberName::CallExpression(_) - | TSEnumMemberName::ChainExpression(_) - | TSEnumMemberName::ClassExpression(_) - | TSEnumMemberName::ConditionalExpression(_) - | TSEnumMemberName::FunctionExpression(_) - | TSEnumMemberName::ImportExpression(_) - | TSEnumMemberName::LogicalExpression(_) - | TSEnumMemberName::NewExpression(_) - | TSEnumMemberName::ObjectExpression(_) - | TSEnumMemberName::ParenthesizedExpression(_) - | TSEnumMemberName::SequenceExpression(_) - | TSEnumMemberName::TaggedTemplateExpression(_) - | TSEnumMemberName::ThisExpression(_) - | TSEnumMemberName::UnaryExpression(_) - | TSEnumMemberName::UpdateExpression(_) - | TSEnumMemberName::YieldExpression(_) - | TSEnumMemberName::PrivateInExpression(_) - | TSEnumMemberName::JSXElement(_) - | TSEnumMemberName::JSXFragment(_) - | TSEnumMemberName::TSAsExpression(_) - | TSEnumMemberName::TSSatisfiesExpression(_) - | TSEnumMemberName::TSTypeAssertion(_) - | TSEnumMemberName::TSNonNullExpression(_) - | TSEnumMemberName::TSInstantiationExpression(_) - | TSEnumMemberName::ComputedMemberExpression(_) - | TSEnumMemberName::StaticMemberExpression(_) - | TSEnumMemberName::PrivateFieldExpression(_) => { - walk_expression(traverser, node as *mut _, ctx) - } } traverser.exit_ts_enum_member_name(&mut *node, ctx); } diff --git a/npm/oxc-types/types.d.ts b/npm/oxc-types/types.d.ts index b861c67d3ab3a..6f225ac7b53bb 100644 --- a/npm/oxc-types/types.d.ts +++ b/npm/oxc-types/types.d.ts @@ -1041,53 +1041,7 @@ export interface TSEnumMember extends Span { initializer: Expression | null; } -export type TSEnumMemberName = - | IdentifierName - | StringLiteral - | TemplateLiteral - | NumericLiteral - | BooleanLiteral - | NullLiteral - | NumericLiteral - | BigIntLiteral - | RegExpLiteral - | StringLiteral - | TemplateLiteral - | IdentifierReference - | MetaProperty - | Super - | ArrayExpression - | ArrowFunctionExpression - | AssignmentExpression - | AwaitExpression - | BinaryExpression - | CallExpression - | ChainExpression - | Class - | ConditionalExpression - | Function - | ImportExpression - | LogicalExpression - | NewExpression - | ObjectExpression - | ParenthesizedExpression - | SequenceExpression - | TaggedTemplateExpression - | ThisExpression - | UnaryExpression - | UpdateExpression - | YieldExpression - | PrivateInExpression - | JSXElement - | JSXFragment - | TSAsExpression - | TSSatisfiesExpression - | TSTypeAssertion - | TSNonNullExpression - | TSInstantiationExpression - | ComputedMemberExpression - | StaticMemberExpression - | PrivateFieldExpression; +export type TSEnumMemberName = IdentifierName | StringLiteral; export interface TSTypeAnnotation extends Span { type: 'TSTypeAnnotation'; diff --git a/tasks/coverage/misc/fail/oxc-4449-1.ts b/tasks/coverage/misc/fail/oxc-4449-1.ts new file mode 100644 index 0000000000000..b69532a853dfa --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-4449-1.ts @@ -0,0 +1 @@ +enum A { [foo] } // Computed property names are not allowed in enums diff --git a/tasks/coverage/misc/fail/oxc-4449-2.ts b/tasks/coverage/misc/fail/oxc-4449-2.ts new file mode 100644 index 0000000000000..dee352062d8c8 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-4449-2.ts @@ -0,0 +1 @@ +enum B { [1] } // An enum member cannot have a numeric name. diff --git a/tasks/coverage/misc/fail/oxc-4449-3.ts b/tasks/coverage/misc/fail/oxc-4449-3.ts new file mode 100644 index 0000000000000..1c34db10e1711 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-4449-3.ts @@ -0,0 +1 @@ +enum C { 1 } // An enum member cannot have a numeric name. diff --git a/tasks/coverage/misc/fail/oxc-4449-4.ts b/tasks/coverage/misc/fail/oxc-4449-4.ts new file mode 100644 index 0000000000000..010fac0eb1d62 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-4449-4.ts @@ -0,0 +1 @@ +enum D { [`test${foo}`] } // Computed property names are not allowed in enums. diff --git a/tasks/coverage/misc/fail/oxc-4449-5.ts b/tasks/coverage/misc/fail/oxc-4449-5.ts new file mode 100644 index 0000000000000..fa0d6f7d554c3 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-4449-5.ts @@ -0,0 +1 @@ +enum E { `baz` = 2 } // Enum member expected. diff --git a/tasks/coverage/misc/fail/oxc-4449-6.ts b/tasks/coverage/misc/fail/oxc-4449-6.ts new file mode 100644 index 0000000000000..6f93ae1cd55a4 --- /dev/null +++ b/tasks/coverage/misc/fail/oxc-4449-6.ts @@ -0,0 +1 @@ +enum F { ['baz' + 'baz'] // Computed property names are not allowed in enums. diff --git a/tasks/coverage/misc/fail/oxc-4449.ts b/tasks/coverage/misc/fail/oxc-4449.ts deleted file mode 100644 index 0c1da7204fb13..0000000000000 --- a/tasks/coverage/misc/fail/oxc-4449.ts +++ /dev/null @@ -1,7 +0,0 @@ -// should fail -enum A { [foo] } // Computed property names are not allowed in enums -enum B { [1] } // An enum member cannot have a numeric name. -enum C { 1 } // An enum member cannot have a numeric name. -enum D { [`test${foo}`] } // Computed property names are not allowed in enums. -enum E { `baz` = 2 } // Enum member expected. -enum F { ['baz' + 'baz'] } // Computed property names are not allowed in enums. diff --git a/tasks/coverage/misc/pass/oxc-4449.ts b/tasks/coverage/misc/pass/oxc-4449.ts index c7fd5ff39ad1e..1e4b305eff9eb 100644 --- a/tasks/coverage/misc/pass/oxc-4449.ts +++ b/tasks/coverage/misc/pass/oxc-4449.ts @@ -1,9 +1,8 @@ -// should work -enum A { ['baz'] } // โŒ currently fails -enum B { [`baz`] } // โŒ currently fails -enum C { ['baz'] = 2 } // โŒ currently fails -enum D { [`baz`] = 2 } // โŒ currently fails -enum E { 'baz' } // ๐Ÿ‘ work fine -enum F { baz } // ๐Ÿ‘ work fine -enum G { 'baz' = 2 } // ๐Ÿ‘ work fine -enum H { baz = 2 } // ๐Ÿ‘ work fine +enum A { ['baz'] } +enum B { [`baz`] } +enum C { ['baz'] = 2 } +enum D { [`baz`] = 2 } +enum E { 'baz' } +enum F { baz } +enum G { 'baz' = 2 } +enum H { baz = 2 } diff --git a/tasks/coverage/snapshots/parser_misc.snap b/tasks/coverage/snapshots/parser_misc.snap index 5ac2dc81441f2..a6e8189eebb46 100644 --- a/tasks/coverage/snapshots/parser_misc.snap +++ b/tasks/coverage/snapshots/parser_misc.snap @@ -1,7 +1,7 @@ parser_misc Summary: AST Parsed : 30/30 (100.00%) Positive Passed: 30/30 (100.00%) -Negative Passed: 20/20 (100.00%) +Negative Passed: 25/25 (100.00%) ร— Unexpected token โ•ญโ”€[misc/fail/oxc-169.js:2:1] @@ -180,49 +180,38 @@ Negative Passed: 20/20 (100.00%) help: Try insert a semicolon here ร— TS(1164): Computed property names are not allowed in enums. - โ•ญโ”€[misc/fail/oxc-4449.ts:2:11] - 1 โ”‚ // should fail - 2 โ”‚ enum A { [foo] } // Computed property names are not allowed in enums + โ•ญโ”€[misc/fail/oxc-4449-1.ts:1:11] + 1 โ”‚ enum A { [foo] } // Computed property names are not allowed in enums ยท โ”€โ”€โ”€ - 3 โ”‚ enum B { [1] } // An enum member cannot have a numeric name. โ•ฐโ”€โ”€โ”€โ”€ ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[misc/fail/oxc-4449.ts:3:11] - 2 โ”‚ enum A { [foo] } // Computed property names are not allowed in enums - 3 โ”‚ enum B { [1] } // An enum member cannot have a numeric name. + โ•ญโ”€[misc/fail/oxc-4449-2.ts:1:11] + 1 โ”‚ enum B { [1] } // An enum member cannot have a numeric name. ยท โ”€ - 4 โ”‚ enum C { 1 } // An enum member cannot have a numeric name. โ•ฐโ”€โ”€โ”€โ”€ ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[misc/fail/oxc-4449.ts:4:10] - 3 โ”‚ enum B { [1] } // An enum member cannot have a numeric name. - 4 โ”‚ enum C { 1 } // An enum member cannot have a numeric name. + โ•ญโ”€[misc/fail/oxc-4449-3.ts:1:10] + 1 โ”‚ enum C { 1 } // An enum member cannot have a numeric name. ยท โ”€ - 5 โ”‚ enum D { [`test${foo}`] } // Computed property names are not allowed in enums. โ•ฐโ”€โ”€โ”€โ”€ ร— TS(1164): Computed property names are not allowed in enums. - โ•ญโ”€[misc/fail/oxc-4449.ts:5:11] - 4 โ”‚ enum C { 1 } // An enum member cannot have a numeric name. - 5 โ”‚ enum D { [`test${foo}`] } // Computed property names are not allowed in enums. + โ•ญโ”€[misc/fail/oxc-4449-4.ts:1:11] + 1 โ”‚ enum D { [`test${foo}`] } // Computed property names are not allowed in enums. ยท โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ - 6 โ”‚ enum E { `baz` = 2 } // Enum member expected. โ•ฐโ”€โ”€โ”€โ”€ - ร— Cannot assign to this expression - โ•ญโ”€[misc/fail/oxc-4449.ts:6:10] - 5 โ”‚ enum D { [`test${foo}`] } // Computed property names are not allowed in enums. - 6 โ”‚ enum E { `baz` = 2 } // Enum member expected. - ยท โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ - 7 โ”‚ enum F { ['baz' + 'baz'] } // Computed property names are not allowed in enums. + ร— TS(1164): Computed property names are not allowed in enums. + โ•ญโ”€[misc/fail/oxc-4449-5.ts:1:10] + 1 โ”‚ enum E { `baz` = 2 } // Enum member expected. + ยท โ”€โ”€โ”€โ”€โ”€ โ•ฐโ”€โ”€โ”€โ”€ ร— TS(1164): Computed property names are not allowed in enums. - โ•ญโ”€[misc/fail/oxc-4449.ts:7:11] - 6 โ”‚ enum E { `baz` = 2 } // Enum member expected. - 7 โ”‚ enum F { ['baz' + 'baz'] } // Computed property names are not allowed in enums. + โ•ญโ”€[misc/fail/oxc-4449-6.ts:1:11] + 1 โ”‚ enum F { ['baz' + 'baz'] // Computed property names are not allowed in enums. ยท โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ•ฐโ”€โ”€โ”€โ”€ diff --git a/tasks/coverage/snapshots/parser_typescript.snap b/tasks/coverage/snapshots/parser_typescript.snap index 10066354d61d5..bc7cf1b339507 100644 --- a/tasks/coverage/snapshots/parser_typescript.snap +++ b/tasks/coverage/snapshots/parser_typescript.snap @@ -7450,30 +7450,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private 3 โ”‚ 11e-1, โ•ฐโ”€โ”€โ”€โ”€ - ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[typescript/tests/cases/compiler/enumIdentifierLiterals.ts:3:5] - 2 โ”‚ 1.0, - 3 โ”‚ 11e-1, - ยท โ”€โ”€โ”€โ”€โ”€ - 4 โ”‚ 0.12e1, - โ•ฐโ”€โ”€โ”€โ”€ - - ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[typescript/tests/cases/compiler/enumIdentifierLiterals.ts:4:5] - 3 โ”‚ 11e-1, - 4 โ”‚ 0.12e1, - ยท โ”€โ”€โ”€โ”€โ”€โ”€ - 5 โ”‚ "13e-1", - โ•ฐโ”€โ”€โ”€โ”€ - - ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[typescript/tests/cases/compiler/enumIdentifierLiterals.ts:6:5] - 5 โ”‚ "13e-1", - 6 โ”‚ 0xF00D - ยท โ”€โ”€โ”€โ”€โ”€โ”€ - 7 โ”‚ } - โ•ฐโ”€โ”€โ”€โ”€ - ร— Expected a semicolon or an implicit semicolon after a statement, but found none โ•ญโ”€[typescript/tests/cases/compiler/enumMemberResolution.ts:5:4] 4 โ”‚ var x = IgnoreRulesSpecific. // error @@ -9139,14 +9115,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private 40 โ”‚ [2] = 2, โ•ฐโ”€โ”€โ”€โ”€ - ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[typescript/tests/cases/compiler/literalsInComputedProperties1.ts:40:6] - 39 โ”‚ 1 = 1, - 40 โ”‚ [2] = 2, - ยท โ”€ - 41 โ”‚ "3" = 3, - โ•ฐโ”€โ”€โ”€โ”€ - ร— Invalid Character `!` โ•ญโ”€[typescript/tests/cases/compiler/manyCompilerErrorsInTheTwoFiles.ts:1:13] 1 โ”‚ const a =!@#!@$ @@ -20064,22 +20032,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private 3 โ”‚ } โ•ฐโ”€โ”€โ”€โ”€ - ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum7.ts:2:6] - 1 โ”‚ enum E { - 2 โ”‚ 1, 2, 3 - ยท โ”€ - 3 โ”‚ } - โ•ฐโ”€โ”€โ”€โ”€ - - ร— TS(2452): An enum member cannot have a numeric name. - โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnum7.ts:2:9] - 1 โ”‚ enum E { - 2 โ”‚ 1, 2, 3 - ยท โ”€ - 3 โ”‚ } - โ•ฐโ”€โ”€โ”€โ”€ - ร— Identifier expected. 'void' is a reserved word that cannot be used here. โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript5/EnumDeclarations/parserEnumDeclaration4.ts:1:6] 1 โ”‚ enum void { @@ -22207,15 +22159,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private 4 โ”‚ [e2] = 1 โ•ฐโ”€โ”€โ”€โ”€ - ร— Expected `,` but found `[` - โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName30.ts:4:5] - 3 โ”‚ [e] = id++ - 4 โ”‚ [e2] = 1 - ยท โ”ฌ - ยท โ•ฐโ”€โ”€ `,` expected - 5 โ”‚ } - โ•ฐโ”€โ”€โ”€โ”€ - ร— Expected a semicolon or an implicit semicolon after a statement, but found none โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName33.ts:4:11] 3 โ”‚ [e] = 0 @@ -22233,14 +22176,6 @@ Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/salsa/private 4 โ”‚ [e2] = 1 โ•ฐโ”€โ”€โ”€โ”€ - ร— TS(1164): Computed property names are not allowed in enums. - โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName34.ts:4:6] - 3 โ”‚ [e] = id++, - 4 โ”‚ [e2] = 1 - ยท โ”€โ”€ - 5 โ”‚ } - โ•ฐโ”€โ”€โ”€โ”€ - ร— Expected `]` but found `,` โ•ญโ”€[typescript/tests/cases/conformance/parser/ecmascript6/ComputedPropertyNames/parserComputedPropertyName35.ts:2:7] 1 โ”‚ var x = { diff --git a/tasks/coverage/snapshots/semantic_misc.snap b/tasks/coverage/snapshots/semantic_misc.snap index 7bf0e30195567..c33b7e2790fa7 100644 --- a/tasks/coverage/snapshots/semantic_misc.snap +++ b/tasks/coverage/snapshots/semantic_misc.snap @@ -102,15 +102,27 @@ after transform: [ReferenceId(36), ReferenceId(39), ReferenceId(82), ReferenceId rebuilt : [ReferenceId(281)] tasks/coverage/misc/pass/oxc-4449.ts -semantic error: Scope flags mismatch: +semantic error: Bindings mismatch: +after transform: ScopeId(1): ["A", "baz"] +rebuilt : ScopeId(1): ["A"] +Scope flags mismatch: after transform: ScopeId(1): ScopeFlags(StrictMode) rebuilt : ScopeId(1): ScopeFlags(StrictMode | Function) +Bindings mismatch: +after transform: ScopeId(2): ["B", "baz"] +rebuilt : ScopeId(2): ["B"] Scope flags mismatch: after transform: ScopeId(2): ScopeFlags(StrictMode) rebuilt : ScopeId(2): ScopeFlags(StrictMode | Function) +Bindings mismatch: +after transform: ScopeId(3): ["C", "baz"] +rebuilt : ScopeId(3): ["C"] Scope flags mismatch: after transform: ScopeId(3): ScopeFlags(StrictMode) rebuilt : ScopeId(3): ScopeFlags(StrictMode | Function) +Bindings mismatch: +after transform: ScopeId(4): ["D", "baz"] +rebuilt : ScopeId(4): ["D"] Scope flags mismatch: after transform: ScopeId(4): ScopeFlags(StrictMode) rebuilt : ScopeId(4): ScopeFlags(StrictMode | Function) @@ -142,25 +154,25 @@ Symbol flags mismatch for "A": after transform: SymbolId(0): SymbolFlags(RegularEnum) rebuilt : SymbolId(0): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "B": -after transform: SymbolId(1): SymbolFlags(RegularEnum) +after transform: SymbolId(2): SymbolFlags(RegularEnum) rebuilt : SymbolId(2): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "C": -after transform: SymbolId(2): SymbolFlags(RegularEnum) +after transform: SymbolId(4): SymbolFlags(RegularEnum) rebuilt : SymbolId(4): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "D": -after transform: SymbolId(3): SymbolFlags(RegularEnum) +after transform: SymbolId(6): SymbolFlags(RegularEnum) rebuilt : SymbolId(6): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "E": -after transform: SymbolId(4): SymbolFlags(RegularEnum) +after transform: SymbolId(8): SymbolFlags(RegularEnum) rebuilt : SymbolId(8): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "F": -after transform: SymbolId(6): SymbolFlags(RegularEnum) +after transform: SymbolId(10): SymbolFlags(RegularEnum) rebuilt : SymbolId(10): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "G": -after transform: SymbolId(8): SymbolFlags(RegularEnum) +after transform: SymbolId(12): SymbolFlags(RegularEnum) rebuilt : SymbolId(12): SymbolFlags(FunctionScopedVariable) Symbol flags mismatch for "H": -after transform: SymbolId(10): SymbolFlags(RegularEnum) +after transform: SymbolId(14): SymbolFlags(RegularEnum) rebuilt : SymbolId(14): SymbolFlags(FunctionScopedVariable) tasks/coverage/misc/pass/oxc-5177.ts diff --git a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md index 0632152579b3f..f586d7b0561d4 100644 --- a/tasks/prettier_conformance/snapshots/prettier.ts.snap.md +++ b/tasks/prettier_conformance/snapshots/prettier.ts.snap.md @@ -1,4 +1,4 @@ -ts compatibility: 185/526 (35.17%) +ts compatibility: 184/526 (34.98%) # Failed @@ -312,6 +312,7 @@ ts compatibility: 185/526 (35.17%) * end-of-line/multiline.ts ### enum +* enum/computed-members.ts * enum/enum.ts ### error-recovery