Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.9k
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
base:main
Are you sure you want to change the base?
Changes fromall commits
744c678b52f2169270fc46ced6b365803e4b628bfa78f1686001ea12f696ce15eb48ef37c500fdc4dcc04e5902b91d2b00336c584ee1ada929a9c73File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,70 @@ | ||||||||||||||||||||||
| import * as ts from 'typescript'; | ||||||||||||||||||||||
| import type { TSNode } from './ts-estree'; | ||||||||||||||||||||||
| import { checkModifiers } from './check-modifiers'; | ||||||||||||||||||||||
| import { isValidAssignmentTarget, createError } from './node-utils'; | ||||||||||||||||||||||
Contributor 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. We should also define a so other checks can be copied easier to here. Member 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. Ehh should we? We don't generally do namespace property shorthands like that in the typescript-eslint monorepo. IMO it just adds a bit of clutter. Contributor 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. We'll add lots of Contributor 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. At least keep it before it's done? | ||||||||||||||||||||||
| const SyntaxKind = ts.SyntaxKind; | ||||||||||||||||||||||
| export function checkSyntaxError(tsNode: ts.Node): void { | ||||||||||||||||||||||
| checkModifiers(tsNode); | ||||||||||||||||||||||
| const node = tsNode as TSNode; | ||||||||||||||||||||||
| switch (node.kind) { | ||||||||||||||||||||||
Member 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. [Refactor] I don't think this is a positive change for the logic? Converter functions generally directly call the @fisker please correct me if I'm wrong here, but I think it'd still be in the spirit of the issue to have As in, the diffs to + import { checkForStatementDeclaration } from "./checks/...";... case SyntaxKind.ForInStatement:- this.#checkForStatementDeclaration(node.initializer, node.kind);+ checkForStatementDeclaration(node.initializer, node.kind); return this.createNode<TSESTree.ForInStatement>(node, {...- #checkForStatementDeclaration(...) { ... } Contributor 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. Take this typescript-eslint/packages/typescript-estree/src/convert.ts Lines 802 to 811 in16cf0f7
SyntaxKind.ForInStatement.If we call from covert.ts directly after the I think a single call like Most kind of node check will inline, instead of separate function. Author 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. @JoshuaKGoldberg@fisker Would it prove more efficient to separate out | ||||||||||||||||||||||
| case SyntaxKind.ForInStatement: | ||||||||||||||||||||||
| case SyntaxKind.ForOfStatement: { | ||||||||||||||||||||||
| checkForStatementDeclaration(node); | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| default: { | ||||||||||||||||||||||
| break; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
Comment on lines +21 to +23 Contributor 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. Add this because ESLint complaint? Will this work? Suggested change
Author 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. I think it's valid, exiting with the default, for now at least. | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| function checkForStatementDeclaration( | ||||||||||||||||||||||
| node: ts.ForInStatement | ts.ForOfStatement, | ||||||||||||||||||||||
| ): void { | ||||||||||||||||||||||
| const { initializer, kind } = node; | ||||||||||||||||||||||
| const loop = kind === SyntaxKind.ForInStatement ? 'for...in' : 'for...of'; | ||||||||||||||||||||||
| if (ts.isVariableDeclarationList(initializer)) { | ||||||||||||||||||||||
| if (initializer.declarations.length !== 1) { | ||||||||||||||||||||||
| throw createError( | ||||||||||||||||||||||
| initializer, | ||||||||||||||||||||||
| `Only a single variable declaration is allowed in a '${loop}' statement.`, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| const declaration = initializer.declarations[0]; | ||||||||||||||||||||||
| if (declaration.initializer) { | ||||||||||||||||||||||
| throw createError( | ||||||||||||||||||||||
| declaration, | ||||||||||||||||||||||
| `The variable declaration of a '${loop}' statement cannot have an initializer.`, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } else if (declaration.type) { | ||||||||||||||||||||||
| throw createError( | ||||||||||||||||||||||
| declaration, | ||||||||||||||||||||||
| `The variable declaration of a '${loop}' statement cannot have a type annotation.`, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| if ( | ||||||||||||||||||||||
| kind === SyntaxKind.ForInStatement && | ||||||||||||||||||||||
| initializer.flags & ts.NodeFlags.Using | ||||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| throw createError( | ||||||||||||||||||||||
| initializer, | ||||||||||||||||||||||
| "The left-hand side of a 'for...in' statement cannot be a 'using' declaration.", | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } else if ( | ||||||||||||||||||||||
| !isValidAssignmentTarget(initializer) && | ||||||||||||||||||||||
| initializer.kind !== SyntaxKind.ObjectLiteralExpression && | ||||||||||||||||||||||
| initializer.kind !== SyntaxKind.ArrayLiteralExpression | ||||||||||||||||||||||
| ) { | ||||||||||||||||||||||
| throw createError( | ||||||||||||||||||||||
| initializer, | ||||||||||||||||||||||
| `The left-hand side of a '${loop}' statement must be a variable or a property access.`, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||