Skip to content

Commit

Permalink
fix error when constructor default arg refers to own static property …
Browse files Browse the repository at this point in the history
…or self

(closes #4253)
(closes #4442)
  • Loading branch information
danharper committed Oct 4, 2016
1 parent fc54264 commit 53426d2
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/babel-plugin-transform-es2015-parameters/src/default.js
Expand Up @@ -24,10 +24,16 @@ function hasDefaults(node) {
return false;
}

function isSafeBinding(scope, node) {
if (!scope.hasOwnBinding(node.name)) return true;
const { kind } = scope.getOwnBinding(node.name);
return kind === "param" || kind === "local";
}

let iifeVisitor = {
ReferencedIdentifier(path, state) {
let name = path.node.name;
if (name === "eval" || (path.scope.hasOwnBinding(name) && path.scope.getOwnBinding(name).kind !== "param")) {
const { scope, node } = path;
if (node.name === "eval" || !isSafeBinding(scope, node)) {
state.iife = true;
path.stop();
}
Expand Down Expand Up @@ -100,7 +106,7 @@ export let visitor = {

//
if (!state.iife) {
if (right.isIdentifier() && scope.hasOwnBinding(right.node.name) && scope.getOwnBinding(right.node.name).kind !== "param") {
if (right.isIdentifier() && !isSafeBinding(scope, right.node)) {
// the right hand side references a parameter
state.iife = true;
} else {
Expand Down
@@ -0,0 +1,6 @@
class Ref {
constructor(id = ++Ref.nextID) {
this.id = id
}
}
Ref.nextID = 0
@@ -0,0 +1,9 @@
class Ref {
static nextId = 0
constructor(id = ++Ref.nextId, n = id) {
this.id = n
}
}

assert.equal(1, new Ref().id)
assert.equal(2, new Ref().id)
@@ -0,0 +1,8 @@
var Ref = function Ref() {
var id = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : ++Ref.nextID;
babelHelpers.classCallCheck(this, Ref);

this.id = id;
};

Ref.nextID = 0;
@@ -0,0 +1,11 @@
class Ref {
constructor(ref = Ref) {
this.ref = ref
}
}

class X {
constructor(x = foo) {
this.x = x
}
}
@@ -0,0 +1,7 @@
class Ref {
constructor(ref = Ref) {
this.ref = ref
}
}

assert.equal(Ref, new Ref().ref)
@@ -0,0 +1,13 @@
var Ref = function Ref() {
var ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Ref;
babelHelpers.classCallCheck(this, Ref);

this.ref = ref;
};

var X = function X() {
var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : foo;
babelHelpers.classCallCheck(this, X);

this.x = x;
};

0 comments on commit 53426d2

Please sign in to comment.