diff --git a/src/__tests__/__snapshots__/interpreter.test.js.snap b/src/__tests__/__snapshots__/interpreter.test.js.snap index 1858c5c0..64ae381b 100644 --- a/src/__tests__/__snapshots__/interpreter.test.js.snap +++ b/src/__tests__/__snapshots__/interpreter.test.js.snap @@ -170,6 +170,8 @@ exports[`interpreter correct target code {"foo": 3} | {"bar": "baz", ...$} 1`] = exports[`interpreter correct target code {"original": $.foo} | {"new": $} 1`] = `"(function(_) { return (function(input) { return (function (input) {return (_.objectify(Array.from([[\\"new\\",input]])))})((_.objectify(Array.from([[\\"original\\",input.foo]]))))})})"`; +exports[`interpreter correct target code {'foo': ('bar' | 'baz')} 1`] = `"(function(_) { return (function(input) { return (_.objectify(Array.from([['foo',((function (input) {return 'baz'})('bar'))]])))})})"`; + exports[`interpreter correct target code {each $: (length) in $} 1`] = `"(function(_) { return (function(input) { return (_.objectify(Array.from(((function (input) {return _.map((function(input) {return [input,(_.length(input))]}))(input)})(input)))))})})"`; exports[`interpreter correct target code -(3 * 2) -1 > -1.34 * 3 && true 1`] = `"(function(_) { return (function(input) { return (-((3*2)))-1>(-(1.34))*3&&true})})"`; @@ -5204,6 +5206,67 @@ Object { } `; +exports[`interpreter correct target tree {'foo': ('bar' | 'baz')} 1`] = ` +Object { + "status": true, + "value": Object { + "end": Object { + "column": 25, + "line": 1, + "offset": 24, + }, + "name": "object", + "start": Object { + "column": 1, + "line": 1, + "offset": 0, + }, + "value": Array [ + Object { + "end": Object { + "column": 24, + "line": 1, + "offset": 23, + }, + "name": "simpleList", + "start": Object { + "column": 2, + "line": 1, + "offset": 1, + }, + "value": Array [ + Object { + "name": "tuple", + "value": Array [ + Object { + "name": "primitive", + "value": "'foo'", + }, + Object { + "name": "parentheses", + "value": Object { + "name": "pipe", + "value": Object { + "left": Object { + "name": "primitive", + "value": "'bar'", + }, + "right": Object { + "name": "primitive", + "value": "'baz'", + }, + }, + }, + }, + ], + }, + ], + }, + ], + }, +} +`; + exports[`interpreter correct target tree {each $: (length) in $} 1`] = ` Object { "status": true, diff --git a/src/__tests__/interpreter.test.js b/src/__tests__/interpreter.test.js index 4f645f77..f5cd9f49 100644 --- a/src/__tests__/interpreter.test.js +++ b/src/__tests__/interpreter.test.js @@ -106,6 +106,12 @@ const tests = [ foo: 'baz' } }, + { + sourceCode: `{'foo': ('bar' | 'baz')}`, + output: { + foo: 'baz' + } + }, { sourceCode: `{"foo": ("bar" | "baz")}["foo"]`, output: 'baz' diff --git a/src/parsers/__tests__/primitives.test.js b/src/parsers/__tests__/primitives.test.js index 536fe425..d7a88966 100644 --- a/src/parsers/__tests__/primitives.test.js +++ b/src/parsers/__tests__/primitives.test.js @@ -17,7 +17,7 @@ const testExample = (parser: ParserType): (SourceCodeType => void) => ( }) } -const stringExamples = [`""`, `"foo"`, `"Fo bar ²¡ü"`] +const stringExamples = [`""`, `"foo"`, `"Fo ba'r' ²¡ü"`, `'asd "go" asd'`] const numberExamples = ['3.14', '42', '0', '111.111', '0.33'] const examples = [...stringExamples, ...numberExamples] diff --git a/src/parsers/primitive.js b/src/parsers/primitive.js index 8910859b..2e77b85e 100644 --- a/src/parsers/primitive.js +++ b/src/parsers/primitive.js @@ -6,7 +6,9 @@ import P from 'parsimmon' import type { NodeType } from '../types' const keywords = ['null', 'true', 'false'] -export const StringParserRegExp = /("(((?=\\)\\(["\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\0-\x1F\x7F]+)*")/ +const DoubleQuoteStringRegexp = /("(((?=\\)\\(["\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^"\\\0-\x1F\x7F]+)*")/ +const SingleQuoteStringRegexp = /('(((?=\\)\\(['\\\/bfnrt]|u[0-9a-fA-F]{4}))|[^'\\\0-\x1F\x7F]+)*')/ +export const StringParserRegExp = new RegExp(`(${DoubleQuoteStringRegexp.source}|${SingleQuoteStringRegexp.source})`) const NumberParserRegExp = /(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?/ const options = [