From 23f6c67199d633a0f2a7f2d71c16e5188123876e Mon Sep 17 00:00:00 2001 From: Rune Botten Date: Wed, 22 May 2024 10:46:18 -0700 Subject: [PATCH] fix(core): avoid attribute names being scanned as symbols Fixes #5673 Co-authored-by: Binoy Patel --- .../@sanity/mutator/src/jsonpath/tokenize.ts | 13 ++++++++ packages/@sanity/mutator/test/parse.test.ts | 4 +++ .../@sanity/mutator/test/tokenize.test.ts | 32 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/packages/@sanity/mutator/src/jsonpath/tokenize.ts b/packages/@sanity/mutator/src/jsonpath/tokenize.ts index e0fd66a94ae..2f8933a8dc9 100644 --- a/packages/@sanity/mutator/src/jsonpath/tokenize.ts +++ b/packages/@sanity/mutator/src/jsonpath/tokenize.ts @@ -113,6 +113,19 @@ class Tokenizer { return null } if (str === this.source.slice(this.i, this.i + str.length)) { + // When checking symbols that consist of valid attribute characters, we + // need to make sure we don't inadvertently treat an attribute as a + // symbol. For example, an attribute 'trueCustomerField' should not be + // scanned as the boolean symbol "true". + if (str[0].match(attributeCharMatcher)) { + // check that char following the symbol match is not also an attribute char + if (this.length > this.i + str.length) { + const nextChar = this.source[this.i + str.length] + if (nextChar && nextChar.match(attributeCharMatcher)) { + return null + } + } + } this.i += str.length return str } diff --git a/packages/@sanity/mutator/test/parse.test.ts b/packages/@sanity/mutator/test/parse.test.ts index c7e3cd92ba1..6a8a4f37d70 100644 --- a/packages/@sanity/mutator/test/parse.test.ts +++ b/packages/@sanity/mutator/test/parse.test.ts @@ -183,6 +183,10 @@ const cases = { }, ], }, + 'trueOrFalseField': { + type: 'attribute', + name: 'trueOrFalseField', + }, } Object.keys(cases).forEach((path) => { diff --git a/packages/@sanity/mutator/test/tokenize.test.ts b/packages/@sanity/mutator/test/tokenize.test.ts index a6d355d2f94..e0651bd8919 100644 --- a/packages/@sanity/mutator/test/tokenize.test.ts +++ b/packages/@sanity/mutator/test/tokenize.test.ts @@ -128,6 +128,38 @@ const cases = { type: 'paren', }, ], + 'trueish': [ + { + type: 'identifier', + name: 'trueish', + }, + ], + 'trueOrFalse[trueField == true]': [ + { + type: 'identifier', + name: 'trueOrFalse', + }, + { + type: 'paren', + symbol: '[', + }, + { + type: 'identifier', + name: 'trueField', + }, + { + type: 'comparator', + symbol: '==', + }, + { + symbol: 'true', + type: 'boolean', + }, + { + type: 'paren', + symbol: ']', + }, + ], } test('Tokenization of jsonpath', () => {