Skip to content

Commit

Permalink
Make sure pattern defaults are deconflicted correctly (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 16, 2018
1 parent 138033e commit c1bb2fb
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/ast/nodes/AssignmentPattern.ts
@@ -1,3 +1,6 @@
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
import { EMPTY_PATH, ObjectPath, UNKNOWN_PATH } from '../values';
import * as NodeType from './NodeType';
Expand Down Expand Up @@ -27,4 +30,13 @@ export default class AssignmentPattern extends NodeBase implements PatternNode {
deoptimizePath(path: ObjectPath) {
path.length === 0 && this.left.deoptimizePath(path);
}

render(
code: MagicString,
options: RenderOptions,
{ isShorthandProperty }: NodeRenderOptions = BLANK
) {
this.left.render(code, options, { isShorthandProperty });
this.right.render(code, options);
}
}
10 changes: 2 additions & 8 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -10,9 +10,7 @@ import { ImmutableEntityPathTracker } from '../utils/ImmutableEntityPathTracker'
import { LiteralValueOrUnknown, ObjectPath, UNKNOWN_EXPRESSION, UNKNOWN_VALUE } from '../values';
import LocalVariable from '../variables/LocalVariable';
import Variable from '../variables/Variable';
import AssignmentPattern from './AssignmentPattern';
import * as NodeType from './NodeType';
import Property from './Property';
import { ExpressionEntity } from './shared/Expression';
import { Node, NodeBase } from './shared/Node';

Expand Down Expand Up @@ -146,7 +144,7 @@ export default class Identifier extends NodeBase {
render(
code: MagicString,
_options: RenderOptions,
{ renderedParentType, isCalleeOfRenderedParent }: NodeRenderOptions = BLANK
{ renderedParentType, isCalleeOfRenderedParent, isShorthandProperty }: NodeRenderOptions = BLANK
) {
if (this.variable) {
const name = this.variable.getName();
Expand All @@ -156,11 +154,7 @@ export default class Identifier extends NodeBase {
storeName: true,
contentOnly: true
});
let relevantParent = this.parent;
if (relevantParent.type === NodeType.AssignmentPattern) {
relevantParent = (<AssignmentPattern>relevantParent).parent;
}
if (relevantParent.type === NodeType.Property && (<Property>relevantParent).shorthand) {
if (isShorthandProperty) {
code.prependRight(this.start, `${this.name}: `);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/ast/nodes/Property.ts
Expand Up @@ -155,7 +155,7 @@ export default class Property extends NodeBase implements DeoptimizableEntity {
if (!this.shorthand) {
this.key.render(code, options);
}
this.value.render(code, options);
this.value.render(code, options, { isShorthandProperty: this.shorthand });
}

private updateReturnExpression() {
Expand Down
1 change: 1 addition & 0 deletions src/utils/renderHelpers.ts
Expand Up @@ -16,6 +16,7 @@ export interface NodeRenderOptions {
isNoStatement?: boolean;
renderedParentType?: string; // also serves as a flag if the rendered parent is different from the actual parent
isCalleeOfRenderedParent?: boolean;
isShorthandProperty?: boolean;
}

export const NO_SEMICOLON: NodeRenderOptions = { isNoStatement: true };
Expand Down
3 changes: 3 additions & 0 deletions test/form/samples/renamed-pattern-defaults/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'handles deconflicting of variables used as defaults in patterns (#2445)'
};
7 changes: 7 additions & 0 deletions test/form/samples/renamed-pattern-defaults/_expected.js
@@ -0,0 +1,7 @@
const EMPTY = null;
const {foo = EMPTY} = {};
console.log(foo);

const EMPTY$1 = null;
const {foo: foo$1 = EMPTY$1} = {};
console.log(foo$1);
3 changes: 3 additions & 0 deletions test/form/samples/renamed-pattern-defaults/dep.js
@@ -0,0 +1,3 @@
const EMPTY = null;
const {foo = EMPTY} = {};
console.log(foo);
5 changes: 5 additions & 0 deletions test/form/samples/renamed-pattern-defaults/main.js
@@ -0,0 +1,5 @@
import './dep.js';

const EMPTY = null;
const {foo = EMPTY} = {};
console.log(foo);

0 comments on commit c1bb2fb

Please sign in to comment.