Merge pull request #3096 from loganfsmyth/double-class-traverse

Avoid duplicating traversal of class declarations - fixes T2694
This commit is contained in:
Sebastian McKenzie 2015-11-19 14:37:21 -08:00
commit ba0388eba5
4 changed files with 74 additions and 6 deletions

View File

@ -8,5 +8,5 @@ var MyClass2 = function MyClass2() {
babelHelpers.classCallCheck(this, MyClass2);
};
export default MyClass2;
MyClass2.property = value;
export default MyClass2;

View File

@ -8,16 +8,23 @@ export default function ({ types: t }) {
return {
visitor: {
ExportDefaultDeclaration(path){
if (!path.get("declaration").isClassDeclaration()) return;
let { node } = path;
let ref = node.declaration.id || path.scope.generateUidIdentifier("class");
node.declaration.id = ref;
// Split the class declaration and the export into two separate statements.
path.replaceWith(node.declaration);
path.insertAfter(t.exportDefaultDeclaration(ref));
},
ClassDeclaration(path) {
let { node } = path;
let ref = node.id || path.scope.generateUidIdentifier("class");
if (path.parentPath.isExportDefaultDeclaration()) {
path = path.parentPath;
path.insertAfter(t.exportDefaultDeclaration(ref));
}
path.replaceWith(t.variableDeclaration("let", [
t.variableDeclarator(ref, t.toExpression(node))
]));

View File

@ -0,0 +1,16 @@
class b {
}
class a1 extends b {
constructor() {
super();
this.x = () => this;
}
}
export default class a2 extends b {
constructor() {
super();
this.x = () => this;
}
}

View File

@ -0,0 +1,45 @@
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var b = function b() {
babelHelpers.classCallCheck(this, b);
};
var a1 = (function (_b) {
babelHelpers.inherits(a1, _b);
function a1() {
babelHelpers.classCallCheck(this, a1);
var _this = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(a1).call(this));
_this.x = function () {
return _this;
};
return _this;
}
return a1;
})(b);
var a2 = (function (_b2) {
babelHelpers.inherits(a2, _b2);
function a2() {
babelHelpers.classCallCheck(this, a2);
var _this2 = babelHelpers.possibleConstructorReturn(this, Object.getPrototypeOf(a2).call(this));
_this2.x = function () {
return _this2;
};
return _this2;
}
return a2;
})(b);
exports.default = a2;