feat(isolated-declarations): support optional class methods (#4035)

Example class
```
export class A {
  m1?(): void;
  m2(): void {}
}
```

Before the fix:
```
export declare class A {
  m1(): void;
}
```

with the fix:
```
export declare class A {
  m1?(): void;
  m2(): void;
}
```
This commit is contained in:
Egor Blinov 2024-07-03 03:46:59 +02:00 committed by GitHub
parent c043bec674
commit 7768d233c6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 9 additions and 1 deletions

View file

@ -2413,6 +2413,9 @@ impl<'a, const MINIFY: bool> Gen<MINIFY> for MethodDefinition<'a> {
if self.computed {
p.print(b']');
}
if self.optional {
p.print(b'?');
}
if let Some(type_parameters) = self.value.type_parameters.as_ref() {
type_parameters.gen(p, ctx);
}

View file

@ -201,6 +201,7 @@ fn typescript() {
);
test_ts("let foo: { <T>(t: T): void }", "let foo: {<T>(t: T): void};\n", false);
test_ts("function <const T>(){}", "function<const T>() {}\n", false);
test_ts("class A {m?(): void}", "class A {\n\tm?(): void;\n}\n", false);
}
fn test_comment_helper(source_text: &str, expected: &str) {

View file

@ -336,7 +336,9 @@ impl<'a> IsolatedDeclarations<'a> {
match element {
ClassElement::StaticBlock(_) => {}
ClassElement::MethodDefinition(ref method) => {
if !method.r#type.is_abstract() && method.value.body.is_none() {
if !(method.r#type.is_abstract() || method.optional)
&& method.value.body.is_none()
{
is_function_overloads = true;
} else if is_function_overloads {
// Skip implementation of function overloads

View file

@ -14,6 +14,7 @@ export class Zoo {
export abstract class Qux {
abstract foo(): void;
protected foo2?(): void;
bar(): void {}
baz(): void {}
}

View file

@ -15,6 +15,7 @@ export declare class Zoo {
}
export declare abstract class Qux {
abstract foo(): void;
protected foo2?(): void;
bar(): void;
baz(): void;
}