From 7fd886f0ca08f3f0a726dcf2060592c0275c6255 Mon Sep 17 00:00:00 2001 From: Roy Van Liew Date: Thu, 9 Jun 2022 16:13:20 -0400 Subject: [PATCH 1/2] fix(Lexer): Interpret minus correctly --- lib/Lexer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/Lexer.js b/lib/Lexer.js index bbed63e2..8b876476 100644 --- a/lib/Lexer.js +++ b/lib/Lexer.js @@ -29,7 +29,8 @@ const minusNegatesAfter = [ 'openParen', 'openBracket', 'question', - 'colon' + 'colon', + 'comma' ] /** From bb5e1d3bdf9fc709b50c30cb0b01a90f64b4b69f Mon Sep 17 00:00:00 2001 From: Roy Van Liew Date: Fri, 10 Jun 2022 11:25:59 -0400 Subject: [PATCH 2/2] Add unit tests in Lexer for minus interpretation --- __tests__/lib/Lexer.test.js | 41 +++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/__tests__/lib/Lexer.test.js b/__tests__/lib/Lexer.test.js index e72af200..bdde41b0 100644 --- a/__tests__/lib/Lexer.test.js +++ b/__tests__/lib/Lexer.test.js @@ -181,13 +181,38 @@ describe('Lexer', () => { { type: 'closeBracket', value: ']', raw: ']' } ]) }) - it('considers minus to be negative appropriately', () => { - expect(inst.tokenize('-1?-2:-3')).toEqual([ - { type: 'literal', value: -1, raw: '-1' }, - { type: 'question', value: '?', raw: '?' }, - { type: 'literal', value: -2, raw: '-2' }, - { type: 'colon', value: ':', raw: ':' }, - { type: 'literal', value: -3, raw: '-3' } - ]) + describe('minus interpretation', () => { + it('considers minus to be negative appropriately', () => { + expect(inst.tokenize('-1?-2:-3')).toEqual([ + { type: 'literal', value: -1, raw: '-1' }, + { type: 'question', value: '?', raw: '?' }, + { type: 'literal', value: -2, raw: '-2' }, + { type: 'colon', value: ':', raw: ':' }, + { type: 'literal', value: -3, raw: '-3' } + ]) + }) + it('can interpret minus in an array', () => { + expect(inst.tokenize('[-1, -2, 3]')).toEqual([ + { type: 'openBracket', value: '[', raw: '[' }, + { type: 'literal', value: -1, raw: '-1' }, + { type: 'comma', value: ',', raw: ', ' }, + { type: 'literal', value: -2, raw: '-2' }, + { type: 'comma', value: ',', raw: ', ' }, + { type: 'literal', value: 3, raw: '3' }, + { type: 'closeBracket', value: ']', raw: ']' } + ]) + }) + it('can interpret minus in a method call', () => { + expect(inst.tokenize('min(1, -2, 3)')).toEqual([ + { type: 'identifier', value: 'min', raw: 'min' }, + { type: 'openParen', value: '(', raw: '(' }, + { type: 'literal', value: 1, raw: '1' }, + { type: 'comma', value: ',', raw: ', ' }, + { type: 'literal', value: -2, raw: '-2' }, + { type: 'comma', value: ',', raw: ', ' }, + { type: 'literal', value: 3, raw: '3' }, + { type: 'closeParen', value: ')', raw: ')' } + ]) + }) }) })