@@ -12,6 +12,88 @@ const getQuery = (request) => {
1212 return request . indexOf ( "?" ) < 0 ? "" : request . substr ( i ) ;
1313} ;
1414
15+ const collectDeclaration = ( declarations , pattern ) => {
16+ const stack = [ pattern ] ;
17+ while ( stack . length > 0 ) {
18+ const node = stack . pop ( ) ;
19+ switch ( node . type ) {
20+ case "Identifier" :
21+ declarations . add ( node . name ) ;
22+ break ;
23+ case "ArrayPattern" :
24+ for ( const element of node . elements )
25+ if ( element ) stack . push ( element ) ;
26+ break ;
27+ case "AssignmentPattern" :
28+ stack . push ( node . left ) ;
29+ break ;
30+ case "ObjectPattern" :
31+ for ( const property of node . properties )
32+ stack . push ( property . value ) ;
33+ break ;
34+ case "RestElement" :
35+ stack . push ( node . argument ) ;
36+ break ;
37+ }
38+ }
39+ } ;
40+
41+ const getHoistedDeclarations = ( branch , includeFunctionDeclarations ) => {
42+ const declarations = new Set ( ) ;
43+ const stack = [ branch ] ;
44+ while ( stack . length > 0 ) {
45+ const node = stack . pop ( ) ;
46+ // Some node could be `null` or `undefined`.
47+ if ( ! node )
48+ continue ;
49+ switch ( node . type ) {
50+ // Walk through control statements to look for hoisted declarations.
51+ // Some branches are skipped since they do not allow declarations.
52+ case "BlockStatement" :
53+ for ( const stmt of node . body )
54+ stack . push ( stmt ) ;
55+ break ;
56+ case "IfStatement" :
57+ stack . push ( node . consequent ) ;
58+ stack . push ( node . alternate ) ;
59+ break ;
60+ case "ForStatement" :
61+ stack . push ( node . init ) ;
62+ stack . push ( node . body ) ;
63+ break ;
64+ case "ForInStatement" :
65+ case "ForOfStatement" :
66+ stack . push ( node . left ) ;
67+ stack . push ( node . body ) ;
68+ break ;
69+ case "DoWhileStatement" :
70+ case "WhileStatement" :
71+ case "LabeledStatement" :
72+ stack . push ( node . body ) ;
73+ break ;
74+ case "SwitchStatement" :
75+ for ( const cs of node . cases )
76+ for ( const consequent of cs . consequent )
77+ stack . push ( consequent ) ;
78+ break ;
79+ case "TryStatement" :
80+ stack . push ( node . block ) ;
81+ stack . push ( node . finalizer ) ;
82+ break ;
83+ case "FunctionDeclaration" :
84+ if ( includeFunctionDeclarations )
85+ collectDeclaration ( declarations , node . id ) ;
86+ break ;
87+ case "VariableDeclaration" :
88+ if ( node . kind === "var" )
89+ for ( const decl of node . declarations )
90+ collectDeclaration ( declarations , decl . id ) ;
91+ break ;
92+ }
93+ }
94+ return Array . from ( declarations ) ;
95+ } ;
96+
1597class ConstPlugin {
1698 apply ( compiler ) {
1799 compiler . hooks . compilation . tap ( "ConstPlugin" , ( compilation , {
@@ -30,6 +112,57 @@ class ConstPlugin {
30112 dep . loc = statement . loc ;
31113 parser . state . current . addDependency ( dep ) ;
32114 }
115+ const branchToRemove = bool ? statement . alternate : statement . consequent ;
116+ if ( branchToRemove ) {
117+ // Before removing the dead branch, the hoisted declarations
118+ // must be collected.
119+ //
120+ // Given the following code:
121+ //
122+ // if (true) f() else g()
123+ // if (false) {
124+ // function f() {}
125+ // const g = function g() {}
126+ // if (someTest) {
127+ // let a = 1
128+ // var x, {y, z} = obj
129+ // }
130+ // } else {
131+ // …
132+ // }
133+ //
134+ // the generated code is:
135+ //
136+ // if (true) f() else {}
137+ // if (false) {
138+ // var f, x, y, z; (in loose mode)
139+ // var x, y, z; (in strict mode)
140+ // } else {
141+ // …
142+ // }
143+ //
144+ // NOTE: When code runs in strict mode, `var` declarations
145+ // are hoisted but `function` declarations don't.
146+ //
147+ let declarations ;
148+ if ( parser . scope . isStrict ) {
149+ // If the code runs in strict mode, variable declarations
150+ // using `var` must be hoisted.
151+ declarations = getHoistedDeclarations ( branchToRemove , false ) ;
152+ } else {
153+ // Otherwise, collect all hoisted declaration.
154+ declarations = getHoistedDeclarations ( branchToRemove , true ) ;
155+ }
156+ let replacement ;
157+ if ( declarations . length > 0 ) {
158+ replacement = `{ var ${ declarations . join ( ", " ) } ; }` ;
159+ } else {
160+ replacement = "{}" ;
161+ }
162+ const dep = new ConstDependency ( replacement , branchToRemove . range ) ;
163+ dep . loc = branchToRemove . loc ;
164+ parser . state . current . addDependency ( dep ) ;
165+ }
33166 return bool ;
34167 }
35168 } ) ;
@@ -42,6 +175,21 @@ class ConstPlugin {
42175 dep . loc = expression . loc ;
43176 parser . state . current . addDependency ( dep ) ;
44177 }
178+ // Expressions do not hoist.
179+ // It is safe to remove the dead branch.
180+ //
181+ // Given the following code:
182+ //
183+ // false ? someExpression() : otherExpression();
184+ //
185+ // the generated code is:
186+ //
187+ // false ? undefined : otherExpression();
188+ //
189+ const branchToRemove = bool ? expression . alternate : expression . consequent ;
190+ const dep = new ConstDependency ( "undefined" , branchToRemove . range ) ;
191+ dep . loc = branchToRemove . loc ;
192+ parser . state . current . addDependency ( dep ) ;
45193 return bool ;
46194 }
47195 } ) ;
0 commit comments