Skip to content

Commit

Permalink
Rewrite "const" declarations of Polymer class objects to "let"
Browse files Browse the repository at this point in the history
With this change, const declarations of Polymer will no longer throw POLYMER_INVALID_DECLARATION error and they will be converted to let declarations instead.

Polymer pass can not handle const declarations of Polymer class objects and throws POLYMER_INVALID_DECLARATION error. The existing JS code which uses this code pattern  gets away with it because ClosureRewriteModule pass rewrites the code to assign to a qualified name, instead of making it a const declaration. (due to export inlining). Since we will be moving closureRewriteModule pass to run after Polymer pass, we need
to make the Polymer pass handle the const declarations of Polymer classes.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=258601781
  • Loading branch information
rishipal authored and lauraharker committed Jul 18, 2019
1 parent bb46d74 commit 34a643f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/com/google/javascript/jscomp/PolymerPass.java
Expand Up @@ -18,13 +18,13 @@
import static com.google.common.base.MoreObjects.toStringHelper; import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_DECLARATION;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_EXTENDS; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_EXTENDS;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_MISSING_EXTERNS; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_MISSING_EXTERNS;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.javascript.jscomp.NodeTraversal.ExternsSkippingCallback; import com.google.javascript.jscomp.NodeTraversal.ExternsSkippingCallback;
import com.google.javascript.jscomp.parsing.parser.FeatureSet.Feature;
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.JSDocInfo; import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.JSDocInfoBuilder; import com.google.javascript.rhino.JSDocInfoBuilder;
Expand Down Expand Up @@ -130,8 +130,12 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
private void rewritePolymer1ClassDefinition(Node node, Node parent, NodeTraversal traversal) { private void rewritePolymer1ClassDefinition(Node node, Node parent, NodeTraversal traversal) {
Node grandparent = parent.getParent(); Node grandparent = parent.getParent();
if (grandparent.isConst()) { if (grandparent.isConst()) {
compiler.report(JSError.make(node, POLYMER_INVALID_DECLARATION)); grandparent.setToken(Token.LET);
return; Node scriptNode = traversal.getCurrentScript();
if (scriptNode != null) {
NodeUtil.addFeatureToScript(scriptNode, Feature.LET_DECLARATIONS);
}
traversal.reportCodeChange();
} }
PolymerClassDefinition def = PolymerClassDefinition.extractFromCallNode( PolymerClassDefinition def = PolymerClassDefinition.extractFromCallNode(
node, compiler, globalNames); node, compiler, globalNames);
Expand Down
5 changes: 0 additions & 5 deletions src/com/google/javascript/jscomp/PolymerPassErrors.java
Expand Up @@ -26,11 +26,6 @@ final class PolymerPassErrors {
"The argument to Polymer() is not an obj lit or the Polymer 2 class does not have a" "The argument to Polymer() is not an obj lit or the Polymer 2 class does not have a"
+ " static getter named 'config'. Ignoring this definition."); + " static getter named 'config'. Ignoring this definition.");


// Disallow 'const Foo = Polymer(...)' because the code the PolymerPass outputs will reassign
// Foo which is not allowed for 'const' variables.
static final DiagnosticType POLYMER_INVALID_DECLARATION = DiagnosticType.error(
"JSC_POLYMER_INVALID_DECLARATION", "A Polymer() declaration cannot use ''const''.");

static final DiagnosticType POLYMER_INVALID_BEHAVIOR = DiagnosticType.error( static final DiagnosticType POLYMER_INVALID_BEHAVIOR = DiagnosticType.error(
"JSC_POLYMER_INVALID_BEHAVIOR", "A Polymer behavior may not include an ''is'' property."); "JSC_POLYMER_INVALID_BEHAVIOR", "A Polymer behavior may not include an ''is'' property.");


Expand Down
7 changes: 2 additions & 5 deletions test/com/google/javascript/jscomp/IntegrationTest.java
Expand Up @@ -767,18 +767,15 @@ public void testPolymer1() {
} }


@Test @Test
public void testConstPolymerElementNotAllowed() { public void testConstPolymerElementAllowed() {
CompilerOptions options = createCompilerOptions(); CompilerOptions options = createCompilerOptions();
options.setPolymerVersion(1); options.setPolymerVersion(1);
options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR); options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.ERROR);
options.setLanguageIn(LanguageMode.ECMASCRIPT_2017); options.setLanguageIn(LanguageMode.ECMASCRIPT_2017);
options.setLanguageOut(LanguageMode.ECMASCRIPT5); options.setLanguageOut(LanguageMode.ECMASCRIPT5);
addPolymerExterns(); addPolymerExterns();


test( testNoWarnings(options, "const Foo = Polymer({ is: 'x-foo' });");
options,
"const Foo = Polymer({ is: 'x-foo' });",
PolymerPassErrors.POLYMER_INVALID_DECLARATION);
} }


private void addPolymer2Externs() { private void addPolymer2Externs() {
Expand Down
15 changes: 9 additions & 6 deletions test/com/google/javascript/jscomp/PolymerPassTest.java
Expand Up @@ -20,7 +20,6 @@
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_CLASS_PROPERTIES_INVALID; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_CLASS_PROPERTIES_INVALID;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_CLASS_PROPERTIES_NOT_STATIC; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_CLASS_PROPERTIES_NOT_STATIC;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_DESCRIPTOR_NOT_VALID; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_DESCRIPTOR_NOT_VALID;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_DECLARATION;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_EXTENDS; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_EXTENDS;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_PROPERTY; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_INVALID_PROPERTY;
import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_MISSING_IS; import static com.google.javascript.jscomp.PolymerPassErrors.POLYMER_MISSING_IS;
Expand Down Expand Up @@ -257,11 +256,15 @@ public void testLetTarget() {


@Test @Test
public void testConstTarget() { public void testConstTarget() {
testError( test(
lines( lines("const X = Polymer({", " is: 'x-element',", "});"),
"const X = Polymer({", lines(
" is: 'x-element',", "/**\n",
"});"), POLYMER_INVALID_DECLARATION); "* @constructor\n",
"* @extends {PolymerElement}\n",
"* @implements {PolymerXInterface}\n",
"*/\n",
"var X=function(){};X=Polymer(/** @lends {X.prototype} */ {is:\"x-element\"})"));


test( test(
lines( lines(
Expand Down

0 comments on commit 34a643f

Please sign in to comment.