Skip to content

Commit

Permalink
fix(core): avoid attribute names being scanned as symbols
Browse files Browse the repository at this point in the history
Fixes #5673

Co-authored-by: Binoy Patel <me@binoy.io>
  • Loading branch information
runeb and binoy14 committed May 22, 2024
1 parent dadfcfa commit 23f6c67
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/@sanity/mutator/src/jsonpath/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions packages/@sanity/mutator/test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ const cases = {
},
],
},
'trueOrFalseField': {
type: 'attribute',
name: 'trueOrFalseField',
},
}

Object.keys(cases).forEach((path) => {
Expand Down
32 changes: 32 additions & 0 deletions packages/@sanity/mutator/test/tokenize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 23f6c67

Please sign in to comment.