Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery
while (true) {
skipWhitespace()

if (query[i] === '.' && 'pipe' in currentOperators) {
// an implicitly piped property like "fn().prop"
const right = parseProperty()
left = left[0] === 'pipe' ? [...left, right] : ['pipe', left, right]
continue
}

const start = i
const name = parseOperatorName(currentOperators)
if (!name) {
Expand Down Expand Up @@ -115,6 +122,8 @@ export function parse(query: string, options?: JSONQueryParseOptions): JSONQuery
parseInteger() ??
throwSyntaxError('Property expected')
)

skipWhitespace()
}

return ['get', ...props]
Expand Down
47 changes: 39 additions & 8 deletions test-suite/parse.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@
{ "input": ".array.2", "output": ["get", "array", 2] }
]
},
{
"category": "property",
"description": "should parse an implicitly piped a property",
"tests": [
{
"input": "groupBy(.city).myCity",
"output": ["pipe", ["groupBy", ["get", "city"]], ["get", "myCity"]]
},
{
"input": "groupBy(.city).myCity | size()",
"output": ["pipe", ["groupBy", ["get", "city"]], ["get", "myCity"], ["size"]]
},
{
"input": "groupBy(.city).myCity.location | size()",
"output": ["pipe", ["groupBy", ["get", "city"]], ["get", "myCity", "location"], ["size"]]
},
{
"input": ".data | groupBy(.city).myCity",
"output": ["pipe", ["get", "data"], ["groupBy", ["get", "city"]], ["get", "myCity"]]
},
{
"input": "\"hello\".length",
"output": ["pipe", "hello", ["get", "length"]]
}
]
},
{
"category": "property",
"description": "should allow whitespace between multiple properties",
"tests": [
{ "input": ".\"address\" .\"city\"", "output": ["get", "address", "city"] },
{ "input": ".address .city", "output": ["get", "address", "city"] },
{ "input": ".address\t.city", "output": ["get", "address", "city"] },
{ "input": ".address\n.city", "output": ["get", "address", "city"] }
]
},
{
"category": "property",
"description": "should throw an error when a property misses an end quote",
Expand All @@ -54,11 +90,7 @@
{
"category": "property",
"description": "should throw an error when there is whitespace between the dot and the property name",
"tests": [
{ "input": ". \"name\"", "throws": "Property expected (pos: 1)" },
{ "input": ".\"address\" .\"city\"", "throws": "Unexpected part '.\"city\"' (pos: 11)" },
{ "input": ".address .city", "throws": "Unexpected part '.city' (pos: 9)" }
]
"tests": [{ "input": ". \"name\"", "throws": "Property expected (pos: 1)" }]
},
{
"category": "function",
Expand Down Expand Up @@ -453,11 +485,10 @@
"description": "should throw an error in case of an invalid number",
"tests": [
{ "input": "-", "throws": "Value expected (pos: 0)" },
{ "input": "2.", "throws": "Unexpected part '.' (pos: 1)" },
{ "input": "2.", "throws": "Property expected (pos: 2)" },
{ "input": "2.3e", "throws": "Unexpected part 'e' (pos: 3)" },
{ "input": "2.3e+", "throws": "Unexpected part 'e+' (pos: 3)" },
{ "input": "2.3e-", "throws": "Unexpected part 'e-' (pos: 3)" },
{ "input": "2.", "throws": "Unexpected part '.' (pos: 1)" }
{ "input": "2.3e-", "throws": "Unexpected part 'e-' (pos: 3)" }
]
},
{
Expand Down