@@ -45,11 +45,18 @@ function optPassSimplifyModularizeFunction(ast) {
45
45
if ( node . params . length == 1 && node . params [ 0 ] . name == 'Module' ) {
46
46
const body = node . body . body ;
47
47
// Nuke 'Module = Module || {};'
48
- if ( body [ 0 ] . type == 'ExpressionStatement' && body [ 0 ] . expression . type == 'AssignmentExpression' && body [ 0 ] . expression . left . name == 'Module' ) {
48
+ if (
49
+ body [ 0 ] . type == 'ExpressionStatement' &&
50
+ body [ 0 ] . expression . type == 'AssignmentExpression' &&
51
+ body [ 0 ] . expression . left . name == 'Module'
52
+ ) {
49
53
body . splice ( 0 , 1 ) ;
50
54
}
51
55
// Replace 'function(Module) {var f = Module;' -> 'function(f) {'
52
- if ( body [ 0 ] . type == 'VariableDeclaration' && body [ 0 ] . declarations [ 0 ] ?. init ?. name == 'Module' ) {
56
+ if (
57
+ body [ 0 ] . type == 'VariableDeclaration' &&
58
+ body [ 0 ] . declarations [ 0 ] ?. init ?. name == 'Module'
59
+ ) {
53
60
node . params [ 0 ] . name = body [ 0 ] . declarations [ 0 ] . id . name ;
54
61
body [ 0 ] . declarations . splice ( 0 , 1 ) ;
55
62
if ( body [ 0 ] . declarations . length == 0 ) {
@@ -66,8 +73,13 @@ function optPassSimplifyModularizeFunction(ast) {
66
73
function optPassSimplifyModuleInitialization ( ast ) {
67
74
visitNodes ( ast , [ 'BlockStatement' , 'Program' ] , ( node ) => {
68
75
for ( const n of node . body ) {
69
- if ( n . type == 'ExpressionStatement' && n . expression . type == 'LogicalExpression' && n . expression . operator == '||' &&
70
- n . expression . left . name === n . expression . right . left ?. name && n . expression . right . right . name == 'Module' ) {
76
+ if (
77
+ n . type == 'ExpressionStatement' &&
78
+ n . expression . type == 'LogicalExpression' &&
79
+ n . expression . operator == '||' &&
80
+ n . expression . left . name === n . expression . right . left ?. name &&
81
+ n . expression . right . right . name == 'Module'
82
+ ) {
71
83
// Clear out the logical operator.
72
84
n . expression = n . expression . right ;
73
85
// There is only one Module assignment, so can finish the pass here.
@@ -169,7 +181,11 @@ function optPassMergeVarInitializationAssignments(ast) {
169
181
const name = nodeArray [ i ] . expression . left . name ;
170
182
for ( let j = i - 1 ; j >= 0 ; -- j ) {
171
183
const n = nodeArray [ j ] ;
172
- if ( n . type == 'ExpressionStatement' && n . expression . type == 'AssignmentExpression' && n . expression . left . name == name ) {
184
+ if (
185
+ n . type == 'ExpressionStatement' &&
186
+ n . expression . type == 'AssignmentExpression' &&
187
+ n . expression . left . name == name
188
+ ) {
173
189
return [ null , null ] ;
174
190
}
175
191
if ( n . type == 'VariableDeclaration' ) {
@@ -248,7 +264,10 @@ function test(input, expected) {
248
264
249
265
function runTests ( ) {
250
266
// optPassSimplifyModularizeFunction:
251
- test ( 'var Module = function(Module) {Module = Module || {};var f = Module;}' , 'var Module=function(f){};' ) ;
267
+ test (
268
+ 'var Module = function(Module) {Module = Module || {};var f = Module;}' ,
269
+ 'var Module=function(f){};'
270
+ ) ;
252
271
253
272
// optPassSimplifyModuleInitialization:
254
273
test ( 'b || (b = Module);' , 'b=Module;' ) ;
@@ -258,7 +277,10 @@ function runTests() {
258
277
test ( 'new Uint16Array(a);' , '' ) ;
259
278
test ( 'new Uint16Array(a),new Uint16Array(a);' , ';' ) ;
260
279
test ( "new function(a) {new TextDecoder(a);}('utf8');" , '' ) ;
261
- test ( 'WebAssembly.instantiate(c.wasm,{}).then(function(a){new Int8Array(b);});' , 'WebAssembly.instantiate(c.wasm,{}).then(function(a){});' ) ;
280
+ test (
281
+ 'WebAssembly.instantiate(c.wasm,{}).then(function(a){new Int8Array(b);});' ,
282
+ 'WebAssembly.instantiate(c.wasm,{}).then(function(a){});'
283
+ ) ;
262
284
test ( 'let x=new Uint16Array(a);' , 'let x=new Uint16Array(a);' ) ;
263
285
264
286
// optPassMergeVarDeclarations:
@@ -271,7 +293,10 @@ function runTests() {
271
293
test ( 'var a = 1, b; ++a; var c;' , 'var a=1,b,c;++a;' ) ;
272
294
273
295
// Interaction between multiple passes:
274
- test ( 'var d, f; f = new Uint8Array(16); var h = f.buffer; d = new Uint8Array(h);' , 'var f=new Uint8Array(16),h=f.buffer,d=new Uint8Array(h);' ) ;
296
+ test (
297
+ 'var d, f; f = new Uint8Array(16); var h = f.buffer; d = new Uint8Array(h);' ,
298
+ 'var f=new Uint8Array(16),h=f.buffer,d=new Uint8Array(h);'
299
+ ) ;
275
300
276
301
// Older versions of terser would produce sub-optimal output for this.
277
302
// We keep this test around to prevent regression.
0 commit comments