Skip to content
Merged
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
1 change: 1 addition & 0 deletions crates/oxc_ast/src/ast/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,7 @@ pub struct MethodDefinition<'a> {
pub r#type: MethodDefinitionType,
#[ts]
pub decorators: Vec<'a, Decorator<'a>>,
#[estree(via = MethodDefinitionKey)]
pub key: PropertyKey<'a>,
#[visit(args(flags = match self.kind {
MethodDefinitionKind::Get => ScopeFlags::Function | ScopeFlags::GetAccessor,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/generated/derive_estree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1514,7 +1514,7 @@ impl ESTree for MethodDefinition<'_> {
state.serialize_field("end", &self.span.end);
state.serialize_field("static", &self.r#static);
state.serialize_field("computed", &self.computed);
state.serialize_field("key", &self.key);
state.serialize_field("key", &crate::serialize::MethodDefinitionKey(self));
state.serialize_field("kind", &self.kind);
state.serialize_field("value", &self.value);
state.serialize_ts_field("decorators", &self.decorators);
Expand Down
46 changes: 46 additions & 0 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,52 @@ impl ESTree for FunctionFormalParameters<'_, '_> {
}
}

/// Serializer for `key` field of `MethodDefinition`.
///
/// In TS-ESTree `"constructor"` in `class C { "constructor"() {} }`
/// is represented as an `Identifier`.
/// In Acorn and Espree, it's a `Literal`.
/// <https://github.com/typescript-eslint/typescript-eslint/issues/11084>
#[ast_meta]
#[estree(
ts_type = "PropertyKey",
raw_deser = "
/* IF_JS */
DESER[PropertyKey](POS_OFFSET.key)
/* END_IF_JS */

/* IF_TS */
let key = DESER[PropertyKey](POS_OFFSET.key);
if (THIS.kind === 'constructor') {
key = {
type: 'Identifier',
start: key.start,
end: key.end,
name: 'constructor',
decorators: [],
optional: false,
typeAnnotation: null,
};
}
key
/* END_IF_TS */
"
)]
pub struct MethodDefinitionKey<'a, 'b>(pub &'b MethodDefinition<'a>);

impl ESTree for MethodDefinitionKey<'_, '_> {
fn serialize<S: Serializer>(&self, serializer: S) {
let method = self.0;
if S::INCLUDE_TS_FIELDS && method.kind == MethodDefinitionKind::Constructor {
// `key` can only be either an identifier `constructor`, or string `"constructor"`
let span = method.key.span();
IdentifierName { span, name: Atom::from("constructor") }.serialize(serializer);
} else {
method.key.serialize(serializer);
}
}
}

/// Serializer for `extends` field of `TSInterfaceDeclaration`.
///
/// Serialize `extends` as an empty array if it's `None`.
Expand Down
17 changes: 15 additions & 2 deletions napi/parser/deserialize-ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -932,14 +932,27 @@ function deserializeClassBody(pos) {
}

function deserializeMethodDefinition(pos) {
const kind = deserializeMethodDefinitionKind(pos + 72);
let key = deserializePropertyKey(pos + 48);
if (kind === 'constructor') {
key = {
type: 'Identifier',
start: key.start,
end: key.end,
name: 'constructor',
decorators: [],
optional: false,
typeAnnotation: null,
};
}
return {
type: deserializeMethodDefinitionType(pos + 8),
start: deserializeU32(pos),
end: deserializeU32(pos + 4),
static: deserializeBool(pos + 74),
computed: deserializeBool(pos + 73),
key: deserializePropertyKey(pos + 48),
kind: deserializeMethodDefinitionKind(pos + 72),
key,
kind,
value: deserializeBoxFunction(pos + 64),
decorators: deserializeVecDecorator(pos + 16),
override: deserializeBool(pos + 75),
Expand Down
3 changes: 1 addition & 2 deletions tasks/coverage/snapshots/estree_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ commit: 15392346

estree_typescript Summary:
AST Parsed : 10619/10725 (99.01%)
Positive Passed: 9037/10725 (84.26%)
Positive Passed: 9038/10725 (84.27%)
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts
A class member cannot have the 'const' keyword.
Mismatch: tasks/coverage/typescript/tests/cases/compiler/accessOverriddenBaseClassMember1.ts
Expand Down Expand Up @@ -1055,7 +1055,6 @@ Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/constructorD
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/constructorDeclarations/constructorParameters/readonlyReadonly.ts
readonly' modifier already seen.
Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/constructorDeclarations/constructorWithExpressionLessReturn.ts
Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/constructorDeclarations/quotedConstructors.ts
Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassParameterProperties.ts
Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperCallsWithThisArg.ts
Mismatch: tasks/coverage/typescript/tests/cases/conformance/classes/constructorDeclarations/superCalls/derivedClassSuperStatementPosition.ts
Expand Down
Loading