-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(es/parser): emit TS2371 for object defaults in declare signatures #12095
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
base: main
Are you sure you want to change the base?
Changes from all commits
c76a9ea
a15012a
a767462
732b90e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| swc_core: patch | ||
| swc_ecma_parser: patch | ||
| --- | ||
|
|
||
| fix(es/parser): emit TS2371 for object-pattern defaults in declare signatures |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,25 @@ fn assert_module_error(src: &'static str) -> Module { | |
| }) | ||
| } | ||
|
|
||
| #[test] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This new AGENTS.md reference: AGENTS.md:L52-L55 Useful? React with 👍 / 👎. |
||
| fn ts2371_declare_params_are_not_duplicated() { | ||
| // Eager parse-time TS2371 plus the post-list walker must not double-report. | ||
| test_parser( | ||
| "declare function top(a = 1): void; declare function obj({ a = 1 }): void;", | ||
| Syntax::Typescript(Default::default()), | ||
| |p| { | ||
| let _ = p.parse_typescript_module()?; | ||
| let errors = p.take_errors(); | ||
| let n = errors | ||
| .iter() | ||
| .filter(|e| matches!(e.kind(), crate::error::SyntaxError::TS2371)) | ||
| .count(); | ||
| assert_eq!(n, 2, "{errors:?}"); | ||
| Ok(()) | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn parse_program_module_01() { | ||
| module("import 'foo';"); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| declare function bar([x = 1]: number[]): void; | ||
| declare function baz([x, y = 2]: number[]): void; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/array-still-errors/input.ts:1:1] | ||
| 1 | declare function bar([x = 1]: number[]): void; | ||
| : ^^^^^ | ||
| 2 | declare function baz([x, y = 2]: number[]): void; | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/array-still-errors/input.ts:2:1] | ||
| 1 | declare function bar([x = 1]: number[]): void; | ||
| 2 | declare function baz([x, y = 2]: number[]): void; | ||
| : ^^^^^ | ||
| `---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| declare class C { | ||
| method({ a = 1 }: { a?: number }): void; | ||
| constructor({ a = 1 }: { a?: number }); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/declare-method/input.ts:2:1] | ||
| 1 | declare class C { | ||
| 2 | method({ a = 1 }: { a?: number }): void; | ||
| : ^^^^^ | ||
| 3 | constructor({ a = 1 }: { a?: number }); | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/declare-method/input.ts:3:1] | ||
| 2 | method({ a = 1 }: { a?: number }): void; | ||
| 3 | constructor({ a = 1 }: { a?: number }); | ||
| : ^^^^^ | ||
| 4 | } | ||
| `---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| declare function foo({ a: b = 1 }: { a?: number }): void; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/key-value-default/input.ts:1:1] | ||
| 1 | declare function foo({ a: b = 1 }: { a?: number }): void; | ||
| : ^^^^^ | ||
| `---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| declare function top(a = 1): void; | ||
| declare function obj({ a = 1 }): void; | ||
| declare function arr([a = 1]): void; | ||
| declare function nested({ a: { b = 1 } }): void; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/mixed/input.ts:1:1] | ||
| 1 | declare function top(a = 1): void; | ||
| : ^^^^^ | ||
| 2 | declare function obj({ a = 1 }): void; | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/mixed/input.ts:2:1] | ||
| 1 | declare function top(a = 1): void; | ||
| 2 | declare function obj({ a = 1 }): void; | ||
| : ^^^^^ | ||
| 3 | declare function arr([a = 1]): void; | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/mixed/input.ts:3:1] | ||
| 2 | declare function obj({ a = 1 }): void; | ||
| 3 | declare function arr([a = 1]): void; | ||
| : ^^^^^ | ||
| 4 | declare function nested({ a: { b = 1 } }): void; | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/mixed/input.ts:4:1] | ||
| 3 | declare function arr([a = 1]): void; | ||
| 4 | declare function nested({ a: { b = 1 } }): void; | ||
| : ^^^^^ | ||
| `---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| declare function foo({ a: { b = 1 } }: { a: { b?: number } }): void; | ||
| declare function bar({ a: [x = 1] }: { a: number[] }): void; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/nested-object/input.ts:1:1] | ||
| 1 | declare function foo({ a: { b = 1 } }: { a: { b?: number } }): void; | ||
| : ^^^^^ | ||
| 2 | declare function bar({ a: [x = 1] }: { a: number[] }): void; | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/nested-object/input.ts:2:1] | ||
| 1 | declare function foo({ a: { b = 1 } }: { a: { b?: number } }): void; | ||
| 2 | declare function bar({ a: [x = 1] }: { a: number[] }): void; | ||
| : ^^^^^ | ||
| `---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| declare function foo({ a = 1 }: { a?: number }): void; | ||
| declare function bar({ a = 1, b = 2 }: { a?: number; b?: number }): void; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/object-shorthand/input.ts:1:1] | ||
| 1 | declare function foo({ a = 1 }: { a?: number }): void; | ||
| : ^^^^^ | ||
| 2 | declare function bar({ a = 1, b = 2 }: { a?: number; b?: number }): void; | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/object-shorthand/input.ts:2:1] | ||
| 1 | declare function foo({ a = 1 }: { a?: number }): void; | ||
| 2 | declare function bar({ a = 1, b = 2 }: { a?: number; b?: number }): void; | ||
| : ^^^^^ | ||
| `---- | ||
| x A parameter initializer is only allowed in a function or constructor implementation | ||
| ,-[$DIR/tests/typescript-errors/ts2371/object-shorthand/input.ts:2:1] | ||
| 1 | declare function foo({ a = 1 }: { a?: number }): void; | ||
| 2 | declare function bar({ a = 1, b = 2 }: { a?: number; b?: number }): void; | ||
| : ^^^^^ | ||
| `---- |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| type T1 = (a = 1) => void; | ||
| type T2 = ({ a = 1 }: { a?: number }) => void; | ||
| type T3 = ({ a: b = 1 }: { a?: number }) => void; | ||
| interface I { | ||
| (x = 1): void; | ||
| ({ a = 1 }: { a?: number }): void; | ||
| method({ a = 1 }: { a?: number }): void; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For declare function/method signatures parsed under
Context::InParameters, nestedPat::Assigndefaults have already reported TS2371 fromparse_binding_elementbefore this post-parse walk runs. This line reports the same span a second time for cases such asdeclare function f([x = 1]): void;ordeclare function f({ a: b = 1 }): void;, so consumers and the newly added stderr fixtures will see duplicate TS2371 diagnostics unless the walker skips already-checked assign patterns in that context or the eager check is centralized here.Useful? React with 👍 / 👎.