Skip to content

Commit

Permalink
fix assignment + destructiring regression (closes #2650) (#2651)
Browse files Browse the repository at this point in the history
* fix assignment + destructiring regression (closes #2650)

* review
  • Loading branch information
AlexKamaev authored Jun 15, 2021
1 parent e985143 commit 06afd8a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
19 changes: 14 additions & 5 deletions src/processing/script/destructuring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import TempVariables from './transformers/temp-variables';
import INSTRUCTION from './instruction';

type NodeBuilder = (pattern: Pattern, value: Expression, isTemp?: boolean) => void;
type NodeBuilder = (pattern: Pattern, value: Expression | null, isTemp?: boolean) => void;

function processObjectProperty (prop: AssignmentProperty, temp: Identifier, build: NodeBuilder, baseTempName: string) {
const pattern = prop.value;
Expand Down Expand Up @@ -55,7 +55,10 @@ function createTempIdentifierOrUseExisting (value: Expression, build: NodeBuilde
return tempIdentifier;
}

function processObjectPattern (pattern: ObjectPattern, value: Expression, build: NodeBuilder, baseTempName?: string) {
function processObjectPattern (pattern: ObjectPattern, value: Expression | null, build: NodeBuilder, baseTempName?: string) {
if (!value)
return;

const properties = pattern.properties;
const hasRest = properties.length && properties[properties.length - 1].type === Syntax.RestElement;
const tempIdentifier = createTempIdentifierOrUseExisting(value, build, baseTempName);
Expand Down Expand Up @@ -104,7 +107,10 @@ function processObjectPattern (pattern: ObjectPattern, value: Expression, build:
}
}

function processArrayPattern (pattern: ArrayPattern, value: Expression, build: NodeBuilder, baseTempName?: string) {
function processArrayPattern (pattern: ArrayPattern, value: Expression | null, build: NodeBuilder, baseTempName?: string) {
if (!value)
return;

const tempIdentifier = createTempIdentifierOrUseExisting(value, build, baseTempName);

if (!baseTempName)
Expand All @@ -127,7 +133,10 @@ function processArrayPattern (pattern: ArrayPattern, value: Expression, build: N
}
}

function processAssignmentPattern (pattern: AssignmentPattern, value: Expression, build: NodeBuilder, baseTempName?: string) {
function processAssignmentPattern (pattern: AssignmentPattern, value: Expression | null, build: NodeBuilder, baseTempName?: string) {
if (!value)
return;

const { left, right } = pattern;
const tempIdentifier = createTempIdentifierOrUseExisting(value, build, baseTempName);
const tempCondition = createBinaryExpression(tempIdentifier, '===', createUndefined());
Expand All @@ -141,7 +150,7 @@ function processAssignmentPattern (pattern: AssignmentPattern, value: Expression
process(left, tempConditional, build, baseTempName);
}

export default function process(pattern: Pattern, value: Expression, build: NodeBuilder, baseTempName?: string) {
export default function process(pattern: Pattern, value: Expression | null, build: NodeBuilder, baseTempName?: string) {
if (pattern.type === Syntax.ObjectPattern)
processObjectPattern(pattern, value, build, baseTempName);
else if (pattern.type === Syntax.ArrayPattern)
Expand Down
4 changes: 3 additions & 1 deletion src/processing/script/node-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export function createExpressionStatement (expression: Expression): ExpressionSt
return { type: Syntax.ExpressionStatement, expression };
}

export function createAssignmentExpression (left: Pattern | MemberExpression, operator: AssignmentOperator, right: Expression): AssignmentExpression {
export function createAssignmentExpression (left: Pattern | MemberExpression, operator: AssignmentOperator, right: Expression | null): AssignmentExpression {
//@ts-ignore the `right` value can actually be null, but the AssignmentExpression type definition
// does not allow to the `right` value be null
return { type: Syntax.AssignmentExpression, operator, left, right };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ const transformer: Transformer<VariableDeclaration> = {
const declarations = [] as VariableDeclarator[];

for (const declarator of node.declarations) {
if (!declarator.init)
continue;

destructuring(declarator.id, declarator.init, (pattern, value) =>
destructuring(declarator.id, declarator.init || null, (pattern, value) =>
declarations.push(createVariableDeclarator(pattern, value)));
}

Expand Down
9 changes: 9 additions & 0 deletions test/server/script-processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1499,5 +1499,14 @@ describe('Script processor', () => {
}
]);
});

it('Should not lose empty var assignment with destructuring', () => {
testProcessing([
{
src: 'var n, { q } = e; n();',
expected: 'var n,_hh$temp0 = e, q = _hh$temp0.q; n();'
}
]);
});
});
});

0 comments on commit 06afd8a

Please sign in to comment.