From 3214061815c2fb396e43f40ccb719ad8ce7a5d86 Mon Sep 17 00:00:00 2001 From: Fabio Grande Date: Tue, 18 Jun 2024 17:18:25 +0200 Subject: [PATCH] Improved solution --- packages/bruno-js/src/utils.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/bruno-js/src/utils.js b/packages/bruno-js/src/utils.js index 791637134..397a82f29 100644 --- a/packages/bruno-js/src/utils.js +++ b/packages/bruno-js/src/utils.js @@ -1,6 +1,8 @@ const jsonQuery = require('json-query'); const { get } = require('@usebruno/query'); +const _jsInvalidChars = /\-/; + const JS_KEYWORDS = ` break case catch class const continue debugger default delete do else export extends false finally for function if import in instanceof @@ -45,12 +47,12 @@ const compileJsExpression = (expr) => { globals: globals.map((name) => ` ${name} = ${name} ?? globalThis.${name};`).join('') }; - // If expr contains an hyphen and has dotted identifiers, we need to adjust fieldnames - // to use square bracket access to the property - if (expr.indexOf('-') > 0 && expr.indexOf('.') > 0) { + // If expr contains an hyphen (or other invalid chars) and has dotted identifiers, we need to adjust fieldnames + // to use square bracket access to the property (if not already used) + if (_jsInvalidChars.test(expr) > 0 && expr.indexOf('.') > 0) { let _expr = ''; expr.split('.').forEach((_part, index) => { - if (_part.indexOf('-') > 0) { + if (_jsInvalidChars.test(_part) > 0 && _part.indexOf('[') < 0) { _expr += `['${_part}']`; } else { _expr += (index > 0 ? '.' : '') + `${_part}`;