Skip to content

Commit

Permalink
fix(parser): fixes do while edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
KFlash committed May 16, 2019
1 parent 4ffe12d commit 024e459
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3888,7 +3888,7 @@ export function parseObjectLiteralOrPattern(
destructible |= DestructuringKind.NotDestructible;
}
} else if (parser.destructible & DestructuringKind.Required) {
// report(parser, Errors.InvalidDestructuringTarget);
report(parser, Errors.InvalidDestructuringTarget);
} else {
value = parseMemberOrUpdateExpression(parser, context, value, /* assignable */ 0);
destructible = parser.assignable & AssignmentKind.NotAssignable ? DestructuringKind.NotDestructible : 0;
Expand Down Expand Up @@ -4003,9 +4003,29 @@ export function parseObjectLiteralOrPattern(
if (parser.token === Token.Comma || parser.token === Token.RightBrace) {
if (parser.assignable & AssignmentKind.NotAssignable) destructible |= DestructuringKind.NotDestructible;
} else {
value = parseMemberOrUpdateExpression(parser, context, value, /* isNewExpression */ 0);
value = parseMemberOrUpdateExpression(parser, context, value, /* assignable */ 0);

destructible =
parser.assignable & AssignmentKind.Assignable ? 0 : (destructible = DestructuringKind.NotDestructible);
parser.assignable & AssignmentKind.NotAssignable ? destructible | DestructuringKind.NotDestructible : 0;

const notAssignable = parser.token !== Token.Assign;

if (parser.token !== Token.Comma && parser.token !== Token.RightBrace) {
if (notAssignable) destructible |= DestructuringKind.NotDestructible;

value = parseAssignmentExpression(
parser,
(context | Context.DisallowInContext) ^ Context.DisallowInContext,
value
);

if (notAssignable) destructible |= DestructuringKind.NotDestructible;
} else if (notAssignable) {
destructible |=
type || parser.assignable & AssignmentKind.NotAssignable
? DestructuringKind.NotDestructible
: DestructuringKind.Assignable;
}
}
} else {
value = parseLeftHandSideExpression(parser, context, /* assignable */ 1);
Expand Down
2 changes: 2 additions & 0 deletions test/parser/declarations/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ describe('Declarations - const', () => {
['const x, {y};', Context.None],
['const {x}, y;', Context.None],
['const x = y, {z};', Context.None],
['const let = 1;', Context.Strict],
['let let = 1;', Context.Strict],
['const {x=y};', Context.None],
['const {x:y=z};', Context.None],
['const {x:y=z} = obj, {a:b=c};', Context.None],
Expand Down
24 changes: 24 additions & 0 deletions test/parser/miscellaneous/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,30 @@ describe('Miscellaneous - Pass', () => {
'function f() { [] in [5,6] * [,5,] / [,,5,,] || [a,] && new [,b] % [,,] }',
'function f() { (delete new function f(){} + function(a,b){}(5)(6)) }',
'(delete new function f(){} + function(a,b){}(5)(6))',
`do x
while ({ [y]: {} = x ? null : false })`,
`do x
while ({ [y]: {x = y} = z ? null : false })`,
`do x
while ({ [y = [yy]]: { x = (y)} ? null : false })`,
`do x
while ({ [(y)]: {} ? null : false })`,
`do x
while ({ [y]: {} ? function () {} : false })`,
`do x
while ({ [(y)()]: {} ? null : false })`,
`do x
while ({ [(x)(y = 2 / 3)]: { } ? null : false })`,
`do x
while ({ [(x)(y = 2 / 3)]: { x } ? (((y = z))) : {x} = y })`,
`do x
while ({ [(y)((([[x / y - 2]])))]: {} ? null : false })`,
`do x
while ({ [(y)((([[x / y - 2]])))]: {} = {[x]: {} = x ? null : false } ? null : false })`,
`do x
while ({ ["foo"]: {bar} ? z - (y) : {a}[b] })`,
`do x
while ({ [[](e)]: {f,g,h, i: (j)} ? null : false })`,
"let e = ['_this_', '_x_', '_y_', '_z_', '[object Arguments]'];",
"let r = f.call('_this_', '_x_', '_y_', '_z_')();",
'function f() { function f() {} + function g() {} }',
Expand Down
52 changes: 52 additions & 0 deletions test/parser/statements/do-while.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,58 @@ describe('Statements - Do while', () => {
]);

pass('Statements - Do while (pass)', [
[
`do x
while ({ [y]: {} ? null : false })`,
Context.None,
{
body: [
{
body: {
expression: {
name: 'x',
type: 'Identifier'
},
type: 'ExpressionStatement'
},
test: {
properties: [
{
computed: true,
key: {
name: 'y',
type: 'Identifier'
},
kind: 'init',
method: false,
shorthand: false,
type: 'Property',
value: {
alternate: {
type: 'Literal',
value: false
},
consequent: {
type: 'Literal',
value: null
},
test: {
properties: [],
type: 'ObjectExpression'
},
type: 'ConditionalExpression'
}
}
],
type: 'ObjectExpression'
},
type: 'DoWhileStatement'
}
],
sourceType: 'script',
type: 'Program'
}
],
[
'do async \n while (y)',
Context.None,
Expand Down

0 comments on commit 024e459

Please sign in to comment.