Skip to content

Commit

Permalink
feat: assignment right-to-left and precedence with ternary
Browse files Browse the repository at this point in the history
assignment now sets right-to-left associativity (new jsep feature)
ternary now checks for higher precedence so that it can fix it's `test` node accordingly. Assignment plugin then has to handle other node types (like ternary) to change from BinaryExpression to AssignmentExpression, so it recurses through all object property values (not just left and right).

fixes #189
  • Loading branch information
6utt3rfly committed Nov 8, 2021
1 parent 4196623 commit e5652eb
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 49 deletions.
17 changes: 12 additions & 5 deletions packages/assignment/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const plugin = {

init(jsep) {
const updateNodeTypes = [jsep.IDENTIFIER, jsep.MEMBER_EXP];
plugin.assignmentOperators.forEach(op => jsep.addBinaryOp(op, plugin.assignmentPrecedence));
plugin.assignmentOperators.forEach(op => jsep.addBinaryOp(op, plugin.assignmentPrecedence, true));

jsep.hooks.add('gobble-token', function gobbleUpdatePrefix(env) {
const code = this.code;
Expand Down Expand Up @@ -65,15 +65,22 @@ const plugin = {
// Note: Binaries can be chained in a single expression to respect
// operator precedence (i.e. a = b = 1 + 2 + 3)
// Update all binary assignment nodes in the tree
updateBinariessToAssignments(env.node);
updateBinariesToAssignments(env.node);
}
});

function updateBinariessToAssignments(node) {
function updateBinariesToAssignments(node) {
if (plugin.assignmentOperators.has(node.operator)) {
node.type = 'AssignmentExpression';
updateBinariessToAssignments(node.left);
updateBinariessToAssignments(node.right);
updateBinariesToAssignments(node.left);
updateBinariesToAssignments(node.right);
}
else if (!node.operator) {
Object.values(node).forEach((val) => {
if (val && typeof val === 'object') {
updateBinariesToAssignments(val);
}
});
}
}
},
Expand Down
132 changes: 88 additions & 44 deletions packages/assignment/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,41 +42,32 @@ const { test } = QUnit;
}));

test('should correctly parse chained assignment', (assert) => {
testParser('a = b = c + 1 = d', {
testParser('a = b = c = d', {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'a'
},
right: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'a'
},
right: {
type: 'Identifier',
name: 'b'
}
type: 'Identifier',
name: 'b'
},
right: {
type: 'BinaryExpression',
operator: '+',
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'c'
},
right: {
type: 'Literal',
value: 1,
raw: '1'
type: 'Identifier',
name: 'd'
}
}
},
right: {
type: 'Identifier',
name: 'd'
}
}, assert);
});
Expand All @@ -86,38 +77,38 @@ const { test } = QUnit;
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'a'
},
right: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'a'
},
right: {
type: 'Identifier',
name: 'b'
}
},
right: {
type: 'ConditionalExpression',
test: {
type: 'Identifier',
name: 'c'
},
consequent: {
type: 'Identifier',
name: 'd'
},
alternate: {
type: 'AssignmentExpression',
operator: '=',
left: {
right: {
type: 'ConditionalExpression',
test: {
type: 'Identifier',
name: 'e'
name: 'c'
},
right: {
type: 'Literal',
value: 2,
raw: '2'
consequent: {
type: 'Identifier',
name: 'd'
},
alternate: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'e'
},
right: {
type: 'Literal',
value: 2,
raw: '2'
}
}
}
}
Expand Down Expand Up @@ -160,6 +151,59 @@ const { test } = QUnit;
}, assert);
});

test('should correctly parse assignment in consequent and alternate', (assert) => {
testParser('a = b + 2 ? c = 3 : d = 4', {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'a'
},
right: {
type: 'ConditionalExpression',
test: {
type: 'BinaryExpression',
left: {
type: 'Identifier',
name: 'b'
},
operator: '+',
right: {
type: 'Literal',
value: 2,
raw: '2'
}
},
consequent: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'c'
},
right: {
type: 'Literal',
value: 3,
raw: '3'
}
},
alternate: {
type: 'AssignmentExpression',
operator: '=',
left: {
type: 'Identifier',
name: 'd'
},
right: {
type: 'Literal',
value: 4,
raw: '4'
}
}
}
}, assert);
});

[
{ expr: 'a++', expect: { operator: '++', prefix: false } },
{ expr: 'a--', expect: { operator: '--', prefix: false } },
Expand Down
12 changes: 12 additions & 0 deletions packages/ternary/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ export default {
consequent,
alternate,
};

// check for operators of higher priority than ternary (i.e. assignment)
// jsep sets || at 1, and assignment at 0.9, and conditional should be between them
if (test.operator && jsep.binary_ops[test.operator] <= 0.9) {
let newTest = test;
while (newTest.right.operator && jsep.binary_ops[newTest.right.operator] <= 0.9) {
newTest = newTest.right;
}
env.node.test = newTest.right;
newTest.right = env.node;
env.node = test;
}
}
else {
this.throwError('Expected :');
Expand Down

0 comments on commit e5652eb

Please sign in to comment.