Skip to content

Commit 0d13eb0

Browse files
committed
Make literals
1 parent ced9628 commit 0d13eb0

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

parse.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const PERIOD=46, OPAREN=40, CPAREN=41, CBRACK=93, SPACE=32,
77
// current string & index
88
let idx, cur
99

10-
export const parse = (str, tree) => (cur=str, idx=0, tree=expr(), idx<cur.length ? err() : tree),
10+
export const parse = (str, tree) => (cur=str, idx=0, tree=expr(), idx<cur.length ? err() : tree.valueOf()),
1111

1212
err = (msg='Bad syntax') => { throw Error(msg + ' `' + cur[idx] + '` at ' + idx) },
1313

@@ -73,11 +73,11 @@ operator = (op, prec=0, type=0, map, c=op.charCodeAt(0), l=op.length, prev=looku
7373

7474
map = !type ? node => { // binary, consume same-op group
7575
node = [op, node || err()]
76-
do { idx+=l, node.push(expr(prec) || err()) } while (parse.space()==c && isop())
76+
do { idx+=l, node.push((expr(prec) || err()).valueOf()) } while (parse.space()==c && isop())
7777
return node
7878
} :
7979
type > 0 ? node => node && [skip(l), node] : // postfix unary
80-
type < 0 ? node => !node && [skip(l), expr(prec-1) || err()] : // prefix unary
80+
type < 0 ? node => !node && [skip(l), (expr(prec-1) || err()).valueOf()] : // prefix unary
8181
type,
8282

8383
lookup[c] = (node, curPrec) => curPrec < prec && isop() && map(node) || (prev && prev(node, curPrec))
@@ -128,15 +128,15 @@ for (let i = 0, ops = [
128128
'%', PREC_MULT,,
129129

130130
// a.b
131-
'.', PREC_CALL, (node,b) => node && [skip(),node, typeof (b = expr(PREC_CALL)) === 'string' ? '"' + b + '"' : b],
131+
'.', PREC_CALL, (node,b) => node && [skip(),node, typeof (b = expr(PREC_CALL)) === 'string' ? '"' + b + '"' : b.valueOf()],
132132

133133
// a[b]
134-
'[', PREC_CALL, (node) => (idx++, node = ['.', node, expr(0,CBRACK)], idx++, node),
134+
'[', PREC_CALL, (node) => (idx++, node = ['.', node, expr(0,CBRACK).valueOf()], idx++, node),
135135
']',,,
136136

137137
// a(b)
138138
'(', PREC_CALL, (node,b) => ( idx++, b=expr(0,CPAREN), idx++,
139-
Array.isArray(b) && b[0]===',' ? (b[0]=node, b) : b ? [node, b] : [node]
139+
Array.isArray(b) && b[0]===',' ? (b[0]=node, b) : b ? [node, b.valueOf()] : [node]
140140
),
141141
// (a+b)
142142
'(', PREC_GROUP, (node,b) => !node && (++idx, b=expr(0,CPAREN) || err(), ++idx, b),

subscript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ test('ext: literals', t=> {
118118
)
119119
is(parse('null'), null)
120120
is(parse('(null)'), null)
121+
is(parse('!null'), ['!',null])
122+
is(parse('false++'), ['++',false])
123+
is(parse('++false'), ['++',false])
124+
is(parse('(a)(null)'), ['a',null])
125+
is(parse('false&true'), ['&',false,true])
126+
is(parse('(false)||((null))'), ['||',false,null])
121127
// parse.literal['undefined'] = undefined
122128
is(parse('undefined'), undefined)
123129
is(parse('(undefined)'), undefined)

0 commit comments

Comments
 (0)