Skip to content

Commit 34da224

Browse files
hardfistquininer
authored andcommitted
fix: Fix encode and decode bug for binary ast (#3)
* Add regression test for function * fix function params encode * fix child at index --------- Co-authored-by: quininer <quininer@live.com>
1 parent 41344f3 commit 34da224

3 files changed

Lines changed: 71 additions & 2 deletions

File tree

_packages/api/src/node.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,29 @@ export class RemoteNode extends RemoteNodeBase implements Node {
390390
return this.decoder.decode(text);
391391
}
392392

393+
private getChildAtNodeIndex(parent: number, index: number): RemoteNode | RemoteNodeList | undefined {
394+
let next = parent + 1;
395+
let count = 0;
396+
while (next !== 0) {
397+
let child = new RemoteNode(this.view, this.decoder, next, this);
398+
399+
if (child.parent.index !== parent) {
400+
return undefined;
401+
}
402+
403+
if (count === index) {
404+
if (child.kind == KIND_NODE_LIST) {
405+
return new RemoteNodeList(this.view, this.decoder, child.index, this);
406+
} else {
407+
return child;
408+
}
409+
}
410+
411+
count += 1;
412+
next = child.next;
413+
}
414+
}
415+
393416
private getOrCreateChildAtNodeIndex(index: number): RemoteNode | RemoteNodeList {
394417
const pos = this.view.getUint32(this.offsetNodes + index * NODE_LEN + NODE_OFFSET_POS, true);
395418
let child = (this._children ??= new Map()).get(pos);
@@ -487,7 +510,8 @@ export class RemoteNode extends RemoteNodeBase implements Node {
487510
// were present, we would have `parameters = children[5]`, but since `postfixToken` and `astersiskToken` are
488511
// missing, we have `parameters = children[5 - 2]`.
489512
const propertyIndex = order - popcount8[~(mask | ((0xff << order) & 0xff)) & 0xff];
490-
return this.getOrCreateChildAtNodeIndex(this.index + 1 + propertyIndex);
513+
return this.getChildAtNodeIndex(this.index, propertyIndex);
514+
// return this.getOrCreateChildAtNodeIndex(this.index + 1 + propertyIndex);
491515
}
492516

493517
__print(): string {

_packages/api/test/api.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ import {
1212
isTemplateHead,
1313
isTemplateMiddle,
1414
isTemplateTail,
15+
isFunctionDeclaration,
16+
isExpressionStatement,
17+
isCallExpression,
18+
isPropertyAccessExpression,
19+
isIdentifier,
20+
isStringLiteral,
1521
} from "@typescript/ast";
1622
import assert from "node:assert";
1723
import {
@@ -177,6 +183,45 @@ test("Dispose", () => {
177183
});
178184
});
179185

186+
test("Function", () => {
187+
const currentFiles = {
188+
"/tsconfig.json": "{}",
189+
"/src/index.ts": `function foo() {
190+
console.log("hello", "world")
191+
}`,
192+
};
193+
194+
let api = spawnAPI(currentFiles);
195+
const project = api.loadProject("/tsconfig.json");
196+
const sourceFile = project.getSourceFile("/src/index.ts");
197+
198+
let func = sourceFile?.statements[0]!;
199+
assert.ok(isFunctionDeclaration(func));
200+
201+
let body = func.body!;
202+
let expr = body.statements[0]!;
203+
assert.ok(isExpressionStatement(expr));
204+
let call_expr = expr.expression;
205+
assert.ok(isCallExpression(call_expr));
206+
207+
let callee = call_expr.expression;
208+
assert.ok(isPropertyAccessExpression(callee));
209+
let left = callee.expression;
210+
let right = callee.name;
211+
assert.ok(isIdentifier(left));
212+
assert.ok(isIdentifier(right));
213+
assert.equal(left.text, "console");
214+
assert.equal(right.text, "log");
215+
216+
let args = call_expr.arguments;
217+
let arg0 = args[0];
218+
let arg1 = args[1];
219+
assert.ok(isStringLiteral(arg0));
220+
assert.ok(isStringLiteral(arg1));
221+
assert.equal(arg0.text, "hello");
222+
assert.equal(arg1.text, "world");
223+
});
224+
180225
test("Benchmarks", async () => {
181226
await runBenchmarks(/*singleIteration*/ true);
182227
});

internal/api/encoder/encoder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func getChildrenPropertyMask(node *ast.Node) uint8 {
440440
return (boolToByte(n.DotDotDotToken != nil) << 0) | (boolToByte(n.PropertyName != nil) << 1) | (boolToByte(n.Name() != nil) << 2) | (boolToByte(n.Initializer != nil) << 3)
441441
case ast.KindFunctionDeclaration:
442442
n := node.AsFunctionDeclaration()
443-
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)
443+
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)
444444
case ast.KindInterfaceDeclaration:
445445
n := node.AsInterfaceDeclaration()
446446
return (boolToByte(n.Modifiers() != nil) << 0) | (boolToByte(n.Name() != nil) << 1) | (boolToByte(n.TypeParameters != nil) << 2) | (boolToByte(n.HeritageClauses != nil) << 3) | (boolToByte(n.Members != nil) << 4)

0 commit comments

Comments
 (0)