Skip to content

Commit 80122ea

Browse files
authored
fix: apply unary operators after member access (#36)
1 parent ce1a926 commit 80122ea

6 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎.github/workflows/build.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Setup Node.js
2222
uses: actions/setup-node@v4
2323
with:
24-
node-version: "18"
24+
node-version: "22"
2525
registry: "https://registry.npmjs.org"
2626

2727
- name: Install dependencies

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"limit-size": "^0.1.4",
3333
"rollup": "^4.34.6",
3434
"tslib": "^2.8.1",
35+
"typescript": "^5.9.3",
3536
"vitest": "^3.0.8"
3637
},
3738
"limit-size": [

‎pnpm-workspace.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
allowBuilds:
2+
"@biomejs/biome": true
3+
esbuild: true

‎src/parser.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,11 @@ export const parse = (tokens: Token[]): Program => {
352352
(token.value === "!" || token.value === "-")
353353
) {
354354
consume(); // consume operator
355-
const argument = parsePrimary();
355+
let argument = parsePrimary();
356+
// Member access binds tighter than unary operators: !a.b is !(a.b)
357+
while (match(TokenType.DOT) || match(TokenType.BRACKET_LEFT)) {
358+
argument = parseMemberExpression(argument);
359+
}
356360
return {
357361
type: NodeType.UnaryExpression,
358362
operator: token.value,

‎tests/api-integration.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ describe("API Integration Tests", () => {
1818
expect(evaluate("x / y", context)).toBe(2);
1919
});
2020

21+
it("should apply unary operators to member access results", () => {
22+
const context = { d: { active: false, value: 120 }, arr: [0, 1] };
23+
expect(evaluate("!d.active", context)).toBe(true);
24+
expect(evaluate('!d.active ? "grey" : "red"', context)).toBe("grey");
25+
expect(evaluate("!arr[0]", context)).toBe(true);
26+
expect(evaluate("!!arr[1]", context)).toBe(true);
27+
expect(evaluate("-d.value", context)).toBe(-120);
28+
expect(evaluate("-d.value + 20", context)).toBe(-100);
29+
expect(evaluate("-(d).value", context)).toBe(-120);
30+
});
31+
2132
it("should throw ExpressionError for invalid expressions", () => {
2233
expect(() => evaluate("")).toThrow(ExpressionError);
2334
expect(() => evaluate("x + y")).toThrow(ExpressionError);

‎tests/parser.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ describe("Parser", () => {
230230
},
231231
});
232232
});
233+
234+
it("should apply unary operators after member access", () => {
235+
const ast = parseExpression("!a.b");
236+
expect(ast).toEqual({
237+
type: NodeType.Program,
238+
body: {
239+
type: NodeType.UnaryExpression,
240+
operator: "!",
241+
argument: {
242+
type: NodeType.MemberExpression,
243+
object: {
244+
type: NodeType.Identifier,
245+
name: "a",
246+
},
247+
property: {
248+
type: NodeType.Identifier,
249+
name: "b",
250+
},
251+
computed: false,
252+
},
253+
prefix: true,
254+
},
255+
});
256+
});
233257
});
234258

235259
describe("Conditional Expressions", () => {

0 commit comments

Comments
 (0)