Skip to content

Commit 5a132f0

Browse files
committed
fix: preserve brackets around var
Fix #113
1 parent ccf0d0c commit 5a132f0

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

src/lib/reducer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ function collectAddSubItems(preOperator, node, collected, precision) {
100100
collected.push({node: reducedNode, preOperator});
101101
}
102102
}
103+
} else if (node.type === 'ParenthesizedExpression') {
104+
collectAddSubItems(preOperator, node.content, collected, precision);
103105
} else {
104106
collected.push({node, preOperator});
105107
}
@@ -286,6 +288,7 @@ function convertNodesUnits(left, right, precision) {
286288
/**
287289
* @param {import('../parser').CalcNode} node
288290
* @param {number} precision
291+
* @return {import('../parser').CalcNode}
289292
*/
290293
function reduce(node, precision) {
291294
if (node.type === "MathExpression") {
@@ -305,6 +308,12 @@ function reduce(node, precision) {
305308
return node;
306309
}
307310

311+
if (node.type === 'ParenthesizedExpression') {
312+
if (node.content.type !== 'Function') {
313+
return reduce(node.content, precision);
314+
}
315+
}
316+
308317
return node;
309318
}
310319

src/lib/stringifier.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ function round(value, prec) {
2020
/**
2121
* @param {number | false} prec
2222
* @param {import('../parser').CalcNode} node
23+
*
24+
* @return {string}
2325
*/
2426
function stringify(node, prec) {
2527
switch (node.type) {
@@ -46,6 +48,8 @@ function stringify(node, prec) {
4648
return round(node.value, prec).toString();
4749
case 'Function':
4850
return node.value.toString();
51+
case 'ParenthesizedExpression':
52+
return `(${stringify(node.content, prec)})`;
4953
default:
5054
return round(node.value, prec) + node.unit;
5155
}

src/parser.d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ export interface MathExpression {
55
operator: '*' | '+' | '-' | '/';
66
}
77

8+
export interface ParenthesizedExpression {
9+
type: 'ParenthesizedExpression';
10+
content: CalcNode;
11+
}
12+
813
export interface DimensionExpression {
914
type:
1015
| 'LengthValue'
@@ -37,7 +42,7 @@ export interface FunctionExpression {
3742

3843
export type ValueExpression = DimensionExpression | NumberExpression;
3944

40-
export type CalcNode = MathExpression | ValueExpression | FunctionExpression;
45+
export type CalcNode = MathExpression | ValueExpression | FunctionExpression | ParenthesizedExpression;
4146

4247
export interface Parser {
4348
parse: (arg: string) => CalcNode;

src/parser.jison

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ expression
7474
| math_expression SUB math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
7575
| math_expression MUL math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
7676
| math_expression DIV math_expression { $$ = { type: 'MathExpression', operator: $2, left: $1, right: $3 }; }
77-
| LPAREN math_expression RPAREN { $$ = $2; }
77+
| LPAREN math_expression RPAREN { $$ = { type: 'ParenthesizedExpression', content: $2 }; }
7878
| function { $$ = $1; }
7979
| dimension { $$ = $1; }
8080
| number { $$ = $1; }

types/lib/reducer.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ export type Collectible = {
66
/**
77
* @param {import('../parser').CalcNode} node
88
* @param {number} precision
9+
* @return {import('../parser').CalcNode}
910
*/
10-
declare function reduce(node: import('../parser').CalcNode, precision: number): import("../parser").MathExpression | import("../parser").DimensionExpression | import("../parser").NumberExpression | import("../parser").FunctionExpression;
11+
declare function reduce(node: import('../parser').CalcNode, precision: number): import('../parser').CalcNode;

0 commit comments

Comments
 (0)