Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent invalid code when removing assignment target of side-effectful object expression #3921

Merged
merged 1 commit into from Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 });