Skip to content
This repository was archived by the owner on Apr 10, 2023. It is now read-only.

Commit 4f792e6

Browse files
committed
feat(parse): ✨SequenceExpression
1 parent 0cfb85f commit 4f792e6

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,2
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "SequenceExpression",
8+
"expressions": [
9+
{
10+
"type": "Literal",
11+
"value": 1
12+
},
13+
{
14+
"type": "Literal",
15+
"value": 2
16+
}
17+
]
18+
}
19+
}
20+
]
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1,2===1
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "SequenceExpression",
8+
"expressions": [
9+
{
10+
"type": "Literal",
11+
"value": 1
12+
},
13+
{
14+
"type": "BinaryExpression",
15+
"operator": "===",
16+
"left": {
17+
"type": "Literal",
18+
"value": 2
19+
},
20+
"right": {
21+
"type": "Literal",
22+
"value": 1
23+
}
24+
}
25+
]
26+
}
27+
}
28+
]
29+
}

lib/parse.js

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,8 @@ module.exports = function parse(input) {
106106
/**
107107
* (a, b)
108108
*/
109-
let exprs = [];
110-
while (i < _endIndex) {
111-
const expr = parseExpressionStatement(i + 1, _endIndex).expression;
112-
exprs.push(expr);
113-
}
114-
consumeParenNodes(exprs);
109+
const expr = parseExpressionStatement(i + 1, _endIndex).expression;
110+
consumeParenNodes(expr);
115111
i = _endIndex + 1;
116112
continue;
117113
}
@@ -325,22 +321,30 @@ module.exports = function parse(input) {
325321
setPostfixUpdateOperator(expressionStatement, operator);
326322
operator = null;
327323
}
324+
325+
// support sequence expression
326+
if (input.charCodeAt(i - 1) === codes[',']) {
327+
// is sequence expression
328+
expressionStatement.expression = new astFactory.SequenceExpression([
329+
expressionStatement.expression,
330+
]);
331+
while (i < endIndex) {
332+
const expr = parseExpressionStatement(i, endIndex);
333+
expressionStatement.expression.expressions.push(expr.expression);
334+
}
335+
}
328336
}
329337

330-
function consumeParenNodes(nodes) {
338+
function consumeParenNodes(expr) {
331339
if (
332340
operator === null &&
333341
secondOperator === null &&
334342
expressionStatement !== null
335343
) {
336-
return consumeCallExpression(nodes);
337-
}
338-
if (nodes.length > 1) {
339-
throw new Error('???');
344+
return consumeCallExpression(expr);
340345
}
341-
const node = nodes[0];
342-
node.paren = true;
343-
return consumeNode(node);
346+
expr.paren = true;
347+
return consumeNode(expr);
344348
}
345349

346350
function consumeNode(node) {
@@ -377,7 +381,13 @@ module.exports = function parse(input) {
377381
expressionStatement.expression = expr;
378382
}
379383

380-
function consumeCallExpression(exprs) {
384+
function consumeCallExpression(expr) {
385+
let exprs = [];
386+
if (expr.type === astTypes.SEQUENCE_EXPRESSION) {
387+
exprs = expr.expressions;
388+
} else {
389+
exprs = [expr];
390+
}
381391
expressionStatement.expression = new astFactory.CallExpression(
382392
expressionStatement.expression,
383393
exprs
@@ -564,6 +574,17 @@ module.exports = function parse(input) {
564574
);
565575
consumeNode(arrayExpression);
566576
}
577+
578+
function consumeSequenceExpression(startIndex) {
579+
expressionStatement.expression = new astFactory.SequenceExpression([
580+
expressionStatement.expression,
581+
]);
582+
while (startIndex < input.length) {
583+
expressionStatement.expression.expressions.push(
584+
parseExpressionStatement(startIndex, input.length)
585+
);
586+
}
587+
}
567588
}
568589
};
569590

0 commit comments

Comments
 (0)