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.
+59 −96
Diff settings

Always

Just for now

Viewing a subset of changes. View all

Add tests for evaluation order and fix transformation

  • Loading branch information...
peey committed Aug 7, 2017
commit d8cebbac0b21dd2d4e8a410cfa4cb7af43ebe658
@@ -42,9 +42,8 @@ export default function({ types: t }) {
if (!method.isClassMethod()) continue;
if (!node.decorators || !method.node.decorators.length) continue;
const decorators = method.node.decorators
.map(d => d.expression)
.reverse(); // reverse for correct evaluation order
const decorators = method.node.decorators.map(d => d.expression);
node.decorators = []; //TODO: should we remove using path methods? method.get("decorators") doesn't work
const entry = [];
@@ -75,8 +74,7 @@ export default function({ types: t }) {
// expects path of a ClassExpression
function takeClassDecorators(path) {
if (path.node.decorators && path.node.decorators.length) {
// reverse for correct decorator evaluation order
const decorators = path.node.decorators.map(d => d.expression).reverse();
const decorators = path.node.decorators.map(d => d.expression);
path.node.decorators = [];
return t.arrayExpression(decorators);
} else {
@@ -0,0 +1,27 @@
const calls = [];
function dec(id){
calls.push(id);
return function(descriptor) { return {descriptor, extras:[], finishers:[] };};
}
class Foo {
@dec(1)
@dec(2)
method1() {}
@dec(3)
@dec(4)
method2() {}
@dec(5)
@dec(6)
method3() {}
@dec(7)
@dec(8)
method4() {}
}
assert.deepEqual(calls, [1, 2, 3, 4, 5, 6, 7, 8]);
@@ -0,0 +1,28 @@
const calls = [];
function dec(id){
return function(descriptor) {
calls.push(id);
return {descriptor, extras:[], finishers:[] };
};
}
class Foo {
@dec(2)
@dec(1)
method1() {}
@dec(4)
@dec(3)
method2() {}
@dec(6)
@dec(5)
method3() {}
@dec(8)
@dec(7)
method4() {}
}
assert.deepEqual(calls, [1, 2, 3, 4, 5, 6, 7, 8]);
@@ -21,4 +21,4 @@ let Bizz = babelHelpers.decorate(class Bizz {
[calculated + and + undecorated]() {}
}, [[andAnUndecoratedMethod], [calculated + and + undecorated]], [["m1", [dec]], ["m2", [foo.bar(baz), bar]], [(_key = 3 + 7), [dec], true]], void 0)([decorator]);
}, [[andAnUndecoratedMethod], [calculated + and + undecorated]], [["m1", [dec]], ["m2", [bar, foo.bar(baz)]], [(_key = 3 + 7), [dec], true]], void 0)([decorator]);

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.