-
-
Notifications
You must be signed in to change notification settings - Fork 2.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 our terms of service and privacy 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 from all commits
744c678
b52f216
9270fc4
6ced6b3
65803e4
b628bfa
78f1686
001ea12
f696ce1
5eb48ef
37c500f
dc4dcc0
4e5902b
91d2b00
336c584
ee1ada9
29a9c73
File filter
Filter by extension
Conversations
Jump to
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'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 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 in 16cf0f7
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.`, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should also define a
here, https://github.com/Lonercode/typescript-eslint/blob/ee1ada9db5229902f70faa19dc27950dc457cc7d/packages/typescript-estree/src/convert.ts#L42
so other checks can be copied easier to here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll add lots of
cases in switch, better align with convert.ts.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least keep it before it's done?