@@ -234,8 +234,9 @@ private void visitImport(NodeTraversal t, Node importDecl, Node parent) {
234
234
child .isImportStar (), "Expected an IMPORT_STAR node, but was: %s" , child );
235
235
// Namespace imports cannot be imported "as *".
236
236
if (isNamespaceImport ) {
237
- compiler .report (t .makeError (importDecl , NAMESPACE_IMPORT_CANNOT_USE_STAR ,
238
- child .getString (), moduleName ));
237
+ compiler .report (
238
+ t .makeError (
239
+ importDecl , NAMESPACE_IMPORT_CANNOT_USE_STAR , child .getString (), moduleName ));
239
240
}
240
241
importMap .put (
241
242
child .getString (),
@@ -288,8 +289,7 @@ private void visitExport(NodeTraversal t, Node export, Node parent) {
288
289
}
289
290
} else if (export .getBooleanProp (Node .EXPORT_ALL_FROM )) {
290
291
// export * from 'moduleIdentifier';
291
- compiler .report (JSError .make (export , Es6ToEs3Util .CANNOT_CONVERT_YET ,
292
- "Wildcard export" ));
292
+ compiler .report (JSError .make (export , Es6ToEs3Util .CANNOT_CONVERT_YET , "Wildcard export" ));
293
293
} else if (export .hasTwoChildren ()) {
294
294
// export {x, y as z} from 'moduleIdentifier';
295
295
Node moduleIdentifier = export .getLastChild ();
@@ -314,8 +314,8 @@ private void visitExport(NodeTraversal t, Node export, Node parent) {
314
314
for (Node exportSpec : export .getFirstChild ().children ()) {
315
315
String nameFromOtherModule = exportSpec .getFirstChild ().getString ();
316
316
String exportedName = exportSpec .getLastChild ().getString ();
317
- exportMap .put (exportedName ,
318
- new NameNodePair (moduleName + "." + nameFromOtherModule , exportSpec ));
317
+ exportMap .put (
318
+ exportedName , new NameNodePair (moduleName + "." + nameFromOtherModule , exportSpec ));
319
319
}
320
320
parent .removeChild (export );
321
321
} else {
@@ -468,19 +468,6 @@ private void visitRequire(Node requireCall, Node parent) {
468
468
compiler .report (JSError .make (parent .getParent (), LHS_OF_GOOG_REQUIRE_MUST_BE_CONST ));
469
469
}
470
470
471
- // If the LHS is a destructuring pattern with the "shorthand" syntax,
472
- // desugar it because otherwise the renaming will not be done correctly.
473
- // const {x} = goog.require('y')
474
- // becomes
475
- // const {x: x} = goog.require('y');
476
- if (parent .isObjectPattern ()) {
477
- for (Node key = parent .getFirstChild (); key != null ; key = key .getNext ()) {
478
- if (!key .hasChildren ()) {
479
- key .addChildToBack (IR .name (key .getString ()).useSourceInfoFrom (key ));
480
- }
481
- }
482
- }
483
-
484
471
Node replacement = NodeUtil .newQName (compiler , namespace ).srcrefTree (requireCall );
485
472
parent .replaceChild (requireCall , replacement );
486
473
Node varNode = parent .getParent ();
@@ -493,6 +480,7 @@ private void visitRequire(Node requireCall, Node parent) {
493
480
494
481
/**
495
482
* Traverses a node tree and
483
+ *
496
484
* <ol>
497
485
* <li>Appends a suffix to all global variable names defined in this module.
498
486
* <li>Changes references to imported values to be property accesses on the
@@ -515,12 +503,7 @@ public void visit(NodeTraversal t, Node n, Node parent) {
515
503
}
516
504
}
517
505
518
- boolean isShorthandObjLitKey =
519
- (n .isStringKey () && !n .hasChildren ())
520
- || (n .isName ()
521
- && n .getParent ().isDefaultValue ()
522
- && n .getGrandparent ().isObjectPattern ());
523
- if (n .isName () || isShorthandObjLitKey ) {
506
+ if (n .isName ()) {
524
507
String name = n .getString ();
525
508
if (suffix .equals (name )) {
526
509
// TODO(moz): Investigate whether we need to return early in this unlikely situation.
@@ -531,14 +514,9 @@ public void visit(NodeTraversal t, Node n, Node parent) {
531
514
if (var != null && var .isGlobal ()) {
532
515
// Avoid polluting the global namespace.
533
516
String newName = name + "$$" + suffix ;
534
- if (isShorthandObjLitKey ) {
535
- // Change {a} to {a: a$$module$foo}
536
- fixShorthandObjLit (t , n , IR .name (newName ));
537
- } else {
538
- n .setString (newName );
539
- n .setOriginalName (name );
540
- t .reportCodeChange (n );
541
- }
517
+ n .setString (newName );
518
+ n .setOriginalName (name );
519
+ t .reportCodeChange (n );
542
520
} else if (var == null && importMap .containsKey (name )) {
543
521
// Change to property access on the imported module object.
544
522
if (parent .isCall () && parent .getFirstChild () == n ) {
@@ -548,48 +526,19 @@ public void visit(NodeTraversal t, Node n, Node parent) {
548
526
ModuleOriginalNamePair pair = importMap .get (name );
549
527
boolean isImportStar = pair .originalName .isEmpty ();
550
528
Node moduleAccess = NodeUtil .newQName (compiler , pair .module );
551
- if (isShorthandObjLitKey ) {
552
- if (isImportStar ) {
553
- fixShorthandObjLit (t , n , moduleAccess );
554
- } else {
555
- fixShorthandObjLit (t , n , IR .getprop (moduleAccess , IR .string (pair .originalName )));
556
- }
529
+
530
+ if (isImportStar ) {
531
+ n .replaceWith (moduleAccess .useSourceInfoIfMissingFromForTree (n ));
557
532
} else {
558
- if (isImportStar ) {
559
- n .replaceWith (moduleAccess .useSourceInfoIfMissingFromForTree (n ));
560
- } else {
561
- n .replaceWith (
562
- IR .getprop (moduleAccess , IR .string (pair .originalName ))
563
- .useSourceInfoIfMissingFromForTree (n ));
564
- }
533
+ n .replaceWith (
534
+ IR .getprop (moduleAccess , IR .string (pair .originalName ))
535
+ .useSourceInfoIfMissingFromForTree (n ));
565
536
t .reportCodeChange (moduleAccess );
566
537
}
567
538
}
568
539
}
569
540
}
570
541
571
- /**
572
- * Replace shorthand object literal references to module imports with fully qualified
573
- * value names. Eg: {foo} becomes {foo: module$imported.foo}.
574
- */
575
- private void fixShorthandObjLit (NodeTraversal t , Node n , Node newNode ) {
576
- if (n .isStringKey ()) {
577
- n .addChildToBack (newNode .useSourceInfoIfMissingFromForTree (n ));
578
- } else {
579
- // The AST looks like:
580
- // DEFAULT_VALUE
581
- // NAME oldName
582
- // VALUE
583
- // It needs a STRING_KEY oldName added as the DEFAULT_VALUE's parent.
584
- // Then to replace the oldName node with the new node.
585
- Node stringKeyNode = IR .stringKey (n .getString ()).srcref (n );
586
- n .getParent ().replaceWith (stringKeyNode );
587
- stringKeyNode .addChildToBack (n .getParent ());
588
- n .replaceWith (newNode );
589
- }
590
- t .reportCodeChange (newNode );
591
- }
592
-
593
542
/**
594
543
* Replace type name references. Change short names to fully qualified names
595
544
* with namespace prefixes. Eg: {Foo} becomes {module$test.Foo}.
@@ -649,8 +598,7 @@ private void fixTypeNode(NodeTraversal t, Node typeNode) {
649
598
}
650
599
}
651
600
652
- for (Node child = typeNode .getFirstChild (); child != null ;
653
- child = child .getNext ()) {
601
+ for (Node child = typeNode .getFirstChild (); child != null ; child = child .getNext ()) {
654
602
fixTypeNode (t , child );
655
603
}
656
604
}
0 commit comments