Fix transform of unary expression in Babel plugin
All of our static evaluation & dead-code elimination transforms need to
happen in post-order, transforming inner nodes first. This is so that
in complex nested cases all transforms see the simplified version of
their inner nodes.
For example:
  async getNimbusExperimentData() {
    if (!PDFJSDev.test("GECKOVIEW")) { return null; }
    // other code
  }
-> [evaluation of PDFJSDev.*]
  async getNimbusExperimentData() {
    if (!false) { return null; }
    // other code
  }
-> [!false -> true]
  async getNimbusExperimentData() {
    if (true) { return null; }
    // other code
  }
-> [if (true) -> replace with the if branch]
  async getNimbusExperimentData() {
    return null;
    // other code
  }
-> [early return -> remove dead code]
  async getNimbusExperimentData() {
    return null;
    // other code
  }
This was done correctly in all cases except for our `UnaryExpression`
transform, which was happening in pre-order.
			
			
This commit is contained in:
		
							parent
							
								
									802f702695
								
							
						
					
					
						commit
						a352f28785
					
				| @ -92,17 +92,22 @@ function babelPluginPDFJSPreprocessor(babel, ctx) { | |||||||
|           } |           } | ||||||
|         }, |         }, | ||||||
|       }, |       }, | ||||||
|       UnaryExpression(path) { |       UnaryExpression: { | ||||||
|         const { node } = path; |         exit(path) { | ||||||
|         if (node.operator === "typeof" && isPDFJSPreprocessor(node.argument)) { |           const { node } = path; | ||||||
|           // typeof PDFJSDev => 'object'
 |           if ( | ||||||
|           path.replaceWith(t.stringLiteral("object")); |             node.operator === "typeof" && | ||||||
|           return; |             isPDFJSPreprocessor(node.argument) | ||||||
|         } |           ) { | ||||||
|         if (node.operator === "!" && t.isBooleanLiteral(node.argument)) { |             // typeof PDFJSDev => 'object'
 | ||||||
|           // !true => false,  !false => true
 |             path.replaceWith(t.stringLiteral("object")); | ||||||
|           path.replaceWith(t.booleanLiteral(!node.argument.value)); |             return; | ||||||
|         } |           } | ||||||
|  |           if (node.operator === "!" && t.isBooleanLiteral(node.argument)) { | ||||||
|  |             // !true => false,  !false => true
 | ||||||
|  |             path.replaceWith(t.booleanLiteral(!node.argument.value)); | ||||||
|  |           } | ||||||
|  |         }, | ||||||
|       }, |       }, | ||||||
|       LogicalExpression: { |       LogicalExpression: { | ||||||
|         exit(path) { |         exit(path) { | ||||||
|  | |||||||
| @ -17,3 +17,5 @@ var i = '0'; | |||||||
| var j = { | var j = { | ||||||
|   i: 1 |   i: 1 | ||||||
| }; | }; | ||||||
|  | var k = false; | ||||||
|  | var l = true; | ||||||
|  | |||||||
							
								
								
									
										2
									
								
								external/builder/fixtures_esprima/evals.js
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								external/builder/fixtures_esprima/evals.js
									
									
									
									
										vendored
									
									
								
							| @ -8,3 +8,5 @@ var g = PDFJSDev.eval('OBJ'); | |||||||
| var h = PDFJSDev.json('$ROOT/external/builder/fixtures_esprima/evals.json'); | var h = PDFJSDev.json('$ROOT/external/builder/fixtures_esprima/evals.json'); | ||||||
| var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0'; | var i = typeof PDFJSDev === 'undefined' ? PDFJSDev.eval('FALSE') : '0'; | ||||||
| var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0'; | var j = typeof PDFJSDev !== 'undefined' ? PDFJSDev.eval('OBJ.obj') : '0'; | ||||||
|  | var k = !PDFJSDev.test('TRUE'); | ||||||
|  | var l = !PDFJSDev.test('FALSE'); | ||||||
|  | |||||||
| @ -400,7 +400,7 @@ class ExternalServices extends BaseExternalServices { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   async getNimbusExperimentData() { |   async getNimbusExperimentData() { | ||||||
|     if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("GECKOVIEW")) { |     if (!PDFJSDev.test("GECKOVIEW")) { | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|     const nimbusData = await FirefoxCom.requestAsync( |     const nimbusData = await FirefoxCom.requestAsync( | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user