Skip to content

Commit

Permalink
Disallow assigning to super properties
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadKillingsworth committed Jun 13, 2017
1 parent 4702f7a commit 0506050
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 43 deletions.
10 changes: 9 additions & 1 deletion src/com/google/javascript/jscomp/Es6ConvertSuper.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,14 @@ private void visitSuperPropertyAccess(Node node, Node parent, Node enclosingMemb
Preconditions.checkState(node.isSuper(), node);
Node grandparent = parent.getParent();

if (NodeUtil.isLValue(parent)) {
// We don't support assigning to a super property
compiler.report(
JSError.make(
parent, CANNOT_CONVERT_YET, "Assigning to a super property is not supported."));
return;
}

Node clazz = NodeUtil.getEnclosingClass(node);
Node superName = clazz.getSecondChild();
if (!superName.isQualifiedName()) {
Expand All @@ -251,7 +259,7 @@ private void visitSuperPropertyAccess(Node node, Node parent, Node enclosingMemb
node.replaceWith(superName.cloneTree());
} else {
String newPropName = Joiner.on('.').join(superName.getQualifiedName(), "prototype");
Node newprop = NodeUtil.newQName(compiler, newPropName);
Node newprop = NodeUtil.newQName(compiler, newPropName, node, "super");
node.replaceWith(newprop);
}

Expand Down
44 changes: 2 additions & 42 deletions test/com/google/javascript/jscomp/Es6ToEs3ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ public void testSuperGet() {
public void testSuperAccessToGettersAndSetters() {
// Getters cannot be transpiled to ES3
setLanguageOut(LanguageMode.ECMASCRIPT5);
test(
testError(
LINE_JOINER.join(
"class Base {",
" get g() { return 'base'; }",
Expand All @@ -1424,47 +1424,7 @@ public void testSuperAccessToGettersAndSetters() {
" get g() { return super.g + '-sub'; }",
" set g(v) { super.g = v + '-sub'; }",
"}"),
LINE_JOINER.join(
"/** @constructor @struct */",
"var Base = function() {};",
"/** @type {?} */",
"Base.prototype.g;",
"$jscomp.global.Object.defineProperties(",
" Base.prototype,",
" {",
" g:{",
" configurable:true,",
" enumerable:true,",
" /** @this {Base} */",
" get:function(){return\"base\"},",
" /** @this {Base} */",
" set:function(v){alert(\"base.prototype.g = \" + v);}",
" }",
" });",
"/**",
" * @constructor @struct",
" * @extends {Base}",
" * @param {...?} var_args",
" */",
"var Sub = function(var_args) {",
" Base.apply(this, arguments);",
"};",
"/** @type {?} */",
"Sub.prototype.g;",
"$jscomp.inherits(Sub, Base);",
"$jscomp.global.Object.defineProperties(",
" Sub.prototype,",
" {",
" g:{",
" configurable:true,",
" enumerable:true,",
" /** @this {Sub} */",
" get:function(){return Base.prototype.g + \"-sub\";},",
" /** @this {Sub} */",
" set:function(v){Base.prototype.g = v + \"-sub\";}",
" }",
" });",
""));
CANNOT_CONVERT_YET);
}

public void testSuperNew() {
Expand Down

0 comments on commit 0506050

Please sign in to comment.