Skip to content

Commit 2621850

Browse files
lauraharkerTyler Breisacher
authored and
Tyler Breisacher
committed
Remove even more workarounds for the old AST structure of shorthand properties.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=179745653
1 parent ac41550 commit 2621850

6 files changed

+19
-106
lines changed

src/com/google/javascript/jscomp/CheckMissingAndExtraRequires.java

-6
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,6 @@ public void visit(NodeTraversal t, Node n, Node parent) {
218218
visitQualifiedName(t, n, parent);
219219
}
220220
break;
221-
case STRING_KEY:
222-
if (parent.isObjectLit() && !n.hasChildren()) {
223-
// Object literal shorthand. This is a usage of the name.
224-
visitQualifiedName(t, n, parent);
225-
}
226-
break;
227221
case CALL:
228222
visitCallNode(t, n, parent);
229223
break;

src/com/google/javascript/jscomp/Es6RewriteModules.java

+18-70
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,9 @@ private void visitImport(NodeTraversal t, Node importDecl, Node parent) {
234234
child.isImportStar(), "Expected an IMPORT_STAR node, but was: %s", child);
235235
// Namespace imports cannot be imported "as *".
236236
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));
239240
}
240241
importMap.put(
241242
child.getString(),
@@ -288,8 +289,7 @@ private void visitExport(NodeTraversal t, Node export, Node parent) {
288289
}
289290
} else if (export.getBooleanProp(Node.EXPORT_ALL_FROM)) {
290291
// 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"));
293293
} else if (export.hasTwoChildren()) {
294294
// export {x, y as z} from 'moduleIdentifier';
295295
Node moduleIdentifier = export.getLastChild();
@@ -314,8 +314,8 @@ private void visitExport(NodeTraversal t, Node export, Node parent) {
314314
for (Node exportSpec : export.getFirstChild().children()) {
315315
String nameFromOtherModule = exportSpec.getFirstChild().getString();
316316
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));
319319
}
320320
parent.removeChild(export);
321321
} else {
@@ -468,19 +468,6 @@ private void visitRequire(Node requireCall, Node parent) {
468468
compiler.report(JSError.make(parent.getParent(), LHS_OF_GOOG_REQUIRE_MUST_BE_CONST));
469469
}
470470

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-
484471
Node replacement = NodeUtil.newQName(compiler, namespace).srcrefTree(requireCall);
485472
parent.replaceChild(requireCall, replacement);
486473
Node varNode = parent.getParent();
@@ -493,6 +480,7 @@ private void visitRequire(Node requireCall, Node parent) {
493480

494481
/**
495482
* Traverses a node tree and
483+
*
496484
* <ol>
497485
* <li>Appends a suffix to all global variable names defined in this module.
498486
* <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) {
515503
}
516504
}
517505

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()) {
524507
String name = n.getString();
525508
if (suffix.equals(name)) {
526509
// 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) {
531514
if (var != null && var.isGlobal()) {
532515
// Avoid polluting the global namespace.
533516
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);
542520
} else if (var == null && importMap.containsKey(name)) {
543521
// Change to property access on the imported module object.
544522
if (parent.isCall() && parent.getFirstChild() == n) {
@@ -548,48 +526,19 @@ public void visit(NodeTraversal t, Node n, Node parent) {
548526
ModuleOriginalNamePair pair = importMap.get(name);
549527
boolean isImportStar = pair.originalName.isEmpty();
550528
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));
557532
} 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));
565536
t.reportCodeChange(moduleAccess);
566537
}
567538
}
568539
}
569540
}
570541

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-
593542
/**
594543
* Replace type name references. Change short names to fully qualified names
595544
* with namespace prefixes. Eg: {Foo} becomes {module$test.Foo}.
@@ -649,8 +598,7 @@ private void fixTypeNode(NodeTraversal t, Node typeNode) {
649598
}
650599
}
651600

652-
for (Node child = typeNode.getFirstChild(); child != null;
653-
child = child.getNext()) {
601+
for (Node child = typeNode.getFirstChild(); child != null; child = child.getNext()) {
654602
fixTypeNode(t, child);
655603
}
656604
}

src/com/google/javascript/jscomp/Normalize.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -679,9 +679,8 @@ private void extractForInitializer(
679679
List<Node> lhsNodes = NodeUtil.findLhsNodesInNode(lhs);
680680
for (Node name : lhsNodes) {
681681
// Add a declaration outside the for loop for the given name.
682-
// The lhs can be a string key in property shorthand.
683682
checkState(
684-
name.isName() || name.isStringKey(),
683+
name.isName(),
685684
"lhs in destructuring declaration should be a simple name.",
686685
name);
687686
Node newName = IR.name(name.getString()).srcref(name);

src/com/google/javascript/jscomp/PolymerClassRewriter.java

-19
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,6 @@ final class PolymerClassRewriter {
6969
void rewritePolymerCall(
7070
Node exprRoot, final PolymerClassDefinition cls, boolean isInGlobalScope) {
7171
Node objLit = checkNotNull(cls.descriptor);
72-
if (hasShorthandAssignment(objLit)) {
73-
compiler.report(JSError.make(objLit, PolymerPassErrors.POLYMER_SHORTHAND_NOT_SUPPORTED));
74-
return;
75-
}
7672

7773
// Add {@code @lends} to the object literal.
7874
JSDocInfoBuilder objLitDoc = new JSDocInfoBuilder(true);
@@ -306,11 +302,6 @@ private void addTypesToFunctions(
306302
if (!property.value.isObjectLit()) {
307303
continue;
308304
}
309-
if (hasShorthandAssignment(property.value)){
310-
compiler.report(
311-
JSError.make(property.value, PolymerPassErrors.POLYMER_SHORTHAND_NOT_SUPPORTED));
312-
return;
313-
}
314305

315306
Node defaultValue = NodeUtil.getFirstPropMatchingKey(property.value, "value");
316307
if (defaultValue == null || !defaultValue.isFunction()) {
@@ -598,16 +589,6 @@ private void addInterfaceExterns(
598589
compiler.reportChangeToEnclosingScope(stmts);
599590
}
600591

601-
private static boolean hasShorthandAssignment(Node objLit) {
602-
checkState(objLit.isObjectLit());
603-
for (Node property : objLit.children()){
604-
if (property.isStringKey() && !property.hasChildren()){
605-
return true;
606-
}
607-
}
608-
return false;
609-
}
610-
611592
/**
612593
* @return The name of the generated extern interface which the element implements.
613594
*/

src/com/google/javascript/jscomp/PolymerPassErrors.java

-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ final class PolymerPassErrors {
6363
"JSC_POLYMER_UNANNOTATED_BEHAVIOR",
6464
"Behavior declarations must be annotated with @polymerBehavior.");
6565

66-
static final DiagnosticType POLYMER_SHORTHAND_NOT_SUPPORTED =
67-
DiagnosticType.error(
68-
"JSC_POLYMER_SHORTHAND_NOT_SUPPORTED",
69-
"Shorthand assignment in object literal is not allowed in Polymer call arguments");
70-
7166
static final DiagnosticType POLYMER_CLASS_PROPERTIES_INVALID =
7267
DiagnosticType.error(
7368
"JSC_POLYMER_CLASS_PROPERTIES_INVALID",

src/com/google/javascript/jscomp/PolymerPassStaticUtils.java

-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ static ImmutableList<MemberDefinition> extractProperties(
115115

116116
ImmutableList.Builder<MemberDefinition> members = ImmutableList.builder();
117117
for (Node keyNode : properties.children()) {
118-
if (!keyNode.hasChildren()) {
119-
compiler.report(JSError.make(keyNode, PolymerPassErrors.POLYMER_SHORTHAND_NOT_SUPPORTED));
120-
continue;
121-
}
122118
members.add(new MemberDefinition(NodeUtil.getBestJSDocInfo(keyNode), keyNode,
123119
keyNode.getFirstChild()));
124120
}

0 commit comments

Comments
 (0)