Skip to content

Commit

Permalink
Prevent invalid code when removing assignment target of side-effectfu…
Browse files Browse the repository at this point in the history
…l object expression (#3921)
  • Loading branch information
lukastaegert committed Jan 6, 2021
1 parent 966bfa5 commit 2365ece
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/ast/nodes/AssignmentExpression.ts
@@ -1,7 +1,9 @@
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import {
findFirstOccurrenceOutsideComment,
findNonWhiteSpace,
NodeRenderOptions,
RenderOptions
} from '../../utils/renderHelpers';
import { getSystemExportFunctionLeft } from '../../utils/systemJsRendering';
Expand Down Expand Up @@ -62,11 +64,18 @@ export default class AssignmentExpression extends NodeBase {
this.right.include(context, includeChildrenRecursively);
}

render(code: MagicString, options: RenderOptions) {
this.right.render(code, options);
render(
code: MagicString,
options: RenderOptions,
{ renderedParentType }: NodeRenderOptions = BLANK
) {
if (this.left.included) {
this.left.render(code, options);
this.right.render(code, options);
} else {
this.right.render(code, options, {
renderedParentType: renderedParentType || this.parent.type
});
code.remove(this.start, this.right.start);
}
if (options.format === 'system') {
Expand Down
@@ -0,0 +1,4 @@
module.exports = {
description:
'renders valid code when the target of an object expression with side-effects is tree-shaken'
};
@@ -0,0 +1,6 @@
const result = {};

let foo;
foo = { [(result.x = 1)]: (result.y = 2) };

assert.deepStrictEqual(result, { x: 1, y: 2 });

0 comments on commit 2365ece

Please sign in to comment.