Skip to content

Commit

Permalink
fix: #247 throw an error in case of a missing object value
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed May 5, 2023
1 parent bae1836 commit c260715
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ export function parse(
eatColon()
const value = parseValue()

if (value === undefined) {
throwObjectValueExpected()
}

// TODO: test deep equal instead of strict equal
if (Object.prototype.hasOwnProperty.call(object, key) && !isDeepEqual(value, object[key])) {
// Note that we could also test `if(key in object) {...}`
Expand Down Expand Up @@ -291,6 +295,10 @@ export function parse(
throw new SyntaxError(`Invalid escape character '${chars}' ${pos()}`)
}

function throwObjectValueExpected() {
throw new SyntaxError(`Object value expected after ':' ${pos()}`)
}

function throwInvalidUnicodeCharacter(start: number) {
let end = start + 2
while (/\w/.test(text[end])) {
Expand Down
5 changes: 5 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ describe('throw meaningful exceptions', () => {
input: '{"a",',
expectedError: "Colon ':' expected after property name but got ',' at position 4"
},
{
input: '{"a":}',
expectedError: "Object value expected after ':' at position 5"
},
{ input: '{a:2}', expectedError: "Quoted object key expected but got 'a' at position 1" },
{ input: '{"a":2,}', expectedError: "Quoted object key expected but got '}' at position 7" },
{
Expand All @@ -361,6 +365,7 @@ describe('throw meaningful exceptions', () => {
"Array item or end of array ']' expected but reached end of input at position 1"
},
{ input: '[2,]', expectedError: "Array item expected but got ']' at position 3" },
{ input: '[2,,3]', expectedError: "Array item expected but got ',' at position 3" },
{ input: '[2 3]', expectedError: "Comma ',' expected after value but got '3' at position 3" },
{ input: '2.3.4', expectedError: "Expected end of input but got '.' at position 3" },
{
Expand Down

0 comments on commit c260715

Please sign in to comment.