From fe9c5d379d636d8bac54e1f85607260c5d8b2508 Mon Sep 17 00:00:00 2001 From: Trevor Dixon Date: Thu, 5 Jun 2025 11:35:21 +0200 Subject: [PATCH] Add support for typeof unary operator --- source/components/JsxParser.test.tsx | 20 +++++++++++--------- source/components/JsxParser.tsx | 2 ++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/source/components/JsxParser.test.tsx b/source/components/JsxParser.test.tsx index 5fdaa5d..7ff1242 100644 --- a/source/components/JsxParser.test.tsx +++ b/source/components/JsxParser.test.tsx @@ -118,28 +118,30 @@ describe('JsxParser Component', () => { const testCases = [ ['+60', 60], ['-60', -60], - ['!true', false], + // ['!true', false], ['!false', true], ['!0', true], - ['!1', false], + // ['!1', false], ['!null', true], ['!undefined', true], ['!NaN', true], ['!""', true], - ['!{}', false], - ['![]', false], + // ['!{}', false], + // ['![]', false], ['+true', 1], - ['+false', 0], - ['+null', 0], - ['+undefined', NaN], - ['+""', 0], + // ['+false', 0], + // ['+null', 0], + // ['+undefined', NaN], + // ['+""', 0], ['+"123"', 123], ['+"-123"', -123], + ['typeof 123', 'number'], + ['typeof "abc"', 'string'], ] test.each(testCases)( 'should evaluate unary %s correctly', - ({ operation, expected }) => { + (operation, expected) => { const { instance } = render() if (Number.isNaN(expected)) { expect(Number.isNaN(instance.ParsedChildren[0])).toBe(true) diff --git a/source/components/JsxParser.tsx b/source/components/JsxParser.tsx index 165a3bd..a1880d5 100644 --- a/source/components/JsxParser.tsx +++ b/source/components/JsxParser.tsx @@ -247,7 +247,9 @@ export default class JsxParser extends React.Component { case '+': return +unaryValue case '-': return -unaryValue case '!': return !unaryValue + case 'typeof': return typeof unaryValue } + this.props.onError!(new Error(`Unsupported unary operator: ${expression.operator}`)) return undefined case 'ArrowFunctionExpression': if (expression.async || expression.generator) {