Skip to content

Commit

Permalink
Move adding of implicit return values to the block statement and
Browse files Browse the repository at this point in the history
tree-shake arrow functions with block statement bodies.
  • Loading branch information
lukastaegert committed Nov 8, 2017
1 parent 7aa94bc commit bd911aa
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 27 deletions.
17 changes: 10 additions & 7 deletions src/ast/nodes/ArrowFunctionExpression.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Node from '../Node';
import Scope from '../scopes/Scope.js';
import { UNKNOWN_ASSIGNMENT } from '../values';
import Scope from '../scopes/Scope';
import ReturnValueScope from '../scopes/ReturnValueScope';

export default class ArrowFunctionExpression extends Node {
bindNode () {
this.body.bindImplicitReturnExpressionToScope
? this.body.bindImplicitReturnExpressionToScope()
: this.scope.addReturnExpression( this.body );
}

hasEffects () {
return this.included;
}
Expand Down Expand Up @@ -33,13 +39,10 @@ export default class ArrowFunctionExpression extends Node {
}

initialiseScope ( parentScope ) {
this.scope = new Scope( { parent: parentScope } );
this.scope = new ReturnValueScope( { parent: parentScope } );
}

someReturnExpressionAtPath ( path, callOptions, predicateFunction ) {
if ( this.body.type !== 'BlockStatement' ) {
return predicateFunction( path, this.body );
}
return predicateFunction( path, UNKNOWN_ASSIGNMENT );
return this.scope.someReturnExpressionAtPath( path, callOptions, predicateFunction );
}
}
8 changes: 8 additions & 0 deletions src/ast/nodes/BlockStatement.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import Statement from './shared/Statement.js';
import BlockScope from '../scopes/BlockScope';
import UndefinedIdentifier from './shared/UndefinedIdentifier';

export default class BlockStatement extends Statement {
bindImplicitReturnExpressionToScope () {
const lastStatement = this.body[ this.body.length - 1 ];
if ( !lastStatement || lastStatement.type !== 'ReturnStatement' ) {
this.scope.addReturnExpression( new UndefinedIdentifier() );
}
}

hasEffects ( options ) {
// Empty block statements do not have effects even though they may be included as e.g. function body
return this.body.some( child => child.hasEffects( options ) );
Expand Down
6 changes: 1 addition & 5 deletions src/ast/nodes/shared/FunctionNode.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Node from '../../Node.js';
import FunctionScope from '../../scopes/FunctionScope';
import VirtualObjectExpression from './VirtualObjectExpression';
import UndefinedIdentifier from './UndefinedIdentifier';
import { UNKNOWN_ASSIGNMENT } from '../../values';

export default class FunctionNode extends Node {
bindNode () {
this.thisVariable = this.scope.findVariable( 'this' );
this.body.bindImplicitReturnExpressionToScope();
}

hasEffects ( options ) {
Expand Down Expand Up @@ -45,10 +45,6 @@ export default class FunctionNode extends Node {

initialiseNode () {
this.prototypeObject = new VirtualObjectExpression();
const lastBodyStatement = this.body.body[ this.body.body.length - 1 ];
if ( !lastBodyStatement || lastBodyStatement.type !== 'ReturnStatement' ) {
this.scope.addReturnExpression( new UndefinedIdentifier() );
}
}

initialiseScope ( parentScope ) {
Expand Down
2 changes: 1 addition & 1 deletion test/form/samples/arrow-function-return-values/_config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
description: 'forwards return values of body-less arrow functions'
description: 'forwards return values of arrow functions'
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ define(function () { 'use strict';

(() => () => () => console.log( 'effect' ))()()();

const bar = () => () => console.log('effect');
bar()();
const retained1 = () => () => console.log( 'effect' );
retained1()();

const retained2 = () => {
return () => console.log( 'effect' );
};
retained2()();

});
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

(() => () => () => console.log( 'effect' ))()()();

const bar = () => () => console.log('effect');
bar()();
const retained1 = () => () => console.log( 'effect' );
retained1()();

const retained2 = () => {
return () => console.log( 'effect' );
};
retained2()();
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

(() => () => () => console.log( 'effect' ))()()();

const bar = () => () => console.log('effect');
bar()();
const retained1 = () => () => console.log( 'effect' );
retained1()();

const retained2 = () => {
return () => console.log( 'effect' );
};
retained2()();
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@

(() => () => () => console.log( 'effect' ))()()();

const bar = () => () => console.log('effect');
bar()();
const retained1 = () => () => console.log( 'effect' );
retained1()();

const retained2 = () => {
return () => console.log( 'effect' );
};
retained2()();

}());
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@

(() => () => () => console.log( 'effect' ))()()();

const bar = () => () => console.log('effect');
bar()();
const retained1 = () => () => console.log( 'effect' );
retained1()();

const retained2 = () => {
return () => console.log( 'effect' );
};
retained2()();

})));
17 changes: 13 additions & 4 deletions test/form/samples/arrow-function-return-values/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
(() => () => () => {})()()();
(() => () => () => console.log( 'effect' ))()()();

const foo = () => () => {};
foo()();
const bar = () => () => console.log('effect');
bar()();
const removed1 = () => () => {};
removed1()();
const retained1 = () => () => console.log( 'effect' );
retained1()();

const removed2 = () => {
return () => {};
};
removed2()();
const retained2 = () => {
return () => console.log( 'effect' );
};
retained2()();

0 comments on commit bd911aa

Please sign in to comment.