Compare commits

...

16 Commits

Author SHA1 Message Date
Sebastian McKenzie
682668c219 v2.9.2 2015-01-09 19:51:44 +11:00
Sebastian McKenzie
dd64297838 add 2.9.2 changelog 2015-01-09 19:49:10 +11:00
Sebastian McKenzie
944a9d3908 pass exports to exportsWildcard - fixes #430 2015-01-09 19:48:11 +11:00
Sebastian McKenzie
d6922c9b75 v2.9.1 2015-01-09 19:34:28 +11:00
Sebastian McKenzie
fb0fcc7138 fix runtime generator breaking the helper inclusion loop - fixes #429 2015-01-09 19:31:43 +11:00
Sebastian McKenzie
89148e6029 Merge pull request #424 from 6to5/more-super-issues
Ensure more than one level of superclass works.
2015-01-09 08:35:40 +11:00
Brian Donovan
28d10b8eb4 Ensure more than one level of superclass works. 2015-01-08 13:33:02 -08:00
Sebastian McKenzie
3509990563 enable esnext tests by default 2015-01-09 07:23:17 +11:00
Sebastian McKenzie
c5960fb9f7 Merge pull request #408 from 6to5/fix-esnext-class-tests
[WIP] Fix super with getters and setters and with class prototypes changing.
2015-01-09 07:22:35 +11:00
Brian Donovan
07131576cf Use undefined instead of void 0. 2015-01-08 11:59:01 -08:00
Sebastian McKenzie
e75e778300 v2.9.0 2015-01-09 06:02:43 +11:00
Sebastian McKenzie
f6cb14c975 upgrade acorn-6to5 2015-01-09 06:00:19 +11:00
Brian Donovan
af1912ab7a Ensure constructors use the current super class.
This is an extension of 324a4a1b22.
2015-01-08 09:31:18 -08:00
Brian Donovan
8c478f29bc Use desc.writable instead of "writable" in desc as suggested by @stefanpenner. 2015-01-08 09:21:35 -08:00
Brian Donovan
5b4d6d7ba9 Replace superIdentifier with superProperty.
This also disallows the usage of bare `super` that is not part of a
member expression, call expression, or new expression.
2015-01-08 09:21:32 -08:00
Brian Donovan
324a4a1b22 Fix super with getters and setters and with class prototypes changing. 2015-01-07 21:10:36 -08:00
27 changed files with 267 additions and 96 deletions

View File

@@ -11,6 +11,22 @@
_Note: Gaps between patch versions are faulty/broken releases._
## 2.9.2
* **Bug Fix**
* Pass `exports` to `exportWildcard` helper to allow for use inside the optional runtime.
## 2.9.1
* **Bug Fix**
* Fix runtime generator breaking the helper inclusion loop.
## 2.9.0
* **Internal**
* Upgrade `acorn-6to5`.
* Now supports destructuring shorthand properties.
## 2.8.1
* **Bug Fix**

View File

@@ -36,7 +36,8 @@ File.helpers = [
"interop-require-wildcard",
"typeof",
"exports-wildcard",
"extends"
"extends",
"get"
];
File.excludeHelpersFromRuntime = [

View File

@@ -20,7 +20,7 @@ module.exports = function (namespace) {
_.each(File.helpers, function (name) {
if (_.contains(File.excludeHelpersFromRuntime, name)) {
return false;
return;
}
var key = t.identifier(t.toIdentifier(name));

View File

@@ -218,7 +218,8 @@ DefaultFormatter.prototype._exportSpecifier = function (getRef, specifier, node,
DefaultFormatter.prototype._exportsWildcard = function (objectIdentifier) {
return t.expressionStatement(t.callExpression(this.file.addHelper("exports-wildcard"), [
t.callExpression(this.file.addHelper("interop-require-wildcard"), [objectIdentifier])
t.callExpression(this.file.addHelper("interop-require-wildcard"), [objectIdentifier]),
t.identifier("exports")
]));
};

View File

@@ -1,3 +1,3 @@
if (SUPER_NAME !== null) {
SUPER_NAME.apply(this, arguments);
if (Object.getPrototypeOf(CLASS_NAME) !== null) {
Object.getPrototypeOf(CLASS_NAME).apply(this, arguments);
}

View File

@@ -1,4 +1,4 @@
(function (obj) {
(function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];

View File

@@ -0,0 +1,23 @@
(function get(object, property, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc && desc.writable) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
});

View File

@@ -149,7 +149,7 @@ Class.prototype.buildBody = function () {
if (!this.hasConstructor && superName && !t.isFalsyExpression(superName)) {
constructor.body.body.push(util.template("class-super-constructor-call", {
SUPER_NAME: superName
CLASS_NAME: className
}, true));
}
@@ -215,45 +215,45 @@ Class.prototype.pushMethod = function (node) {
};
/**
* Given a `methodNode`, produce a `MemberExpression` super class reference.
* Gets a node representing the super class value of the named property.
*
* @param {Node} methodNode MethodDefinition
* @param {Node} node Identifier
* @param {Node} parent
* @example
*
* _get(Object.getPrototypeOf(CLASS.prototype), "METHOD", this)
*
* @param {Node} property
* @param {boolean} isStatic
* @param {boolean} isComputed
*
* @returns {Node}
*/
Class.prototype.superIdentifier = function (methodNode, id, parent) {
var methodName = methodNode.key;
var superName = this.superName || t.identifier("Function");
if (parent.property === id) {
return;
} else if (t.isCallExpression(parent, { callee: id })) {
// super(); -> ClassName.prototype.MethodName.call(this);
parent.arguments.unshift(t.thisExpression());
if (methodName.name === "constructor") {
// constructor() { super(); }
return t.memberExpression(superName, t.identifier("call"));
} else {
id = superName;
// foo() { super(); }
if (!methodNode.static) {
id = t.memberExpression(id, t.identifier("prototype"));
}
id = t.memberExpression(id, methodName, methodNode.computed);
return t.memberExpression(id, t.identifier("call"));
}
} else if (t.isMemberExpression(parent) && !methodNode.static) {
// super.test -> ClassName.prototype.test
return t.memberExpression(superName, t.identifier("prototype"));
} else {
return superName;
}
Class.prototype.superProperty = function (property, isStatic, isComputed) {
return t.callExpression(
this.file.addHelper("get"),
[
t.callExpression(
t.memberExpression(
t.identifier("Object"),
t.identifier("getPrototypeOf"),
false
),
[
isStatic ?
this.className :
t.memberExpression(
this.className,
t.identifier("prototype"),
false
)
]
),
isComputed ?
property :
t.literal(property.name),
t.thisExpression()
]
);
};
/**
@@ -268,16 +268,48 @@ Class.prototype.replaceInstanceSuperReferences = function (methodNode) {
traverse(method, {
enter: function (node, parent) {
var property;
var computed;
var args;
if (t.isIdentifier(node, { name: "super" })) {
return self.superIdentifier(methodNode, node, parent);
if (!(t.isMemberExpression(parent) && !parent.computed && parent.property === node)) {
throw self.file.errorWithNode(node, "illegal use of bare super");
}
} else if (t.isCallExpression(node)) {
var callee = node.callee;
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
if (t.isIdentifier(callee) && callee.name === "super") {
// super(); -> _get(Object.getPrototypeOf(ClassName), "MethodName", this).call(this);
property = methodNode.key;
computed = methodNode.computed;
args = node.arguments;
} else {
if (!t.isMemberExpression(callee)) return;
if (callee.object.name !== "super") return;
// super.test(); -> ClassName.prototype.MethodName.call(this);
t.appendToMemberExpression(callee, t.identifier("call"));
node.arguments.unshift(t.thisExpression());
// super.test(); -> _get(Object.getPrototypeOf(ClassName.prototype), "test", this).call(this);
property = callee.property;
computed = callee.computed;
args = node.arguments;
}
} else if (t.isMemberExpression(node)) {
if (!t.isIdentifier(node.object, { name: "super" })) return;
// super.name; -> _get(Object.getPrototypeOf(ClassName.prototype), "name", this);
property = node.property;
computed = node.computed;
}
if (property) {
var superProperty = self.superProperty(property, methodNode.static, computed);
if (args) {
return t.callExpression(
t.memberExpression(superProperty, t.identifier("call"), false),
[t.thisExpression()].concat(args)
);
} else {
return superProperty;
}
}
}
});

View File

@@ -1,7 +1,7 @@
{
"name": "6to5",
"description": "Turn ES6 code into readable vanilla ES5 with source maps",
"version": "2.8.2",
"version": "2.9.2",
"author": "Sebastian McKenzie <sebmck@gmail.com>",
"homepage": "https://github.com/6to5/6to5",
"repository": {
@@ -39,7 +39,7 @@
"test": "make test"
},
"dependencies": {
"acorn-6to5": "0.11.1-11",
"acorn-6to5": "0.11.1-12",
"ast-types": "~0.6.1",
"chokidar": "0.11.1",
"commander": "2.5.0",

View File

@@ -1,5 +1,3 @@
if (!process.env.ALL_6TO5_TESTS) return;
require("./_transformation-helper")({
name: "esnext"
});

View File

@@ -1,9 +1,11 @@
class Animal {
class Base {
get sound() {
return 'I am a ' + this.type + '.';
}
}
class Animal extends Base {}
class Cat extends Animal {
get type() { return 'cat'; }

View File

@@ -3,7 +3,6 @@ class Test extends Foo {
woops.super.test();
super();
super.test();
foob(super);
super(...arguments);
super("test", ...arguments);

View File

@@ -1,6 +1,28 @@
"use strict";
var _slice = Array.prototype.slice;
var _get = function get(object, property, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc && desc.writable) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var _inherits = function (child, parent) {
if (typeof parent !== "function" && parent !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof parent);
@@ -19,34 +41,34 @@ var _inherits = function (child, parent) {
var Test = (function () {
var _Foo = Foo;
var Test = function Test() {
var _Foo$prototype$test, _Foo$prototype$test2;
var _get2, _get3, _get4, _get5;
woops["super"].test();
_Foo.call(this);
_Foo.prototype.test.call(this);
foob(_Foo);
_get(Object.getPrototypeOf(Test.prototype), "constructor", this).call(this);
_get(Object.getPrototypeOf(Test.prototype), "test", this).call(this);
_Foo.call.apply(_Foo, [this].concat(_slice.call(arguments)));
_Foo.call.apply(_Foo, [this, "test"].concat(_slice.call(arguments)));
(_get2 = _get(Object.getPrototypeOf(Test.prototype), "constructor", this)).call.apply(_get2, [this].concat(_slice.call(arguments)));
(_get3 = _get(Object.getPrototypeOf(Test.prototype), "constructor", this)).call.apply(_get3, [this, "test"].concat(_slice.call(arguments)));
(_Foo$prototype$test = _Foo.prototype.test).call.apply(_Foo$prototype$test, [this].concat(_slice.call(arguments)));
(_Foo$prototype$test2 = _Foo.prototype.test).call.apply(_Foo$prototype$test2, [this, "test"].concat(_slice.call(arguments)));
(_get4 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get4, [this].concat(_slice.call(arguments)));
(_get5 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get5, [this, "test"].concat(_slice.call(arguments)));
};
_inherits(Test, _Foo);
Test.prototype.test = function () {
var _Foo$prototype$test3, _Foo$prototype$test4;
_Foo.prototype.test.call(this);
(_Foo$prototype$test3 = _Foo.prototype.test).call.apply(_Foo$prototype$test3, [this].concat(_slice.call(arguments)));
(_Foo$prototype$test4 = _Foo.prototype.test).call.apply(_Foo$prototype$test4, [this, "test"].concat(_slice.call(arguments)));
var _get6, _get7;
_get(Object.getPrototypeOf(Test.prototype), "test", this).call(this);
(_get6 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get6, [this].concat(_slice.call(arguments)));
(_get7 = _get(Object.getPrototypeOf(Test.prototype), "test", this)).call.apply(_get7, [this, "test"].concat(_slice.call(arguments)));
};
Test.foo = function () {
var _Foo$foo, _Foo$foo2;
_Foo.foo.call(this);
(_Foo$foo = _Foo.foo).call.apply(_Foo$foo, [this].concat(_slice.call(arguments)));
(_Foo$foo2 = _Foo.foo).call.apply(_Foo$foo2, [this, "test"].concat(_slice.call(arguments)));
var _get8, _get9;
_get(Object.getPrototypeOf(Test), "foo", this).call(this);
(_get8 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get8, [this].concat(_slice.call(arguments)));
(_get9 = _get(Object.getPrototypeOf(Test), "foo", this)).call.apply(_get9, [this, "test"].concat(_slice.call(arguments)));
};
return Test;
})();

View File

@@ -1,5 +1,27 @@
"use strict";
var _get = function get(object, property, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc && desc.writable) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var _inherits = function (child, parent) {
if (typeof parent !== "function" && parent !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof parent);
@@ -18,11 +40,12 @@ var _inherits = function (child, parent) {
var Test = (function () {
var _Foo = Foo;
var Test = function Test() {
_Foo.prototype.test;
_Foo.prototype.test.whatever;
_get(Object.getPrototypeOf(Test.prototype), "test", this);
_get(Object.getPrototypeOf(Test.prototype), "test", this).whatever;
};
_inherits(Test, _Foo);
return Test;
})();

View File

@@ -0,0 +1,5 @@
class Test {
constructor() {
console.log(super);
}
}

View File

@@ -0,0 +1,3 @@
{
"throws": "illegal use of bare super"
}

View File

@@ -1,5 +1,27 @@
"use strict";
var _get = function get(object, property, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc && desc.writable) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var _inherits = function (child, parent) {
if (typeof parent !== "function" && parent !== null) {
throw new TypeError("Super expression must either be null or a function, not " + typeof parent);
@@ -18,15 +40,16 @@ var _inherits = function (child, parent) {
var Test = (function () {
var _Foo = Foo;
var Test = function Test() {
_Foo.prototype.test.whatever();
_Foo.prototype.test.call(this);
_get(Object.getPrototypeOf(Test.prototype), "test", this).whatever();
_get(Object.getPrototypeOf(Test.prototype), "test", this).call(this);
};
_inherits(Test, _Foo);
Test.test = function () {
return _Foo.wow.call(this);
return _get(Object.getPrototypeOf(Test), "wow", this).call(this);
};
return Test;
})();

View File

@@ -18,8 +18,8 @@ var _inherits = function (child, parent) {
var BaseController = (function () {
var _Chaplin$Controller = Chaplin.Controller;
var BaseController = function BaseController() {
if (_Chaplin$Controller !== null) {
_Chaplin$Controller.apply(this, arguments);
if (Object.getPrototypeOf(BaseController) !== null) {
Object.getPrototypeOf(BaseController).apply(this, arguments);
}
};
@@ -31,8 +31,8 @@ var BaseController = (function () {
var BaseController2 = (function () {
var _Chaplin$Controller$Another = Chaplin.Controller.Another;
var BaseController2 = function BaseController2() {
if (_Chaplin$Controller$Another !== null) {
_Chaplin$Controller$Another.apply(this, arguments);
if (Object.getPrototypeOf(BaseController2) !== null) {
Object.getPrototypeOf(BaseController2).apply(this, arguments);
}
};

View File

@@ -18,8 +18,8 @@ var _inherits = function (child, parent) {
var Test = (function () {
var _Foo = Foo;
var Test = function Test() {
if (_Foo !== null) {
_Foo.apply(this, arguments);
if (Object.getPrototypeOf(Test) !== null) {
Object.getPrototypeOf(Test).apply(this, arguments);
}
};

View File

@@ -1,5 +1,28 @@
"use strict";
var Test = function Test() {
Function.prototype.hasOwnProperty.call(this, "test");
var _get = function get(object, property, receiver) {
var desc = Object.getOwnPropertyDescriptor(object, property);
if (desc === undefined) {
var parent = Object.getPrototypeOf(object);
if (parent === null) {
return undefined;
} else {
return get(parent, property, receiver);
}
} else if ("value" in desc && desc.writable) {
return desc.value;
} else {
var getter = desc.get;
if (getter === undefined) {
return undefined;
}
return getter.call(receiver);
}
};
var Test = function Test() {
_get(Object.getPrototypeOf(Test.prototype), "hasOwnProperty", this).call(this, "test");
};

View File

@@ -7,7 +7,7 @@ define(["exports", "foo"], function (exports, _foo) {
};
};
var _exportsWildcard = function (obj) {
var _exportsWildcard = function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];
@@ -15,7 +15,7 @@ define(["exports", "foo"], function (exports, _foo) {
}
};
_exportsWildcard(_interopRequireWildcard(_foo));
_exportsWildcard(_interopRequireWildcard(_foo), exports);
exports.foo = _foo.foo;
exports.foo = _foo.foo;

View File

@@ -7,7 +7,7 @@ define(["exports", "foo"], function (exports, _foo) {
};
};
var _exportsWildcard = function (obj) {
var _exportsWildcard = function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];
@@ -15,7 +15,7 @@ define(["exports", "foo"], function (exports, _foo) {
}
};
_exportsWildcard(_interopRequireWildcard(_foo));
_exportsWildcard(_interopRequireWildcard(_foo), exports);
exports.foo = _foo.foo;
exports.foo = _foo.foo;

View File

@@ -6,7 +6,7 @@ var _interopRequireWildcard = function (obj) {
};
};
var _exportsWildcard = function (obj) {
var _exportsWildcard = function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];
@@ -14,7 +14,7 @@ var _exportsWildcard = function (obj) {
}
};
_exportsWildcard(_interopRequireWildcard(require("foo")));
_exportsWildcard(_interopRequireWildcard(require("foo")), exports);
exports.foo = require("foo").foo;
exports.foo = require("foo").foo;

View File

@@ -6,7 +6,7 @@ var _interopRequireWildcard = function (obj) {
};
};
var _exportsWildcard = function (obj) {
var _exportsWildcard = function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];
@@ -14,7 +14,7 @@ var _exportsWildcard = function (obj) {
}
};
_exportsWildcard(_interopRequireWildcard(require("foo")));
_exportsWildcard(_interopRequireWildcard(require("foo")), exports);
exports.foo = require("foo").foo;
exports.foo = require("foo").foo;

View File

@@ -13,7 +13,7 @@
};
};
var _exportsWildcard = function (obj) {
var _exportsWildcard = function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];
@@ -21,7 +21,7 @@
}
};
_exportsWildcard(_interopRequireWildcard(_foo));
_exportsWildcard(_interopRequireWildcard(_foo), exports);
exports.foo = _foo.foo;
exports.foo = _foo.foo;

View File

@@ -13,7 +13,7 @@
};
};
var _exportsWildcard = function (obj) {
var _exportsWildcard = function (obj, exports) {
for (var i in obj) {
if (exports[i] !== undefined) {
exports[i] = obj[i];
@@ -21,7 +21,7 @@
}
};
_exportsWildcard(_interopRequireWildcard(_foo));
_exportsWildcard(_interopRequireWildcard(_foo), exports);
exports.foo = _foo.foo;
exports.foo = _foo.foo;

View File

@@ -28,8 +28,8 @@ var _inherits = function (child, parent) {
var Foo = (function () {
var _Bar = Bar;
var Foo = function Foo() {
if (_Bar !== null) {
_Bar.apply(this, arguments);
if (Object.getPrototypeOf(Foo) !== null) {
Object.getPrototypeOf(Foo).apply(this, arguments);
}
};