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

Commit d5e6d89

Browse files
committed
fix(MemberExpression to LogicalExpression): 🐛
1 parent 8321a1f commit d5e6d89

File tree

3 files changed

+60
-41
lines changed

3 files changed

+60
-41
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
item && item.src
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"type": "Program",
3+
"body": [
4+
{
5+
"type": "ExpressionStatement",
6+
"expression": {
7+
"type": "LogicalExpression",
8+
"operator": "&&",
9+
"left": {
10+
"type": "Identifier",
11+
"name": "item"
12+
},
13+
"right": {
14+
"type": "MemberExpression",
15+
"object": {
16+
"type": "Identifier",
17+
"name": "item"
18+
},
19+
"property": {
20+
"type": "Identifier",
21+
"name": "src"
22+
},
23+
"computed": false
24+
}
25+
}
26+
}
27+
]
28+
}

lib/parse.js

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,37 @@ module.exports = function parse(input) {
447447
}
448448
}
449449

450+
function consumeMemberExpression(expression, computed) {
451+
const parent = findBottomRightParentNode(
452+
expressionStatement,
453+
shouldBreak
454+
);
455+
456+
switch (parent.type) {
457+
case astTypes.EXPRESSION_STATEMENT:
458+
parent.expression = new astFactory.MemberExpression(
459+
parent.expression,
460+
expression,
461+
computed
462+
);
463+
break;
464+
case astTypes.BINARY_EXPRESSION:
465+
case astTypes.LOGICAL_EXPRESSION:
466+
parent.right = new astFactory.MemberExpression(
467+
parent.right,
468+
expression,
469+
computed
470+
);
471+
break;
472+
default:
473+
throwError('Unexpected parent.type: ' + parent.type);
474+
}
475+
476+
function shouldBreak(ast) {
477+
return ast.paren || ast.type === astTypes.MEMBER_EXPRESSION;
478+
}
479+
}
480+
450481
function consumeSecondOperator(node) {
451482
if (secondOperator) {
452483
if (astFactory.isUnaryExpressionOperator(secondOperator)) {
@@ -461,47 +492,6 @@ module.exports = function parse(input) {
461492
return node;
462493
}
463494

464-
function consumeMemberExpression(expression, computed) {
465-
setMemberExpression(expressionStatement);
466-
467-
function setMemberExpression(node) {
468-
switch (node.type) {
469-
case astTypes.EXPRESSION_STATEMENT:
470-
if (
471-
node.expression.type === astTypes.LITERAL ||
472-
node.expression.type === astTypes.IDENTIFIER ||
473-
node.expression.type === astTypes.MEMBER_EXPRESSION
474-
) {
475-
node.expression = new astFactory.MemberExpression(
476-
node.expression,
477-
expression,
478-
computed
479-
);
480-
} else {
481-
setMemberExpression(node.expression);
482-
}
483-
break;
484-
case astTypes.BINARY_EXPRESSION:
485-
if (
486-
node.right.type === astTypes.LITERAL ||
487-
node.right.type === astTypes.IDENTIFIER ||
488-
node.right.type === astTypes.MEMBER_EXPRESSION
489-
) {
490-
node.right = new astFactory.MemberExpression(
491-
node.right,
492-
expression,
493-
computed
494-
);
495-
} else {
496-
setMemberExpression(node.right);
497-
}
498-
break;
499-
default:
500-
throwError('Unexpected node type: ' + node.type);
501-
}
502-
}
503-
}
504-
505495
function getIdentifier() {
506496
const start = i;
507497
let code;

0 commit comments

Comments
 (0)