diff --git a/README.md b/README.md index 6b66b64..46f9054 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# @antv/expr +# @antv/expr ![gzip size](https://img.badgesize.io/https://unpkg.com/@antv/expr/dist/index.esm.js?compression=gzip) Have you ever wanted to make your SSR charts more dynamic but worried about security risks? @@ -9,7 +9,7 @@ We've got you covered! Our solution introduces an easy-to-use template syntax th - 🔒 **Secure by default** - No access to global objects or prototype chain, does not use `eval` or `new Function`. - 🚀 **High performance** - Supports pre-compilation of expressions for improved performance with repeated evaluations. - đŸ› ī¸ **Extensible** - Register custom functions to easily extend functionality. -- đŸĒŠ **Lightweight** - Zero dependencies, small footprint, only `7.8 Kb` before gzip. +- đŸĒŠ **Lightweight** - Zero dependencies, small footprint, before gzip it was less than `8 Kb`. ## đŸ“Ĩ Installation diff --git a/package.json b/package.json index d1e5288..ecb919e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/expr", - "version": "1.0.0", + "version": "1.0.1", "description": "A secure, high-performance expression evaluator for dynamic chart rendering", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", @@ -10,7 +10,7 @@ "build": "rollup -c", "test": "vitest run --coverage", "benchmark": "vitest bench", - "prepublishOnly": "npm run build" + "prepublishOnly": "pnpm run test && pnpm run build" }, "keywords": [ "expression", diff --git a/src/index.ts b/src/index.ts index 2ee88f7..0c111c2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,16 +48,13 @@ function compile( // biome-ignore lint/suspicious/noExplicitAny: Return type depends on the expression return (context: Context = {}): any => { try { - // Execute the evaluation with timeout protection return evaluateAst(ast, interpreterState, context); } catch (error) { if (error instanceof ExpressionError) { throw error; } - if (error instanceof Error) { - throw new ExpressionError(error.message); - } + // Pass through other errors without wrapping them throw error; } }; diff --git a/src/interpreter.ts b/src/interpreter.ts index ab26f50..57ff58a 100644 --- a/src/interpreter.ts +++ b/src/interpreter.ts @@ -141,10 +141,9 @@ export const evaluateAst = ( switch (node.operator) { case "+": - if (typeof left === "number" && typeof right === "number") { - return left + right; - } - return String(left) + String(right); + // For addition, handle both numeric addition and string concatenation + // biome-ignore lint/suspicious/noExplicitAny: + return (left as any) + (right as any); case "-": return (left as number) - (right as number); case "*": @@ -224,32 +223,25 @@ export const evaluateAst = ( * @returns The result of evaluation */ const evaluateNode = (node: Expression): unknown => { - try { - switch (node.type) { - case NodeType.Literal: - return evaluateLiteral(node); - case NodeType.Identifier: - return evaluateIdentifier(node); - case NodeType.MemberExpression: - return evaluateMemberExpression(node); - case NodeType.CallExpression: - return evaluateCallExpression(node); - case NodeType.BinaryExpression: - return evaluateBinaryExpression(node); - case NodeType.UnaryExpression: - return evaluateUnaryExpression(node); - case NodeType.ConditionalExpression: - return evaluateConditionalExpression(node); - default: - throw new ExpressionError( - `Unsupported node type: ${(node as Expression).type}`, - ); - } - } catch (error) { - if (error instanceof ExpressionError) { - throw new ExpressionError(`Evaluation error: ${error.message}`); - } - throw error; + switch (node.type) { + case NodeType.Literal: + return evaluateLiteral(node); + case NodeType.Identifier: + return evaluateIdentifier(node); + case NodeType.MemberExpression: + return evaluateMemberExpression(node); + case NodeType.CallExpression: + return evaluateCallExpression(node); + case NodeType.BinaryExpression: + return evaluateBinaryExpression(node); + case NodeType.UnaryExpression: + return evaluateUnaryExpression(node); + case NodeType.ConditionalExpression: + return evaluateConditionalExpression(node); + default: + throw new ExpressionError( + `Evaluation error: Unsupported node type: ${(node as Expression).type}`, + ); } }; diff --git a/tests/coverage-improvement.test.ts b/tests/coverage-improvement.test.ts index 1bfacff..89afa3c 100644 --- a/tests/coverage-improvement.test.ts +++ b/tests/coverage-improvement.test.ts @@ -151,7 +151,7 @@ describe("Coverage Improvement Tests", () => { }; expect(() => evaluateAst(ast, interpreterState, {})).toThrow( - "Evaluation error: Postfix operators are not supported: ~", + "Postfix operators are not supported: ~", ); }); });