Skip to content

Commit

Permalink
fix(isolated-declarations): false positive for class private method t…
Browse files Browse the repository at this point in the history
…hat has arguments without type annotations (#5964)

close: #5874
  • Loading branch information
Dunqing committed Sep 22, 2024
1 parent 6a9e71d commit 6df82ee
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
51 changes: 28 additions & 23 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ impl<'a> IsolatedDeclarations<'a> {
if self.has_internal_annotation(method.span) {
continue;
}

if !(method.r#type.is_abstract() || method.optional)
&& method.value.body.is_none()
{
Expand All @@ -384,36 +383,42 @@ impl<'a> IsolatedDeclarations<'a> {

let inferred_accessor_types = self.collect_inferred_accessor_types(decl);
let function = &method.value;
let params = if method.kind.is_set() {
method.key.static_name().map_or_else(
let params = match method.kind {
MethodDefinitionKind::Set => method.key.static_name().map_or_else(
|| self.transform_formal_parameters(&function.params),
|n| {
self.transform_set_accessor_params(
&function.params,
inferred_accessor_types.get(&self.ast.atom(&n)).map(|t| {
inferred_accessor_types.get(&self.ast.atom(&n)).map(|t|
// SAFETY: `ast.copy` is unsound! We need to fix.
unsafe { self.ast.copy(t) }
}),
unsafe { self.ast.copy(t) }),
)
},
)
} else {
self.transform_formal_parameters(&function.params)
};

if let MethodDefinitionKind::Constructor = method.kind {
elements.splice(
0..0,
self.transform_constructor_params_to_class_properties(
function, &params,
),
);
}
),
MethodDefinitionKind::Constructor => {
let params = self.transform_formal_parameters(&function.params);
elements.splice(
0..0,
self.transform_constructor_params_to_class_properties(
function, &params,
),
);

if method.accessibility.is_some_and(TSAccessibility::is_private) {
elements.push(self.transform_private_modifier_method(method));
continue;
}

if method.accessibility.is_some_and(TSAccessibility::is_private) {
elements.push(self.transform_private_modifier_method(method));
continue;
}
params
}
_ => {
if method.accessibility.is_some_and(TSAccessibility::is_private) {
elements.push(self.transform_private_modifier_method(method));
continue;
}
self.transform_formal_parameters(&function.params)
}
};

let return_type = match method.kind {
MethodDefinitionKind::Method => {
Expand Down
9 changes: 9 additions & 0 deletions crates/oxc_isolated_declarations/tests/fixtures/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ export class Bux {
export class PrivateFieldsWithConstructorAssignments {
private second = 0;
constructor(public first: number) {}
}


export class PrivateMethodClass {
private good(a): void {}
}

export class PublicMethodClass {
public bad(a): void {}
}
21 changes: 21 additions & 0 deletions crates/oxc_isolated_declarations/tests/snapshots/class.snap
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,24 @@ export declare class PrivateFieldsWithConstructorAssignments {
private second;
constructor(first: number);
}
export declare class PrivateMethodClass {
private good;
}
export declare class PublicMethodClass {
bad(a): void;
}


==================== Errors ====================

x TS9011: Parameter must have an explicit type annotation with
| --isolatedDeclarations.
,-[64:14]
63 | export class PublicMethodClass {
64 | public bad(a): void {}
: ^
65 | }
`----
```

0 comments on commit 6df82ee

Please sign in to comment.