From 9ff2a222f92152d1a68ca38eb0e9604570405b27 Mon Sep 17 00:00:00 2001 From: Solo-steven Date: Fri, 15 Sep 2023 13:50:37 +0800 Subject: [PATCH 1/5] feat: check declaration in for init, make sure no using keyword show up --- packages/typescript-estree/src/convert.ts | 21 +++++++++++++++++ .../tests/lib/convert.test.ts | 23 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index a6e35dd4bcc2..dc474e74808b 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -909,6 +909,9 @@ export class Converter { }); case SyntaxKind.ForStatement: + if (node.initializer) { + this.#checkForStatementDeclaration(node.initializer); + } return this.createNode(node, { type: AST_NODE_TYPES.ForStatement, init: this.convertChild(node.initializer), @@ -918,6 +921,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), @@ -926,6 +930,7 @@ export class Converter { }); case SyntaxKind.ForOfStatement: + this.#checkForStatementDeclaration(node.initializer); return this.createNode(node, { type: AST_NODE_TYPES.ForOfStatement, left: this.convertPattern(node.initializer), @@ -3458,4 +3463,20 @@ export class Converter { throw createError(message, this.ast, start, end); } + #checkForStatementDeclaration(initializer: ts.ForInitializer): void { + if (ts.isVariableDeclarationList(initializer)) { + if ( + !( + initializer.flags & ts.NodeFlags.Const || + initializer.flags & ts.NodeFlags.Let || + initializer.flags === ts.NodeFlags.None + ) + ) { + this.#throwError( + initializer, + " The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", + ); + } + } + } } diff --git a/packages/typescript-estree/tests/lib/convert.test.ts b/packages/typescript-estree/tests/lib/convert.test.ts index 9988c969b6a7..3f4a912b9cb5 100644 --- a/packages/typescript-estree/tests/lib/convert.test.ts +++ b/packages/typescript-estree/tests/lib/convert.test.ts @@ -361,4 +361,27 @@ describe('convert', () => { expect(Object.keys(esCallExpression)).toContain('typeParameters'); }); }); + describe('using should be forbidden in for-related initializer ', () => { + it('using should be forbidden in for in statement ', () => { + const ast = convertCode('for(using foo in {});'); + + const instance = new Converter(ast); + + expect(() => instance.convertProgram()).toThrow(); + }); + it('using should be forbidden in for of statement ', () => { + const ast = convertCode('for(using foo of {});'); + + const instance = new Converter(ast); + + expect(() => instance.convertProgram()).toThrow(); + }); + it('using should be forbidden in for statement ', () => { + const ast = convertCode('for(using i; i <= 0 ; ++i);'); + + const instance = new Converter(ast); + + expect(() => instance.convertProgram()).toThrow(); + }); + }); }); From a725c386a90b70678d481b0ef07cdb603ceb0f1d Mon Sep 17 00:00:00 2001 From: Solo-steven Date: Fri, 15 Sep 2023 13:50:37 +0800 Subject: [PATCH 2/5] feat: check declaration in for init, make sure no using keyword show up --- packages/typescript-estree/src/convert.ts | 21 +++++++++++++++++ .../tests/lib/convert.test.ts | 23 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index a6e35dd4bcc2..dc474e74808b 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -909,6 +909,9 @@ export class Converter { }); case SyntaxKind.ForStatement: + if (node.initializer) { + this.#checkForStatementDeclaration(node.initializer); + } return this.createNode(node, { type: AST_NODE_TYPES.ForStatement, init: this.convertChild(node.initializer), @@ -918,6 +921,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), @@ -926,6 +930,7 @@ export class Converter { }); case SyntaxKind.ForOfStatement: + this.#checkForStatementDeclaration(node.initializer); return this.createNode(node, { type: AST_NODE_TYPES.ForOfStatement, left: this.convertPattern(node.initializer), @@ -3458,4 +3463,20 @@ export class Converter { throw createError(message, this.ast, start, end); } + #checkForStatementDeclaration(initializer: ts.ForInitializer): void { + if (ts.isVariableDeclarationList(initializer)) { + if ( + !( + initializer.flags & ts.NodeFlags.Const || + initializer.flags & ts.NodeFlags.Let || + initializer.flags === ts.NodeFlags.None + ) + ) { + this.#throwError( + initializer, + " The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", + ); + } + } + } } diff --git a/packages/typescript-estree/tests/lib/convert.test.ts b/packages/typescript-estree/tests/lib/convert.test.ts index 9988c969b6a7..3f4a912b9cb5 100644 --- a/packages/typescript-estree/tests/lib/convert.test.ts +++ b/packages/typescript-estree/tests/lib/convert.test.ts @@ -361,4 +361,27 @@ describe('convert', () => { expect(Object.keys(esCallExpression)).toContain('typeParameters'); }); }); + describe('using should be forbidden in for-related initializer ', () => { + it('using should be forbidden in for in statement ', () => { + const ast = convertCode('for(using foo in {});'); + + const instance = new Converter(ast); + + expect(() => instance.convertProgram()).toThrow(); + }); + it('using should be forbidden in for of statement ', () => { + const ast = convertCode('for(using foo of {});'); + + const instance = new Converter(ast); + + expect(() => instance.convertProgram()).toThrow(); + }); + it('using should be forbidden in for statement ', () => { + const ast = convertCode('for(using i; i <= 0 ; ++i);'); + + const instance = new Converter(ast); + + expect(() => instance.convertProgram()).toThrow(); + }); + }); }); From 8ca11fb2959db2379862d7e8e3124a018ca50913 Mon Sep 17 00:00:00 2001 From: Solo-steven Date: Sat, 21 Oct 2023 19:09:44 +0800 Subject: [PATCH 3/5] fix(typescript-estree): allow for and for-of statement have using keyword declaration --- packages/typescript-estree/src/convert.ts | 4 ---- .../typescript-estree/tests/lib/convert.test.ts | 14 -------------- 2 files changed, 18 deletions(-) diff --git a/packages/typescript-estree/src/convert.ts b/packages/typescript-estree/src/convert.ts index dc474e74808b..1eda5a6e9207 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -909,9 +909,6 @@ export class Converter { }); case SyntaxKind.ForStatement: - if (node.initializer) { - this.#checkForStatementDeclaration(node.initializer); - } return this.createNode(node, { type: AST_NODE_TYPES.ForStatement, init: this.convertChild(node.initializer), @@ -930,7 +927,6 @@ export class Converter { }); case SyntaxKind.ForOfStatement: - this.#checkForStatementDeclaration(node.initializer); return this.createNode(node, { type: AST_NODE_TYPES.ForOfStatement, left: this.convertPattern(node.initializer), diff --git a/packages/typescript-estree/tests/lib/convert.test.ts b/packages/typescript-estree/tests/lib/convert.test.ts index 3f4a912b9cb5..4c6857a17a78 100644 --- a/packages/typescript-estree/tests/lib/convert.test.ts +++ b/packages/typescript-estree/tests/lib/convert.test.ts @@ -367,20 +367,6 @@ describe('convert', () => { const instance = new Converter(ast); - expect(() => instance.convertProgram()).toThrow(); - }); - it('using should be forbidden in for of statement ', () => { - const ast = convertCode('for(using foo of {});'); - - const instance = new Converter(ast); - - expect(() => instance.convertProgram()).toThrow(); - }); - it('using should be forbidden in for statement ', () => { - const ast = convertCode('for(using i; i <= 0 ; ++i);'); - - const instance = new Converter(ast); - expect(() => instance.convertProgram()).toThrow(); }); }); From 6fc8662f42d72f14189d9c12d7eb6d8adf1221fe Mon Sep 17 00:00:00 2001 From: Solo-steven Date: Tue, 9 Jan 2024 20:13:10 +0800 Subject: [PATCH 4/5] fix(typescript-estree): shorten for in statement check condition, move test case to ast-spec * shorten syntax check for using keyword in for-in statement. * move test file to ast-spec. --- .../fixtures/_error_/using-initializer/fixture.ts | 1 + .../using-initializer/snapshots/1-TSESTree-Error.shot | 7 +++++++ .../using-initializer/snapshots/2-Babel-Error.shot | 3 +++ .../using-initializer/snapshots/3-Alignment-Error.shot | 3 +++ packages/typescript-estree/src/convert.ts | 10 ++-------- packages/typescript-estree/tests/lib/convert.test.ts | 9 --------- 6 files changed, 16 insertions(+), 17 deletions(-) create mode 100644 packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/fixture.ts create mode 100644 packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/1-TSESTree-Error.shot create mode 100644 packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/2-Babel-Error.shot create mode 100644 packages/ast-spec/src/statement/ForInStatement/fixtures/_error_/using-initializer/snapshots/3-Alignment-Error.shot 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..493af2c0b4d7 --- /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 1eda5a6e9207..a4442704e108 100644 --- a/packages/typescript-estree/src/convert.ts +++ b/packages/typescript-estree/src/convert.ts @@ -3461,16 +3461,10 @@ export class Converter { } #checkForStatementDeclaration(initializer: ts.ForInitializer): void { if (ts.isVariableDeclarationList(initializer)) { - if ( - !( - initializer.flags & ts.NodeFlags.Const || - initializer.flags & ts.NodeFlags.Let || - initializer.flags === ts.NodeFlags.None - ) - ) { + if ((initializer.flags & ts.NodeFlags.Using) !== 0) { this.#throwError( initializer, - " The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", + "The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", ); } } diff --git a/packages/typescript-estree/tests/lib/convert.test.ts b/packages/typescript-estree/tests/lib/convert.test.ts index 4c6857a17a78..9988c969b6a7 100644 --- a/packages/typescript-estree/tests/lib/convert.test.ts +++ b/packages/typescript-estree/tests/lib/convert.test.ts @@ -361,13 +361,4 @@ describe('convert', () => { expect(Object.keys(esCallExpression)).toContain('typeParameters'); }); }); - describe('using should be forbidden in for-related initializer ', () => { - it('using should be forbidden in for in statement ', () => { - const ast = convertCode('for(using foo in {});'); - - const instance = new Converter(ast); - - expect(() => instance.convertProgram()).toThrow(); - }); - }); }); From acdd3c1fccf94defbfb5f3f949b5d99ea1596575 Mon Sep 17 00:00:00 2001 From: Brad Zacher Date: Thu, 11 Jan 2024 13:18:34 -0800 Subject: [PATCH 5/5] Update 1-TSESTree-Error.shot --- .../_error_/using-initializer/snapshots/1-TSESTree-Error.shot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 index 493af2c0b4d7..f914c11ac282 100644 --- 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 @@ -3,5 +3,5 @@ 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." + | ^^^^^^^^^ The left-hand side of a 'for...in' statement cannot be a 'using' declaration." `;