Skip to content

Commit

Permalink
Don't manipulate any jsdoc annotations during generator function tran…
Browse files Browse the repository at this point in the history
…spilation (it has no effect)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=195458523
  • Loading branch information
SergejSalnikov authored and lauraharker committed May 5, 2018
1 parent 5856c66 commit 436532d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 60 deletions.
39 changes: 3 additions & 36 deletions src/com/google/javascript/jscomp/Es6RewriteGenerators.java
Expand Up @@ -24,8 +24,6 @@
import com.google.javascript.jscomp.parsing.parser.FeatureSet;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfoBuilder;
import com.google.javascript.rhino.Node;
import com.google.javascript.rhino.Token;
import com.google.javascript.rhino.jstype.FunctionType;
Expand Down Expand Up @@ -2096,9 +2094,6 @@ void visitThis(Node n) {
if (!thisReferenceFound) {
Node var = IR.var(newThis.cloneNode().useSourceInfoFrom(n), n)
.useSourceInfoFrom(newGeneratorHoistBlock);
JSDocInfoBuilder jsDoc = new JSDocInfoBuilder(false);
jsDoc.recordConstancy();
var.setJSDocInfo(jsDoc.build());
hoistNode(var);
thisReferenceFound = true;
}
Expand All @@ -2114,34 +2109,11 @@ void visitArguments(Node n) {
if (!argumentsReferenceFound) {
Node var =
IR.var(newArguments.cloneNode(), n).useSourceInfoFrom(newGeneratorHoistBlock);
JSDocInfoBuilder jsDoc = new JSDocInfoBuilder(false);
jsDoc.recordConstancy();
var.setJSDocInfo(jsDoc.build());
hoistNode(var);
argumentsReferenceFound = true;
}
}

/** Removes {@code @const} annotation from the node if present. */
void maybeRemoveConstAnnotation(Node n) {
if (n.getJSDocInfo() != null && n.getJSDocInfo().hasConstAnnotation()) {
// TODO(skill): report a warning that @const will be ignored.
JSDocInfoBuilder fixedJSDoc = JSDocInfoBuilder.copyFrom(n.getJSDocInfo());
fixedJSDoc.clearConstancy();
n.setJSDocInfo(fixedJSDoc.build());
}
}

/** Moves JsDocInfo from one node to another */
void moveJsDocInfo(Node from, Node to) {
checkState(to.getJSDocInfo() == null);
JSDocInfo jsDocInfo = from.getJSDocInfo();
if (jsDocInfo != null) {
from.setJSDocInfo(null);
to.setJSDocInfo(jsDocInfo);
}
}

/**
* Hoists {@code var} statements into the closure containing the generator to preserve their
* state across multiple invocation of state machine program.
Expand All @@ -2160,20 +2132,15 @@ void moveJsDocInfo(Node from, Node to) {
* </pre>
*/
void visitVar(Node varStatement) {
maybeRemoveConstAnnotation(varStatement);
ArrayList<Node> assignments = new ArrayList<>();
for (Node varName : varStatement.children()) {
if (varName.hasChildren()) {
Node copiedVarName = varName.cloneNode();
Node copiedVarName = varName.cloneNode().setJSDocInfo(null);
Node assign =
IR.assign(copiedVarName, varName.removeFirstChild()).useSourceInfoFrom(varName);
moveJsDocInfo(copiedVarName, assign);
assign.setTypeI(varName.getTypeI());
withType(IR.assign(copiedVarName, varName.removeFirstChild()), varName.getTypeI())
.useSourceInfoFrom(varName);
assignments.add(assign);
}
// Variable assignment will keep @const declaration, if any, but we must remove it from
// the name declaration.
maybeRemoveConstAnnotation(varName);
}
if (assignments.isEmpty()) {
varStatement.detach();
Expand Down
16 changes: 0 additions & 16 deletions src/com/google/javascript/rhino/JSDocInfoBuilder.java
Expand Up @@ -749,22 +749,6 @@ public boolean recordConstancy() {
}
}

/**
* Records that the {@link JSDocInfo} being built should have its
* {@link JSDocInfo#isConstant()} flag set to {@code false}.
*
* @return {@code true} if the constancy was cleared and {@code false}
* if it was not defined
*/
public boolean clearConstancy() {
if (currentInfo.isConstant()) {
currentInfo.setConstant(false);
return true;
} else {
return false;
}
}

/**
* Records that the {@link JSDocInfo} being built should have its
* {@link JSDocInfo#isFinal()} flag set to {@code true}.
Expand Down
16 changes: 8 additions & 8 deletions test/com/google/javascript/jscomp/Es6RewriteGeneratorsTest.java
Expand Up @@ -703,20 +703,20 @@ public void testReturn() {

rewriteGeneratorBodyWithVars(
"return this;",
"/** @const */ var $jscomp$generator$this = this;",
"var $jscomp$generator$this = this;",
lines(
"return $jscomp$generator$context.return($jscomp$generator$this);"));

rewriteGeneratorBodyWithVars(
"return this.test({value: this});",
"/** @const */ var $jscomp$generator$this = this;",
"var $jscomp$generator$this = this;",
lines(
"return $jscomp$generator$context.return(",
" $jscomp$generator$this.test({value: $jscomp$generator$this}));"));

rewriteGeneratorBodyWithVars(
"return this[yield];",
"/** @const */ var $jscomp$generator$this = this;",
"var $jscomp$generator$this = this;",
lines(
"if ($jscomp$generator$context.nextAddress == 1)",
" return $jscomp$generator$context.yield(undefined, 2);",
Expand Down Expand Up @@ -867,15 +867,15 @@ public void testYieldAll() {
public void testYieldArguments() {
rewriteGeneratorBodyWithVars(
"yield arguments[0];",
"/** @const */ var $jscomp$generator$arguments = arguments;",
"var $jscomp$generator$arguments = arguments;",
lines(
"return $jscomp$generator$context.yield($jscomp$generator$arguments[0], 0);"));
}

public void testYieldThis() {
rewriteGeneratorBodyWithVars(
"yield this;",
"/** @const */ var $jscomp$generator$this = this;",
"var $jscomp$generator$this = this;",
lines(
"return $jscomp$generator$context.yield($jscomp$generator$this, 0);"));
}
Expand Down Expand Up @@ -949,14 +949,14 @@ public void testVar() {

rewriteGeneratorBodyWithVars(
lines(
"var /** @const @type {number} */ a = 10, b, c = yield 10, d = yield 20, f, g='test';"),
"var /** @const */ a = 10, b, c = yield 10, d = yield 20, f, g='test';"),
lines(
"var /** @type {number} */ a, b;",
"var /** @const */ a, b;",
"var c;", // note that the yields cause the var declarations to be split up
"var d, f, g;"),
lines(
"if ($jscomp$generator$context.nextAddress == 1) {",
" /** @const @type {number} */ a = 10;",
" a = 10;",
" return $jscomp$generator$context.yield(10, 2);",
"}",
"if ($jscomp$generator$context.nextAddress != 3) {",
Expand Down

0 comments on commit 436532d

Please sign in to comment.