-
Notifications
You must be signed in to change notification settings - Fork 730
Fix encode and decode bug for binary ast #2006
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?
Conversation
|
@quininer please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
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.
Pull Request Overview
This PR fixes a bug in the encoder/decoder protocol for AST nodes. The fix addresses how empty parameter lists are handled for FunctionDeclaration nodes, ensuring the bitmask correctly indicates when the parameters property should be included in the child node structure.
- Updated
FunctionDeclarationencoding to check if parameters are both non-nil and non-empty - Implemented a new
getChildAtNodeIndexmethod that walks children by index without caching - Updated
getNamedChildto use the new uncached child lookup method - Added a test case validating function declaration with console.log call
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| internal/api/encoder/encoder.go | Modified FunctionDeclaration encoder to check for both non-nil and non-empty parameters when setting the child bitmask |
| _packages/api/src/node.ts | Added getChildAtNodeIndex method to retrieve children without caching; updated getNamedChild to use the new uncached method |
| _packages/api/test/api.test.ts | Added new test case for function declarations with multiple arguments to validate the fix |
| case ast.KindFunctionDeclaration: | ||
| n := node.AsFunctionDeclaration() | ||
| return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.AsteriskToken != nil) << 1) | (boolToByte(n.Name() != nil) << 2) | (boolToByte(n.TypeParameters != nil) << 3) | (boolToByte(n.Parameters != nil) << 4) | (boolToByte(n.Type != nil) << 5) | (boolToByte(n.Body != nil) << 6) | ||
| return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.AsteriskToken != nil) << 1) | (boolToByte(n.Name() != nil) << 2) | (boolToByte(n.TypeParameters != nil) << 3) | (boolToByte(n.Parameters != nil && len(n.Parameters.Nodes) > 0) << 4) | (boolToByte(n.Type != nil) << 5) | (boolToByte(n.Body != nil) << 6) |
Copilot
AI
Nov 3, 2025
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 FunctionDeclaration case now checks for both non-nil and non-empty parameters, but other similar function-like constructs (lines 485, 488, 491, 494, 497, 500, 503, 506, 521, 524, 596, 599, 689) only check for != nil. This inconsistency could lead to similar bugs with CallSignature, ConstructSignature, Constructor, GetAccessor, SetAccessor, IndexSignature, MethodSignature, MethodDeclaration, ArrowFunction, FunctionExpression, and other function-like nodes. Consider applying the same empty check (len(n.Parameters.Nodes) > 0) to all node types with Parameters properties for consistency.
| const propertyIndex = order - popcount8[~(mask | ((0xff << order) & 0xff)) & 0xff]; | ||
| return this.getOrCreateChildAtNodeIndex(this.index + 1 + propertyIndex); | ||
| return this.getChildAtNodeIndex(this.index, propertyIndex); | ||
| // return this.getOrCreateChildAtNodeIndex(this.index + 1 + propertyIndex); |
Copilot
AI
Nov 3, 2025
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.
Commented-out code should be removed. If this line is being kept temporarily for reference during development, it should be removed before merging to keep the codebase clean.
| // return this.getOrCreateChildAtNodeIndex(this.index + 1 + propertyIndex); |
| private getChildAtNodeIndex(parent: number, index: number): RemoteNode | RemoteNodeList | undefined { | ||
| let next = parent + 1; | ||
| let count = 0; | ||
| while (next !== 0) { | ||
| let child = new RemoteNode(this.view, this.decoder, next, this); | ||
|
|
||
| if (child.parent.index !== parent) { | ||
| return undefined; | ||
| } | ||
|
|
||
| if (count === index) { | ||
| if (child.kind == KIND_NODE_LIST) { | ||
| return new RemoteNodeList(this.view, this.decoder, child.index, this); | ||
| } else { | ||
| return child; | ||
| } | ||
| } | ||
|
|
||
| count += 1; | ||
| next = child.next; | ||
| } | ||
| } |
Copilot
AI
Nov 3, 2025
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 new getChildAtNodeIndex method is missing documentation. Add a JSDoc comment explaining its purpose, parameters (parent - the parent node index, index - the child's ordinal position), return value, and how it differs from getOrCreateChildAtNodeIndex (specifically that it doesn't cache children).
I encountered two bugs while using binary ast.
FunctionDeclaration.parameteris empty but not nil, it will occupy a slot but will not generate correspond node.getOrCreateChildAtNodeIndexassume a linear layout, but that's not true. likeI provided the correspond tests and fixes in PR, but I don't think the fixes here are optimal. it primarily serves as a means of revealing the cause of a bug.