diff --git a/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/fixture.ts b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/fixture.ts new file mode 100644 index 000000000000..c323632adfe1 --- /dev/null +++ b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/fixture.ts @@ -0,0 +1,4 @@ +try { +} +catch (e = 1) { +} diff --git a/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/1-TSESTree-Error.shot b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/1-TSESTree-Error.shot new file mode 100644 index 000000000000..15afb67aa0f6 --- /dev/null +++ b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/1-TSESTree-Error.shot @@ -0,0 +1,11 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures special CatchClause _error_ with-initializer TSESTree - Error 1`] = ` +"TSError + 1 | try { + 2 | } +> 3 | catch (e = 1) { + | ^ Catch clause variable cannot have an initializer. + 4 | } + 5 |" +`; diff --git a/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/2-Babel-Error.shot b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/2-Babel-Error.shot new file mode 100644 index 000000000000..ded5064c6bac --- /dev/null +++ b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/2-Babel-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures special CatchClause _error_ with-initializer Babel - Error 1`] = `[SyntaxError: Unexpected token, expected ")" (3:9)]`; diff --git a/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/3-Alignment-Error.shot b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/3-Alignment-Error.shot new file mode 100644 index 000000000000..9562d8ee3c45 --- /dev/null +++ b/packages/ast-spec/src/special/CatchClause/fixtures/_error_/with-initializer/snapshots/3-Alignment-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures special CatchClause _error_ with-initializer Error Alignment 1`] = `"Both errored"`; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 5dcde889e8dd..4fea4cd7c8a5 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -886,6 +886,12 @@ export class Converter { }); case SyntaxKind.CatchClause: + if (node.variableDeclaration?.initializer) { + this.#throwError( + node.variableDeclaration.initializer, + 'Catch clause variable cannot have an initializer.', + ); + } return this.createNode(node, { type: AST_NODE_TYPES.CatchClause, param: node.variableDeclaration