Skip to content

Commit

Permalink
feat: support single-quote strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kantord committed Mar 11, 2019
1 parent 4a56b83 commit 8bca19a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
63 changes: 63 additions & 0 deletions src/__tests__/__snapshots__/interpreter.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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})})"`;
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/interpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ const tests = [
foo: 'baz'
}
},
{
sourceCode: `{'foo': ('bar' | 'baz')}`,
output: {
foo: 'baz'
}
},
{
sourceCode: `{"foo": ("bar" | "baz")}["foo"]`,
output: 'baz'
Expand Down
2 changes: 1 addition & 1 deletion src/parsers/__tests__/primitives.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
4 changes: 3 additions & 1 deletion src/parsers/primitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down

0 comments on commit 8bca19a

Please sign in to comment.