diff --git a/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/fixture.ts b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/fixture.ts new file mode 100644 index 000000000000..b65a9983c6cd --- /dev/null +++ b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/fixture.ts @@ -0,0 +1 @@ +for(using foo in {}); \ No newline at end of file diff --git a/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/1-TSESTree-Error.shot b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/1-TSESTree-Error.shot new file mode 100644 index 000000000000..f914c11ac282 --- /dev/null +++ b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/1-TSESTree-Error.shot @@ -0,0 +1,7 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures statement ForInStatement _error_ using-initializer TSESTree - Error 1`] = ` +"TSError +> 1 | for(using foo in {}); + | ^^^^^^^^^ The left-hand side of a 'for...in' statement cannot be a 'using' declaration." +`; diff --git a/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/2-Babel-Error.shot b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/2-Babel-Error.shot new file mode 100644 index 000000000000..f1c91950c6ec --- /dev/null +++ b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/2-Babel-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures statement ForInStatement _error_ using-initializer Babel - Error 1`] = `[SyntaxError: For-in loop may not start with 'using' declaration. (1:4)]`; diff --git a/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/3-Alignment-Error.shot b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/3-Alignment-Error.shot new file mode 100644 index 000000000000..3987fd760265 --- /dev/null +++ b/packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/3-Alignment-Error.shot @@ -0,0 +1,3 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`AST Fixtures statement ForInStatement _error_ using-initializer Error Alignment 1`] = `"Both errored"`; diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index 7ebd5cc9e1cb..2fb094c1b589 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -933,6 +933,7 @@ export class Converter { }); case SyntaxKind.ForInStatement: + this.#checkForStatementDeclaration(node.initializer); return this.createNode(node, { type: AST_NODE_TYPES.ForInStatement, left: this.convertPattern(node.initializer), @@ -3510,4 +3511,14 @@ export class Converter { throw createError(message, this.ast, start, end); } + #checkForStatementDeclaration(initializer: ts.ForInitializer): void { + if (ts.isVariableDeclarationList(initializer)) { + if ((initializer.flags & ts.NodeFlags.Using) !== 0) { + this.#throwError( + initializer, + "The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", + ); + } + } + } }