Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(isolated_declarations): Don't require private method arguments to have type annotations #5874

Closed
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
3 changes: 3 additions & 0 deletions crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ impl<'a> IsolatedDeclarations<'a> {
continue;
}

let errors_length = self.errors.borrow().len();

let inferred_accessor_types = self.collect_inferred_accessor_types(decl);
let function = &method.value;
let params = if method.kind.is_set() {
Expand Down Expand Up @@ -404,6 +406,7 @@ impl<'a> IsolatedDeclarations<'a> {
}

if method.accessibility.is_some_and(TSAccessibility::is_private) {
self.errors.borrow_mut().truncate(errors_length);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit hacky. I think a less hacky way might be to decouple methods like transform_formal_parameters from generating errors and then move the check for errors after this block, but I wanted to get some feedback before starting to attempt something like that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can handle the private method early, can you try it?

elements.push(self.transform_private_modifier_method(method));
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const x = 42;
const y = '';
export function fooGood3({a = x, b: [{c = y}]}: object): void {}

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

// Incorrect
export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
export function fnDeclBad2<T>(p: T = [], r2: T): void { }
Expand All @@ -26,3 +30,7 @@ export function fooBad([a, b] = [1, 2]): number {
export const fooBad2 = ({a, b} = { a: 1, b: 2 }): number => {
return 2;
}

export class Bar {
public bad(a): void {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,65 +9,80 @@ export declare function fnDeclGood2(p?: T, rParam?: number): void;
export declare function fooGood([a, b]?: any[]): number;
export declare const fooGood2: ({ a, b }?: object) => number;
export declare function fooGood3({ a, b: [{ c }] }: object): void;
export declare class Foo {
private good;
}
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;
export declare function fooBad(): number;
export declare const fooBad2: () => number;
export declare class Bar {
bad(a): void;
}


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

x TS9025: Declaration emit for this parameter requires implicitly adding
| undefined to it's type. This is not supported with --isolatedDeclarations.
,-[18:30]
17 | // Incorrect
18 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
,-[22:30]
21 | // Incorrect
22 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
: ^^^^^^^^^
19 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
23 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
`----

x TS9025: Declaration emit for this parameter requires implicitly adding
| undefined to it's type. This is not supported with --isolatedDeclarations.
,-[18:41]
17 | // Incorrect
18 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
,-[22:41]
21 | // Incorrect
22 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
: ^^^^^^^^^^^^^^
19 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
23 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
`----

x TS9025: Declaration emit for this parameter requires implicitly adding
| undefined to it's type. This is not supported with --isolatedDeclarations.
,-[19:31]
18 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
19 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
,-[23:31]
22 | export function fnDeclBad<T>(p: T = [], rParam: T = "", r2: T): void { }
23 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
: ^^^^^^^^^
20 | export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
24 | export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
`----

x TS9025: Declaration emit for this parameter requires implicitly adding
| undefined to it's type. This is not supported with --isolatedDeclarations.
,-[20:31]
19 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
20 | export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
,-[24:31]
23 | export function fnDeclBad2<T>(p: T = [], r2: T): void { }
24 | export function fnDeclBad3<T>(p: T = [], rParam?: T, r2: T): void { }
: ^^^^^^^^^
21 |
25 |
`----

x TS9011: Parameter must have an explicit type annotation with
| --isolatedDeclarations.
,-[22:24]
21 |
22 | export function fooBad([a, b] = [1, 2]): number {
,-[26:24]
25 |
26 | export function fooBad([a, b] = [1, 2]): number {
: ^^^^^^^^^^^^^^^
23 | return 2;
27 | return 2;
`----

x TS9011: Parameter must have an explicit type annotation with
| --isolatedDeclarations.
,-[26:25]
25 |
26 | export const fooBad2 = ({a, b} = { a: 1, b: 2 }): number => {
,-[30:25]
29 |
30 | export const fooBad2 = ({a, b} = { a: 1, b: 2 }): number => {
: ^^^^^^^^^^^^^^^^^^^^^^^
27 | return 2;
31 | return 2;
`----

x TS9011: Parameter must have an explicit type annotation with
| --isolatedDeclarations.
,-[35:14]
34 | export class Bar {
35 | public bad(a): void {}
: ^
36 | }
`----