Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
chore: extract AST check from convert.ts to ast-checks.ts#11748
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
Lonercode wants to merge5 commits intotypescript-eslint:mainChoose a base branch fromLonercode:extract-ast-checks-from-converter
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+202 −40
Open
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
744c678 Chore: Extract AST check from convert.ts to ast-checks.ts
Lonercodeb52f216 chore: extract AST check from convert.ts to ast-checks.ts
Lonercode9270fc4 chore: add test for ast-checks.ts
Lonercode6ced6b3 chore: add test for ast-checks.ts
Lonercode65803e4 chore: add test for ast-checks.ts
LonercodeFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
54 changes: 54 additions & 0 deletionspackages/typescript-estree/src/ast-checks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import * as ts from 'typescript'; | ||
| import type { TSESTree } from './ts-estree'; | ||
| import { isValidAssignmentTarget } from './node-utils'; | ||
| export function checkForStatementDeclaration( | ||
| initializer: ts.ForInitializer, | ||
| kind: ts.SyntaxKind.ForInStatement | ts.SyntaxKind.ForOfStatement, | ||
| throwError: ( | ||
| node: number | ts.Node | TSESTree.Range, | ||
| message: string, | ||
| ) => never, | ||
| ): void { | ||
| const loop = kind === ts.SyntaxKind.ForInStatement ? 'for...in' : 'for...of'; | ||
| if (ts.isVariableDeclarationList(initializer)) { | ||
| if (initializer.declarations.length !== 1) { | ||
| throwError( | ||
| initializer, | ||
| `Only a single variable declaration is allowed in a '${loop}' statement.`, | ||
| ); | ||
| } | ||
| const declaration = initializer.declarations[0]; | ||
| if (declaration.initializer) { | ||
| throwError( | ||
| declaration, | ||
| `The variable declaration of a '${loop}' statement cannot have an initializer.`, | ||
| ); | ||
| } else if (declaration.type) { | ||
| throwError( | ||
| declaration, | ||
| `The variable declaration of a '${loop}' statement cannot have a type annotation.`, | ||
| ); | ||
| } | ||
| if ( | ||
| kind === ts.SyntaxKind.ForInStatement && | ||
| initializer.flags & ts.NodeFlags.Using | ||
| ) { | ||
| throwError( | ||
| initializer, | ||
| "The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", | ||
| ); | ||
| } | ||
| } else if ( | ||
| !isValidAssignmentTarget(initializer) && | ||
| initializer.kind !== ts.SyntaxKind.ObjectLiteralExpression && | ||
| initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression | ||
| ) { | ||
| throwError( | ||
| initializer, | ||
| `The left-hand side of a '${loop}' statement must be a variable or a property access.`, | ||
| ); | ||
| } | ||
| } |
42 changes: 2 additions & 40 deletionspackages/typescript-estree/src/convert.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletionspackages/typescript-estree/tests/lib/ast-checks.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| import * as ts from 'typescript'; | ||
| import type { TSESTree } from '../../src/ts-estree'; | ||
| import { checkForStatementDeclaration } from '../../src/ast-checks'; | ||
| describe(checkForStatementDeclaration, () => { | ||
| let throwError: ( | ||
| node: number | ts.Node | TSESTree.Range, | ||
| message: string, | ||
| ) => never; | ||
| beforeEach(() => { | ||
| throwError = vi.fn((node, message: string) => { | ||
| throw new Error(message); | ||
| }) as unknown as typeof throwError; | ||
| }); | ||
| it('allows single variable declaration with no initializer or type', () => { | ||
| const node = ts.factory.createVariableDeclarationList([ | ||
| ts.factory.createVariableDeclaration('x'), | ||
| ]); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| node, | ||
| ts.SyntaxKind.ForOfStatement, | ||
| throwError, | ||
| ), | ||
| ).not.toThrow(); | ||
| }); | ||
| it('throws when multiple declarations', () => { | ||
| const node = ts.factory.createVariableDeclarationList([ | ||
| ts.factory.createVariableDeclaration('x'), | ||
| ts.factory.createVariableDeclaration('y'), | ||
| ]); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| node, | ||
| ts.SyntaxKind.ForOfStatement, | ||
| throwError, | ||
| ), | ||
| ).toThrow( | ||
| "Only a single variable declaration is allowed in a 'for...of' statement.", | ||
| ); | ||
| }); | ||
| it('throws when declaration has an initializer', () => { | ||
| const node = ts.factory.createVariableDeclarationList([ | ||
| ts.factory.createVariableDeclaration( | ||
| 'x', | ||
| undefined, | ||
| undefined, | ||
| ts.factory.createNumericLiteral(5), | ||
| ), | ||
| ]); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| node, | ||
| ts.SyntaxKind.ForOfStatement, | ||
| throwError, | ||
| ), | ||
| ).toThrow( | ||
| "The variable declaration of a 'for...of' statement cannot have an initializer.", | ||
| ); | ||
| }); | ||
| it('throws when declaration has a type annotation', () => { | ||
| const node = ts.factory.createVariableDeclarationList([ | ||
| ts.factory.createVariableDeclaration( | ||
| 'x', | ||
| undefined, | ||
| ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword), | ||
| ), | ||
| ]); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| node, | ||
| ts.SyntaxKind.ForOfStatement, | ||
| throwError, | ||
| ), | ||
| ).toThrow( | ||
| "The variable declaration of a 'for...of' statement cannot have a type annotation.", | ||
| ); | ||
| }); | ||
| it('throws when "using" declaration in for...in', () => { | ||
| const node = ts.factory.createVariableDeclarationList( | ||
| [ts.factory.createVariableDeclaration('x')], | ||
| ts.NodeFlags.Using, | ||
| ); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| node, | ||
| ts.SyntaxKind.ForInStatement, | ||
| throwError, | ||
| ), | ||
| ).toThrow( | ||
| "The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", | ||
| ); | ||
| }); | ||
| it('throws when initializer is not a valid assignment target', () => { | ||
| const node = ts.factory.createNumericLiteral(42); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| node, | ||
| ts.SyntaxKind.ForOfStatement, | ||
| throwError, | ||
| ), | ||
| ).toThrow( | ||
| "The left-hand side of a 'for...of' statement must be a variable or a property access.", | ||
| ); | ||
| }); | ||
| it('handles both for...in and for...of paths', () => { | ||
| const forInNode = ts.factory.createVariableDeclarationList([ | ||
| ts.factory.createVariableDeclaration('x'), | ||
| ]); | ||
| const forOfNode = ts.factory.createVariableDeclarationList([ | ||
| ts.factory.createVariableDeclaration('x'), | ||
| ]); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| forInNode, | ||
| ts.SyntaxKind.ForInStatement, | ||
| throwError, | ||
| ), | ||
| ).not.toThrow(); | ||
| expect(() => | ||
| checkForStatementDeclaration( | ||
| forOfNode, | ||
| ts.SyntaxKind.ForOfStatement, | ||
| throwError, | ||
| ), | ||
| ).not.toThrow(); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.