Skip to content

Commit d2591b7

Browse files
authored
chore: remove redundant code & v1.0.1 (#27)
1 parent aec1501 commit d2591b7

File tree

5 files changed

+28
-39
lines changed

5 files changed

+28
-39
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# @antv/expr
1+
# @antv/expr ![gzip size](https://img.badgesize.io/https://unpkg.com/@antv/expr/dist/index.esm.js?compression=gzip)
22

33
Have you ever wanted to make your SSR charts more dynamic but worried about security risks?
44

@@ -9,7 +9,7 @@ We've got you covered! Our solution introduces an easy-to-use template syntax th
99
- 🔒 **Secure by default** - No access to global objects or prototype chain, does not use `eval` or `new Function`.
1010
- 🚀 **High performance** - Supports pre-compilation of expressions for improved performance with repeated evaluations.
1111
- 🛠️ **Extensible** - Register custom functions to easily extend functionality.
12-
- 🪩 **Lightweight** - Zero dependencies, small footprint, only `7.8 Kb` before gzip.
12+
- 🪩 **Lightweight** - Zero dependencies, small footprint, before gzip it was less than `8 Kb`.
1313

1414

1515
## 📥 Installation

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/expr",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "A secure, high-performance expression evaluator for dynamic chart rendering",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",
@@ -10,7 +10,7 @@
1010
"build": "rollup -c",
1111
"test": "vitest run --coverage",
1212
"benchmark": "vitest bench",
13-
"prepublishOnly": "npm run build"
13+
"prepublishOnly": "pnpm run test && pnpm run build"
1414
},
1515
"keywords": [
1616
"expression",

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,13 @@ function compile(
4848
// biome-ignore lint/suspicious/noExplicitAny: Return type depends on the expression
4949
return (context: Context = {}): any => {
5050
try {
51-
// Execute the evaluation with timeout protection
5251
return evaluateAst(ast, interpreterState, context);
5352
} catch (error) {
5453
if (error instanceof ExpressionError) {
5554
throw error;
5655
}
5756

58-
if (error instanceof Error) {
59-
throw new ExpressionError(error.message);
60-
}
57+
// Pass through other errors without wrapping them
6158
throw error;
6259
}
6360
};

src/interpreter.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,9 @@ export const evaluateAst = (
141141

142142
switch (node.operator) {
143143
case "+":
144-
if (typeof left === "number" && typeof right === "number") {
145-
return left + right;
146-
}
147-
return String(left) + String(right);
144+
// For addition, handle both numeric addition and string concatenation
145+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
146+
return (left as any) + (right as any);
148147
case "-":
149148
return (left as number) - (right as number);
150149
case "*":
@@ -224,32 +223,25 @@ export const evaluateAst = (
224223
* @returns The result of evaluation
225224
*/
226225
const evaluateNode = (node: Expression): unknown => {
227-
try {
228-
switch (node.type) {
229-
case NodeType.Literal:
230-
return evaluateLiteral(node);
231-
case NodeType.Identifier:
232-
return evaluateIdentifier(node);
233-
case NodeType.MemberExpression:
234-
return evaluateMemberExpression(node);
235-
case NodeType.CallExpression:
236-
return evaluateCallExpression(node);
237-
case NodeType.BinaryExpression:
238-
return evaluateBinaryExpression(node);
239-
case NodeType.UnaryExpression:
240-
return evaluateUnaryExpression(node);
241-
case NodeType.ConditionalExpression:
242-
return evaluateConditionalExpression(node);
243-
default:
244-
throw new ExpressionError(
245-
`Unsupported node type: ${(node as Expression).type}`,
246-
);
247-
}
248-
} catch (error) {
249-
if (error instanceof ExpressionError) {
250-
throw new ExpressionError(`Evaluation error: ${error.message}`);
251-
}
252-
throw error;
226+
switch (node.type) {
227+
case NodeType.Literal:
228+
return evaluateLiteral(node);
229+
case NodeType.Identifier:
230+
return evaluateIdentifier(node);
231+
case NodeType.MemberExpression:
232+
return evaluateMemberExpression(node);
233+
case NodeType.CallExpression:
234+
return evaluateCallExpression(node);
235+
case NodeType.BinaryExpression:
236+
return evaluateBinaryExpression(node);
237+
case NodeType.UnaryExpression:
238+
return evaluateUnaryExpression(node);
239+
case NodeType.ConditionalExpression:
240+
return evaluateConditionalExpression(node);
241+
default:
242+
throw new ExpressionError(
243+
`Evaluation error: Unsupported node type: ${(node as Expression).type}`,
244+
);
253245
}
254246
};
255247

tests/coverage-improvement.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ describe("Coverage Improvement Tests", () => {
151151
};
152152

153153
expect(() => evaluateAst(ast, interpreterState, {})).toThrow(
154-
"Evaluation error: Postfix operators are not supported: ~",
154+
"Postfix operators are not supported: ~",
155155
);
156156
});
157157
});

0 commit comments

Comments
 (0)