Skip to content

Commit 0ca216e

Browse files
authored
fix: compileLHS for subscript and attribute (#311)
1 parent 2948541 commit 0ca216e

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

ui/src/lib/parser.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,16 +427,20 @@ function compileLHS(node, st) {
427427
case "identifier":
428428
return new Set([node.text]);
429429
case "pattern_list":
430+
// [a,b,c] = [1,2,3]
430431
return new Set(node.namedChildren.filter(notComment).map((n) => n.text));
431432
case "tuple_pattern":
433+
// (a,b,c) = (1,2,3)
432434
return new Set(node.namedChildren.filter(notComment).map((n) => n.text));
433435
case "subscript":
436+
// a[1] = 2
434437
let [l, r] = node.namedChildren.filter(notComment);
435438
compileExpression(r, st);
436-
return compileLHS(l, st);
439+
return compileExpression(l, st);
437440
case "attribute":
441+
// a.b = 2
438442
let [obj, attr] = node.namedChildren.filter(notComment);
439-
return compileLHS(obj, st);
443+
return compileExpression(obj, st);
440444
default:
441445
global_errors.push({
442446
message: `unknown LHS type: ${node.type} in ${mysubstr(node.text)}`,

ui/src/tests/parser.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,45 @@ describe("sum module", () => {
4646
expect(errors).toStrictEqual([]);
4747
});
4848
});
49+
50+
test("parse subscript LHS", async () => {
51+
let { annotations, errors } = analyzeCode(`
52+
x = [1,2,3]
53+
x[0] = 1
54+
`);
55+
expect(annotations).toStrictEqual([
56+
{
57+
name: "x",
58+
type: "vardef",
59+
startIndex: 8,
60+
endIndex: 9,
61+
startPosition: { row: 1, column: 7 },
62+
endPosition: { row: 1, column: 8 },
63+
},
64+
{
65+
name: "x",
66+
type: "varuse",
67+
startIndex: 27,
68+
endIndex: 28,
69+
startPosition: { row: 2, column: 7 },
70+
endPosition: { row: 2, column: 8 },
71+
},
72+
]);
73+
});
74+
75+
test("parse attribute LHS", async () => {
76+
let { annotations } = analyzeCode(`
77+
a.b = 3
78+
`);
79+
expect(annotations).toStrictEqual([
80+
{
81+
name: "a",
82+
type: "varuse",
83+
startIndex: 5,
84+
endIndex: 6,
85+
startPosition: { row: 1, column: 4 },
86+
endPosition: { row: 1, column: 5 },
87+
},
88+
]);
89+
});
4990
});

0 commit comments

Comments
 (0)