Skip to content

Commit

Permalink
fix(isolated_declarations): Don't report an error for parameters if t…
Browse files Browse the repository at this point in the history
…hey are ObjectPattern or ArrayPattern with an explicit type (#4065)

The logic in #3810 was slightly
off as demonstrated by the snapshot test.

Co-authored-by: MichaelMitchell-at <=>
  • Loading branch information
MichaelMitchell-at authored Jul 6, 2024
1 parent 65aee19 commit adee728
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
10 changes: 7 additions & 3 deletions crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ impl<'a> IsolatedDeclarations<'a> {
is_remaining_params_have_required: bool,
) -> Option<FormalParameter<'a>> {
let pattern = &param.pattern;
if pattern.type_annotation.is_none() && pattern.kind.is_destructuring_pattern() {
self.error(parameter_must_have_explicit_type(param.span));
return None;
if let BindingPatternKind::AssignmentPattern(pattern) = &pattern.kind {
if pattern.left.kind.is_destructuring_pattern()
&& pattern.left.type_annotation.is_none()
{
self.error(parameter_must_have_explicit_type(param.span));
return None;
}
}

let is_assignment_pattern = pattern.kind.is_assignment_pattern();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ input_file: crates/oxc_isolated_declarations/tests/fixtures/function-parameters.

export declare function fnDeclGood(p?: T, rParam?: string): void;
export declare function fnDeclGood2(p?: T, rParam?: number): void;
export declare function fooGood(): number;
export declare const fooGood2: () => number;
export declare function fooGood([a, b]?: any[]): number;
export declare const fooGood2: ({ a, b }?: object) => number;
export declare function fnDeclBad<T>(p: T, rParam: T, r2: T): void;
export declare function fnDeclBad2<T>(p: T, r2: T): void;
export declare function fnDeclBad3<T>(p: T, rParam?: T, r2: T): void;
Expand All @@ -17,24 +17,6 @@ export declare const fooBad2: () => number;

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

x TS9011: Parameter must have an explicit type annotation with
| --isolatedDeclarations.
,-[5:25]
4 |
5 | export function fooGood([a, b]: any[] = [1, 2]): number {
: ^^^^^^^^^^^^^^^^^^^^^^
6 | return 2;
`----
x TS9011: Parameter must have an explicit type annotation with
| --isolatedDeclarations.
,-[9:26]
8 |
9 | export const fooGood2 = ({a, b}: object = { a: 1, b: 2 }): number => {
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10 | return 2;
`----
x TS9025: Declaration emit for this parameter requires implicitly adding
| undefined to it's type. This is not supported with --isolatedDeclarations.
,-[14:30]
Expand Down

0 comments on commit adee728

Please sign in to comment.