-
-
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?
chore: extract AST check from convert.ts to ast-checks.ts #11748
Conversation
|
Thanks for the PR, @Lonercode! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
View your CI Pipeline Execution ↗ for commit 29a9c73
☁️ Nx Cloud last updated this comment at |
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (25.75%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #11748 +/- ##
==========================================
- Coverage 90.49% 90.48% -0.01%
==========================================
Files 522 523 +1
Lines 53360 53375 +15
Branches 8911 8913 +2
==========================================
+ Hits 48286 48298 +12
- Misses 5059 5062 +3
Partials 15 15
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
|
Can we run the AST check from https://github.com/Lonercode/typescript-eslint/blob/65803e4127b9557ec6cf4802df12e588370d9930/packages/typescript-estree/src/convert.ts#L509, and have a big switch-case in the new file to check every ts node? |
AST check |
|
No need change check-modifiers.ts. In the new file export a function to check ts node, it calls |
|
|
||
| case SyntaxKind.ForInStatement: | ||
| this.#checkForStatementDeclaration(node.initializer, node.kind); | ||
| this.#checkTSNode(node, node.initializer, node.kind); |
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.
This should be able to be removed?
JoshuaKGoldberg
left a comment
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.
What fisker said 🙂
|
@fisker Please let me know if I need to remove every call to the ast check function from convert.ts. |
|
|
| } | ||
|
|
||
| checkModifiers(node); | ||
| checkTSNode(node, this.#throwError, initializer, kind); |
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.
initializer and kind should access from node directly in the check function.
| import { checkModifiers } from './check-modifiers'; | ||
| import { isValidAssignmentTarget } from './node-utils'; | ||
|
|
||
| export function checkTSNode( |
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.
I prefer export as checkSyntaxError, and rename the file accordingly.
|
|
||
| export function checkTSNode( | ||
| node: ts.Node, | ||
| throwError: ( |
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.
This won't be necessary to pass, after we merge #11775
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.
This means we should remove the throwError parameter then?
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.
#11775 is merged, we should be able to throw directly.
| } | ||
| } | ||
|
|
||
| export function checkForStatementDeclaration( |
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.
This should be an internal functionality, no need export.
| @@ -0,0 +1,196 @@ | |||
| import * as ts from 'typescript'; | |||
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.
This test is unnecessary, they have already been well tested.
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.
Was trying to align with codecov requirements. I can remove the file if it could still be merged. Should we also skip testing the new checkSyntaxError function?
| checkForStatementDeclaration( | ||
| (node as ts.ForInStatement | ts.ForOfStatement).initializer, | ||
| node.kind, | ||
| ); | ||
| break; | ||
| } | ||
| default: { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function checkForStatementDeclaration( | ||
| initializer: ts.ForInitializer, | ||
| kind: ts.SyntaxKind, | ||
| ): void { |
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.
| checkForStatementDeclaration( | |
| (node as ts.ForInStatement | ts.ForOfStatement).initializer, | |
| node.kind, | |
| ); | |
| break; | |
| } | |
| default: { | |
| break; | |
| } | |
| } | |
| } | |
| function checkForStatementDeclaration( | |
| initializer: ts.ForInitializer, | |
| kind: ts.SyntaxKind, | |
| ): void { | |
| checkForStatementDeclaration(node); | |
| break; | |
| } | |
| default: { | |
| break; | |
| } | |
| } | |
| } | |
| function checkForStatementDeclaration(node: ts.ForInStatement | ts.ForOfStatement): void { | |
| const {initializer, kind} = node; |
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.
The only issue with this is that an error is thrown unless I specify the node type in checkForStatementDeclaration so I may still need to do checkForStatmentDeclaration(node as ts.ForInStatment | ts.ForOfStatment) when calling it in checkSyntaxErrors
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.
Ts can't narrow type?
Looking at the difference,
checkSyntaxError(node: ts.Node)
while convertNode uses TSNode
maybe change to it will fix the issue?
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.
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.
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.
How about
function checkSyntaxError(tsNode: ts.node): void {
checkModifiers(tsNode)
const node = tsNode as TSNode;
switch (...) {}
}
convert.ts also do similar.
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.
That works :)
|
|
||
| import { checkModifiers } from './check-modifiers'; | ||
| import { isValidAssignmentTarget, createError } from './node-utils'; | ||
|
|
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
const SyntaxKind = ts.SyntaxKind;
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.
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?
|
@JoshuaKGoldberg Are you fine with just one check in this PR? Or should all checks done in this one? If we want to copy variable declaration syntax check, better wait for #11777 and #11778 to merge first. |
| default: { | ||
| break; | ||
| } |
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.
Add this because ESLint complaint? Will this work?
| default: { | |
| break; | |
| } | |
| // No default |
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.
I think it's valid, exiting with the default, for now at least.
|
Looks good to me, thanks! 🙏 Can't approve since I'm not a team member. |
|
Thanks @fisker |
JoshuaKGoldberg
left a comment
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.
Thanks for getting started on this! I think we should discuss the direction a bit.
|
|
||
| const node = tsNode as TSNode; | ||
|
|
||
| switch (node.kind) { |
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.
[Refactor] I don't think this is a positive change for the logic? Converter functions generally directly call the checkFor* logic they need. For example, for-in and for-of statements directly call checkForStatementDeclaration. Now this new checkSyntaxError function has to again switch on node.kind. That's extra code structure and work.
@fisker please correct me if I'm wrong here, but I think it'd still be in the spirit of the issue to have checkForStatementDeclaration be a standalone function separated out from convert.ts, and still called directly?
As in, the diffs to convert.ts would look more like:
+ 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(...) { ... }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.
Take this
typescript-eslint/packages/typescript-estree/src/convert.ts
Lines 802 to 811 in 16cf0f7
| if ( | |
| node.caseBlock.clauses.filter( | |
| switchCase => switchCase.kind === SyntaxKind.DefaultClause, | |
| ).length > 1 | |
| ) { | |
| this.#throwError( | |
| node, | |
| "A 'default' clause cannot appear more than once in a 'switch' statement.", | |
| ); | |
| } |
SyntaxKind.ForInStatement.
If we call from covert.ts directly after the case ..., we'll have lot of lines to add. and forcing many functions to import/export.
I think a single call like checkModifiers() used did and duplicate the big switch(){} in check-syntax-error.ts is much easier to maintain.
Most kind of node check will inline, instead of separate function.
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.
@JoshuaKGoldberg @fisker Would it prove more efficient to separate out checkForStatmentDeclaration? I thought the point of the issue was to extract those checks?



PR Checklist
Converter#11695Overview
This PR extracts the AST check (checkForStatementDeclaration) from
convert.tsto a fileast-checks.tsas requested in the issue due to the increasing length of code in the file. Therefore, a new fie has been added (ast-checks.ts), one modified (convert.ts) and tests have been run.