Merge branch 'master' into code-generator

Conflicts:
	lib/6to5/transformers/classes.js
	test/fixtures/transformation/source-maps/class/expected.js
	test/fixtures/transformation/source-maps/class/source-mappings.json
This commit is contained in:
Sebastian McKenzie 2014-11-03 13:38:44 +11:00
commit 95f3ca6348
14 changed files with 110 additions and 39 deletions

View File

@ -102,9 +102,9 @@ map embedded in a comment at the bottom.
Compile the entire `src` directory and output it to the `lib` directory.
$ 6to5 src --out-dir lib
Compile the entire `src` directory and output it to the one concatenated file.
$ 6to5 src --out-file script-compiled.js
Pipe a file in via stdin and output it to `script-compiled.js`
@ -283,6 +283,23 @@ If you're inheriting from a class then static properties are inherited from it
via [\_\_proto\_\_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto),
this is widely supported but you may run into problems with much older browsers.
**NOTE:** `__proto__` is not supported on IE <= 9 so static properties
**will not** be inherited. A possible workaround is to use `super();`:
```javascript
class Foo {
static foo() {
}
}
class Bar extends Foo {
static foo() {
super();
}
}
```
### Generators
The [regenerator runtime](https://github.com/facebook/regenerator/blob/master/runtime.js)

View File

@ -0,0 +1,4 @@
(function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
})

View File

@ -107,16 +107,28 @@ var buildClassBody = function (file, construct, body, className, superName, node
}, true));
}
var instanceProps;
var staticProps;
if (!_.isEmpty(instanceMutatorMap)) {
var protoId = util.template("prototype-identifier", {
CLASS_NAME: className
});
body.push(util.buildDefineProperties(instanceMutatorMap, protoId));
instanceProps = util.buildDefineProperties(instanceMutatorMap, protoId);
}
if (!_.isEmpty(staticMutatorMap)) {
body.push(util.buildDefineProperties(staticMutatorMap, className));
staticProps = util.buildDefineProperties(staticMutatorMap, className);
}
if (instanceProps || staticProps) {
instanceProps = instanceProps || b.literal(null);
staticProps = staticProps || b.literal(null);
body.push(b.expressionStatement(
b.callExpression(file.addDeclaration("class-props"), [className, staticProps, instanceProps])
));
}
};

View File

@ -24,6 +24,9 @@ exports.ObjectExpression = function (node, parent, file) {
return util.template("object-define-properties-closure", {
KEY: objId,
OBJECT: node,
CONTENT: util.buildDefineProperties(mutatorMap, objId).expression
CONTENT: util.template("object-define-properties", {
OBJECT: objId,
PROPS: util.buildDefineProperties(mutatorMap)
})
});
};

View File

@ -100,7 +100,7 @@ exports.pushMutatorMap = function (mutatorMap, key, kind, method) {
}
};
exports.buildDefineProperties = function (mutatorMap, keyNode) {
exports.buildDefineProperties = function (mutatorMap) {
var objExpr = b.objectExpression([]);
_.each(mutatorMap, function (map, key) {
@ -117,10 +117,7 @@ exports.buildDefineProperties = function (mutatorMap, keyNode) {
objExpr.properties.push(propNode);
});
return exports.template("object-define-properties", {
OBJECT: keyNode,
PROPS: objExpr
}, true);
return objExpr;
};
exports.template = function (name, nodes, keepExpression) {

View File

@ -1,5 +1,12 @@
"use strict";
var _slice = Array.prototype.slice;
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var _extends = function (child, parent) {
child.prototype = Object.create(parent.prototype, {
constructor: {
@ -27,7 +34,17 @@ var Test = function(Foo) {
_extends(Test, Foo);
Object.defineProperties(Test.prototype, {
_classProps(Test, {
foo: {
writable: true,
value: function() {
Foo.foo.call(this);
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
}
}
}, {
test: {
writable: true,
@ -39,17 +56,5 @@ var Test = function(Foo) {
}
});
Object.defineProperties(Test, {
foo: {
writable: true,
value: function() {
Foo.foo.call(this);
Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments)));
Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments)));
}
}
});
return Test;
}(Foo);

View File

@ -1,5 +1,10 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var _extends = function (child, parent) {
child.prototype = Object.create(parent.prototype, {
constructor: {
@ -13,7 +18,6 @@ var _extends = function (child, parent) {
child.__proto__ = parent;
};
var Test = function(Foo) {
var Test = function Test() {
Foo.prototype.test.whatever();
@ -22,7 +26,7 @@ var Test = function(Foo) {
_extends(Test, Foo);
Object.defineProperties(Test, {
_classProps(Test, {
test: {
writable: true,
@ -30,7 +34,7 @@ var Test = function(Foo) {
return Foo.wow.call(this);
}
}
});
}, null);
return Test;
}(Foo);

View File

@ -1,9 +1,14 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = function() {
var Test = function Test() {};
Object.defineProperties(Test.prototype, {
_classProps(Test, null, {
test: {
get: function() {
return 5 + 5;
@ -16,4 +21,4 @@ var Test = function() {
});
return Test;
}();
}();

View File

@ -1,9 +1,14 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = function() {
var Test = function Test() {};
Object.defineProperties(Test.prototype, {
_classProps(Test, null, {
test: {
get: function() {
return 5 + 5;
@ -12,4 +17,4 @@ var Test = function() {
});
return Test;
}();
}();

View File

@ -1,9 +1,14 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = function() {
var Test = function Test() {};
Object.defineProperties(Test.prototype, {
_classProps(Test, null, {
test: {
writable: true,
@ -14,4 +19,4 @@ var Test = function() {
});
return Test;
}();
}();

View File

@ -1,9 +1,14 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = function() {
var Test = function Test() {};
Object.defineProperties(Test.prototype, {
_classProps(Test, null, {
test: {
set: function(val) {
this._test = val;
@ -12,4 +17,4 @@ var Test = function() {
});
return Test;
}();
}();

View File

@ -1,9 +1,14 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var A = function() {
var A = function A() {};
Object.defineProperties(A, {
_classProps(A, {
a: {
writable: true,
value: function() {}
@ -13,7 +18,7 @@ var A = function() {
get: function() {},
set: function(b) {}
}
});
}, null);
return A;
}();
}();

View File

@ -1,7 +1,11 @@
"use strict";
var _classProps = function (child, staticProps, instanceProps) {
if (staticProps) Object.defineProperties(child, staticProps);
if (instanceProps) Object.defineProperties(child.prototype, instanceProps);
};
var Test = (function () {
var Test = function Test() {};
Object.defineProperties(Test.prototype, { bar: { get: function () {
_classProps(Test, null, { bar: { get: function () {
throw new Error("wow");
} } });
return Test;

View File

@ -4,7 +4,7 @@
"column": 11
},
"generated": {
"line": 5,
"line": 9,
"column": 11
}
}]