New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decorators 2 Transform [WIP] #6107

Closed
wants to merge 23 commits into
base: master
from
Commits
Jump to file or symbol
Failed to load files and symbols.
+74 −13
Diff settings

Always

Just for now

Viewing a subset of changes. View all

Clean up code

  • Loading branch information...
peey committed Jul 31, 2017
commit 10d763e3b98e18462e368de4424f52d861736baf
@@ -270,6 +270,8 @@ helpers.createClass = template(`
})()
`);
//NOTE: convention is 'descriptor' is used for element descriptors, while 'propertyDescriptor' is used for
//property descriptor in all helpers related to decorators-2
helpers.decorate = template(`
(function (constructor, undecorated, memberDecorators, heritage) {
const prototype = constructor.prototype;
@@ -1,17 +1,10 @@
import template from "babel-template";
import syntaxDecorators2 from "babel-plugin-syntax-decorators-2";
//TODO: will have to check for dot (.) access to reserved keywords in decorator members
//NOTE: convention is 'descriptor' is used for element descriptors, while 'propertyDescriptor' is used for property descriptor
//TODO: check if we need the 'define' + 'Property' and 'ke' + 'ys' hacks as seen in
//https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy/blob/master/src/index.js#L69
/** manual testing code
class A { method() {console.log("method exec hua");} static estatic() {console.log("estatic hua");}}
function methDec(descriptor) {console.log("methDec hua", arguments); return {descriptor, extras: [], finishers: []}}
function methDec2(descriptor) {console.log("methDec2 hua", arguments); return {descriptor, extras: [], finishers: []}}
function classDec(constructor, heritage, memberDescriptors) {console.log("class hua", arguments); return {constructor, elements: memberDescriptors, finishers: []}}
decorate(A, [], [["method", [methDec, methDec2]], ["estatic", [methDec], true]])([classDec])
**/
export default function({ types: t }) {
// converts [(expression)] to [(let key = (expression))] if expression is impure
@@ -29,7 +22,8 @@ export default function({ types: t }) {
const replacement = t.sequenceExpression([
t.assignmentExpression("=", ref, member.node.key),
]);

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Aug 16, 2017

Member

Why is a SequenceExpression needed if there is only one expression?

@nicolo-ribaudo

nicolo-ribaudo Aug 16, 2017

Member

Why is a SequenceExpression needed if there is only one expression?

//member.get("key").replaceWith(replacement); FIXME: should work, but doesn't accept sequenceexpression type
// FIXME: the following should work: member.get("key").replaceWith(replacement);
// but doesn't accept sequenceexpression type so we have to directly manipulate node
member.node.key = replacement;
}
}
@@ -51,7 +45,7 @@ export default function({ types: t }) {
const decorators = method.node.decorators
.map(d => d.expression)
.reverse(); // reverse for correct evaluation order
node.decorators = []; //TODO: should we remove from path? method.get("decorators") doesn't work
node.decorators = []; //TODO: should we remove using path methods? method.get("decorators") doesn't work
const entry = [];
if (node.computed) {
@@ -80,10 +74,14 @@ export default function({ types: t }) {
// expects path of a ClassExpression
function takeClassDecorators(path) {
// reverse for correct decorator evaluation order
const decorators = path.node.decorators.map(d => d.expression).reverse();
path.node.decorators = [];
return t.arrayExpression(decorators);
if (path.node.decorators && path.node.decorators.length) {
// reverse for correct decorator evaluation order
const decorators = path.node.decorators.map(d => d.expression).reverse();
path.node.decorators = [];
return t.arrayExpression(decorators);
} else {
return t.arrayExpression();
}
}
function undecoratedMethods(path) {
@@ -0,0 +1,24 @@
class A {
@methDec @methDec2 method() {
console.log("method executed");
}
@methDec static estatic() {
console.log("estatic executed");
}
}
function methDec(descriptor) {
console.log("methDec executed", arguments);
return {descriptor, extras: [], finishers: []}
}
function methDec2(descriptor) {
console.log("methDec2 executed", arguments);
return {descriptor, extras: [], finishers: []}
}
function classDec(constructor, heritage, memberDescriptors) {
console.log("class executed", arguments);
return {constructor, elements: memberDescriptors, finishers: []}
}
@@ -0,0 +1,37 @@
let A = babelHelpers.decorate(class A {
method() {
console.log("method executed");
}
static estatic() {
console.log("estatic executed");
}
}, [], [["method", [methDec2, methDec]], ["estatic", [methDec], true]], void 0)([]);
function methDec(descriptor) {
console.log("methDec executed", arguments);
return {
descriptor,
extras: [],
finishers: []
};
}
function methDec2(descriptor) {
console.log("methDec2 executed", arguments);
return {
descriptor,
extras: [],
finishers: []
};
}
function classDec(constructor, heritage, memberDescriptors) {
console.log("class executed", arguments);
return {
constructor,
elements: memberDescriptors,
finishers: []
};
}
ProTip! Use n and p to navigate between commits in a pull request.