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.
+16 −14
Diff settings

Always

Just for now

Viewing a subset of changes. View all

use path.scope.parent.push & other review suggestions

  • Loading branch information...
peey committed Aug 20, 2017
commit 8e28ca7e49240644d913190661710cd009c2db38
@@ -9,7 +9,6 @@ import syntaxDecorators2 from "babel-plugin-syntax-decorators-2";
export default function({ types: t }) {
// converts [(expression)] to [(let key = (expression))] if expression is impure
// so as to avoid recomputation when the key is needed later
let injectedKeyDeclaration;
function addKeysToComputedMembers(path) {
const body = path.get("body.body");
@@ -19,17 +18,7 @@ export default function({ types: t }) {
member.node.key,
);

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Aug 16, 2017

Member

(Lines 17-21)

Instead of searching the parent block and then adding let key, you can use member.scope.parent.push which does that for you.

if (!injectedKeyDeclaration) {
const parent = path.getStatementParent();
parent.insertBefore(t.variableDeclaration("let", []));
injectedKeyDeclaration = parent.getSibling(parent.key - 1);
}
injectedKeyDeclaration.pushContainer(
"declarations",
t.variableDeclarator(ref),
);
path.scope.parent.push({ id: ref });
const replacement = t.assignmentExpression("=", ref, member.node.key);
member.get("key").replaceWith(replacement);
}
@@ -63,7 +52,7 @@ export default function({ types: t }) {
return node.key;
}
} else {
return t.stringLiteral(node.key.name);
return t.stringLiteral(node.key.name || String(node.key.value));
}
}
@@ -175,6 +164,7 @@ export default function({ types: t }) {
const decorateIdentifier = file.addHelper("decorate", [
"decorateClass",
"decorateElement",
"makeElementDescriptor",
]);

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Aug 16, 2017

Member

decorate should have makeElementDescriptor as a dependency.

This comment has been minimized.

@nicolo-ribaudo

nicolo-ribaudo Aug 16, 2017

Member

Also probably dependencies should be declared in the helper definition.

Something like

helpers.myHelper = template("...");
helpers.myHelper.dependencies = ["foo", "bar"];
addKeysToComputedMembers(path);
@@ -1,4 +1,4 @@
let _foo, _ref;
var _foo, _ref;
let Bizz = babelHelpers.decorate(class Bizz {
uncomputed() {}
@@ -0,0 +1,4 @@
@dec class Foo {
3 () {}
"yo" () {}
}
@@ -0,0 +1,6 @@
let Foo = babelHelpers.decorate(class Foo {
3() {}
"yo"() {}
}, [["3"], ["yo"]], [], void 0)([dec]);
@@ -1,3 +1,5 @@
var _ref, _foo;
let Bizz = babelHelpers.decorate(class Bizz {
m1() {}
ProTip! Use n and p to navigate between commits in a pull request.