-
-
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 1 commit
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 |
|---|---|---|
|
|
@@ -77,6 +77,49 @@ impl<I: Tokens> Parser<I> { | |
| } | ||
| } | ||
|
|
||
| /// Emit TS2371 for any parameter initializer nested in a binding pattern. | ||
| /// | ||
| /// Used for ambient / declare function and constructor signatures where | ||
| /// top-level AssignPat checks alone miss object shorthand defaults and | ||
| /// nested defaults. | ||
| pub(crate) fn emit_ts2371_for_param_initializers(&mut self, pat: &Pat) { | ||
| match pat { | ||
| Pat::Assign(a) => { | ||
| self.emit_err(a.span(), SyntaxError::TS2371); | ||
|
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.
For declare function/method signatures parsed under Useful? React with 👍 / 👎. |
||
| self.emit_ts2371_for_param_initializers(&a.left); | ||
| } | ||
| Pat::Array(arr) => { | ||
| for elem in arr.elems.iter().flatten() { | ||
| self.emit_ts2371_for_param_initializers(elem); | ||
| } | ||
| } | ||
| Pat::Object(obj) => { | ||
| for prop in &obj.props { | ||
| match prop { | ||
| ObjectPatProp::KeyValue(KeyValuePatProp { value, .. }) | ||
| | ObjectPatProp::Rest(RestPat { arg: value, .. }) => { | ||
| self.emit_ts2371_for_param_initializers(value); | ||
| } | ||
| ObjectPatProp::Assign(AssignPatProp { | ||
| span, | ||
| value: Some(_), | ||
| .. | ||
| }) => { | ||
| self.emit_err(*span, SyntaxError::TS2371); | ||
| } | ||
| ObjectPatProp::Assign(AssignPatProp { value: None, .. }) => {} | ||
| #[cfg(swc_ast_unknown)] | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
| Pat::Rest(r) => self.emit_ts2371_for_param_initializers(&r.arg), | ||
| Pat::Ident(_) | Pat::Invalid(_) | Pat::Expr(_) => {} | ||
| #[cfg(swc_ast_unknown)] | ||
| _ => {} | ||
| } | ||
| } | ||
|
|
||
| fn assign_pat_type_ann(&mut self, pat: &mut Pat, span: Span, type_ann: Box<TsType>) { | ||
| let type_ann = Some(Box::new(TsTypeAnn { span, type_ann })); | ||
|
|
||
|
|
||
| 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; | ||
| : ^^^^^ | ||
| `---- |
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.
This check runs for every object binding pattern parsed under
Context::InDeclare, not just function/constructor parameters. For example,declare const { a = 1 }: { a?: number };reaches this branch while parsing the variable declarator, and TypeScript accepts that ambient destructuring binding, but SWC will now emit the parameter-only TS2371 diagnostic. Narrow the eager check to parameter/signature parsing (or rely on the post-parse parameter traversal) so declare variable bindings are not rejected.Useful? React with 👍 / 👎.