Skip to content

Commit

Permalink
added support for implicit multiline objects (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
huboneo authored Jan 10, 2020
1 parent fa3459c commit bed0431
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ let ParserRules = [
{"name": "explicitReturn", "symbols": ["L_CURLY", "multiLine", "R_CURLY"], "postprocess": ([,statement]) => ({statement, returnValues: []})},
{"name": "explicitReturn", "symbols": ["L_CURLY", "singleLine", "R_CURLY"], "postprocess": ([,statement]) => ({statement, returnValues: []})},
{"name": "implicitReturn", "symbols": ["singleLine"], "postprocess": ([statement], _, reject) => statement.trim().startsWith('{') ? reject : ({returnValues: []})},
{"name": "implicitReturn", "symbols": ["L_PAREN", "L_CURLY", "multiLine", "R_CURLY", "R_PAREN"], "postprocess": () => ({returnValues: []})},
{"name": "returnValues", "symbols": ["returnValue", "COMMA", "returnValues"], "postprocess": ([hit,, rest]) => [hit, ...rest]},
{"name": "returnValues", "symbols": ["returnValue"]},
{"name": "returnValue", "symbols": ["value", "AS", "token"], "postprocess": ([original,, name]) => ({...original, alias: name.value})},
Expand Down
1 change: 1 addition & 0 deletions src/grammar.ne
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ explicitReturn -> L_CURLY singleLine multiLine R_CURLY {% ([,statement1, stateme

# ...
implicitReturn -> singleLine {% ([statement], _, reject) => statement.trim().startsWith('{') ? reject : ({returnValues: []}) %}
| L_PAREN L_CURLY multiLine R_CURLY R_PAREN {% () => ({returnValues: []}) %}

# RETURN hi AS foo, rand() AS bar
returnValues -> returnValue COMMA returnValues {% ([hit,, rest]) => [hit, ...rest] %}
Expand Down
39 changes: 39 additions & 0 deletions src/grammar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,45 @@ describe('lambda-parser', () => {
}
])
});

test('Supports returning objects wrapped in parens', () => {
const query = `x => ({foo: 1})`;

expect(parseLambda(query)).toEqual([
{
type: 'lambda',
variant: 'implicit',
parameters: {
type: 'token',
value: 'x'
},
body: {
returnValues: []
}
}
])
});

test('Supports returning objects wrapped in parens even with new lines', () => {
const query = `x => ({
foo: 1
})`;

expect(parseLambda(query)).toEqual([
{
type: 'lambda',
variant: 'implicit',
parameters: {
type: 'token',
value: 'x'
},
body: {
returnValues: []
}
},
expect.anything()
])
});
});

describe('explicit returns', () => {
Expand Down

0 comments on commit bed0431

Please sign in to comment.