Skip to content

Commit

Permalink
Correctly render function expression inside simplified expression sta…
Browse files Browse the repository at this point in the history
…tements. (#2696)

Resolves #2684
  • Loading branch information
lukastaegert committed Feb 17, 2019
1 parent 0a46310 commit 14a17af
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/ast/nodes/CallExpression.ts
@@ -1,3 +1,6 @@
import MagicString from 'magic-string';
import { BLANK } from '../../utils/blank';
import { NodeRenderOptions, RenderOptions } from '../../utils/renderHelpers';
import CallOptions from '../CallOptions';
import { DeoptimizableEntity } from '../DeoptimizableEntity';
import { ExecutionPathOptions } from '../ExecutionPathOptions';
Expand Down Expand Up @@ -210,4 +213,19 @@ export default class CallExpression extends NodeBase implements DeoptimizableEnt
this.returnExpression.deoptimizePath(path);
}
}

render(
code: MagicString,
options: RenderOptions,
{ renderedParentType }: NodeRenderOptions = BLANK
) {
super.render(code, options);
if (
renderedParentType === NodeType.ExpressionStatement &&
this.callee.type === NodeType.FunctionExpression
) {
code.appendRight(this.start, '(');
code.prependLeft(this.end, ')');
}
}
}
@@ -0,0 +1,3 @@
module.exports = {
description: 'correctly handles function expressions which are simplified to statements'
};
@@ -0,0 +1,58 @@
var value;

// conditional expression
function foo(){
value = 'foo';
}

true ? function foo(x){
value = x;
}("consequent") : 2;

assert.equal(value, 'consequent');

foo("incorrect");

assert.equal(value, 'foo');

false ? null: function foo(x){
value = x;
}("alternate");

assert.equal(value, 'alternate');

// logical expression
function bar(){
value = 'bar';
}

true && function bar(x){
value = x;
}("and");

assert.equal(value, 'and');

bar("incorrect");

assert.equal(value, 'bar');

false || function bar(x){
value = x;
}("or");

assert.equal(value, 'or');

// sequence expression
function baz(){
value = 'baz';
}

0, function baz(x){
value = x;
}("comma");

assert.equal(value, 'comma');

baz("incorrect");

assert.equal(value, 'baz');

0 comments on commit 14a17af

Please sign in to comment.