Skip to content

Commit

Permalink
Merge pull request #38 from Lazy-work/compiler/fix_return_handling_an…
Browse files Browse the repository at this point in the history
…d_simplify_nested_scope_skipping

fix(compiler): fix component return handling
  • Loading branch information
abdullah-wn authored Feb 1, 2025
2 parents efdfe76 + 91b7631 commit 8ad48bf
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 75 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-mayflies-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'babel-plugin-unisonjs-compiler': patch
---

Fix component return handling and simplify nested scope skipping
94 changes: 19 additions & 75 deletions packages/compiler/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,17 @@ export default function (babel, opts = {}) {
};

const isDirtyTraverse = {
'ArrowFunctionExpression|FunctionExpression': {
enter(path) {
if (!this.ignoredBlock) {
this.ignoredBlock = path.node;
this.ignore = true;
}
},
exit(path) {
if (this.ignoredBlock === path.node) {
this.ignoredBlock = null;
this.ignore = false;
}
},
},
JSXExpressionContainer: {
enter(path) {
if (this.toInspect.includes(path.node)) {
this.observe = true;
}
},
exit(path) {
if (this.toInspect.includes(path.node)) {
this.observe = false;
}
},
'ArrowFunctionExpression|FunctionExpression'(path) {
path.skip();
},
MemberExpression(path) {
if (this.observe && !this.ignore) {
this.dirty();
path.stop();
JSXExpressionContainer(path) {
if (!this.toInspect.includes(path.node)) {
path.skip();
}
},
CallExpression(path) {
if (this.observe && !this.ignore) {
this.dirty();
path.stop();
}
'MemberExpression|CallExpression'(path) {
this.dirty();
path.stop();
},
};

Expand Down Expand Up @@ -126,23 +100,11 @@ export default function (babel, opts = {}) {
}

const optimizeStatic = {
'ArrowFunctionExpression|FunctionExpression': {
enter(path) {
if (!this.ignoredBlock) {
this.ignoredBlock = path.node;
this.ignore = true;
}
},
exit(path) {
if (this.ignoredBlock === path.node) {
this.ignoredBlock = null;
this.ignore = false;
}
},
'ArrowFunctionExpression|FunctionExpression'(path) {
path.skip();
},
'JSXElement|JSXFragment': {
enter(path) {
if (this.ignore) return;
if (!this.dirtiness.has(path.node)) this.dirtiness.set(path.node, false);

const parent = this.parents[this.parents.length - 1];
Expand All @@ -156,7 +118,6 @@ export default function (babel, opts = {}) {
this.parents.push(path.node);
},
exit(path) {
if (this.ignore) return;
if (path.node === this.parents.at(-1)) {
this.parents.length--;
}
Expand All @@ -172,18 +133,13 @@ export default function (babel, opts = {}) {

let rsxIdentifier;
const breakDownReturn = {
'FunctionExpression|ArrowFunctionExpression'(path) {
path.skip()
},
JSXElement: {
exit(path) {
const container = path.findParent((path) => path.isJSXExpressionContainer());
if (container) {
const isInFunctionCall = path
.findParent((path) => path.node === container.node || path.isCallExpression())
.isCallExpression();
if (isInFunctionCall) return;
}
const props = path.get('openingElement');
props.traverse(optimizeFnProp, {
types: t,
seen: this.seen,
componentReturn: this.componentReturn,
});
Expand All @@ -204,29 +160,19 @@ export default function (babel, opts = {}) {
};

const findReturns = {
'ArrowFunctionExpression|FunctionExpression|FunctionDeclaration': {
enter(path) {
if (!this.ignoredBlock) {
this.ignoredBlock = path.node;
this.ignore = true;
}
},
exit(path) {
if (this.ignoredBlock === path.node) {
this.ignoredBlock = null;
this.ignore = false;
}
},
'ArrowFunctionExpression|FunctionExpression|FunctionDeclaration'(path) {
path.skip();
},
ReturnStatement(path) {
if (this.ignore) return;
this.returns.push(path);
},
};

const handleUnisonComponent = {
'FunctionDeclaration|FunctionExpression|ArrowFunctionExpression'(path) {
path.skip()
},
ReturnStatement(path) {
const t = this.types;
const argument = path.get('argument');
if (this.componentReturn.node === path.node && isJSX(argument.node)) {
argument.replaceWith(t.arrowFunctionExpression([], argument.node));
Expand Down Expand Up @@ -266,11 +212,9 @@ export default function (babel, opts = {}) {
};

function optimizeComponent(componentBody) {
componentBody.findParent((path) => path.isFunctionDeclaration() || path.isVariableDeclaration()).node;
const returnIndex = componentBody.node.body.findIndex((item) => t.isReturnStatement(item));
const componentReturn = componentBody.get(`body.${returnIndex}`);
componentBody.traverse(handleUnisonComponent, {
types: t,
componentReturn,
});
}
Expand Down Expand Up @@ -410,7 +354,7 @@ export default function (babel, opts = {}) {
if (noUnison(path.node.directives)) {
path.stop();
return;
};
}
program = path;
let unisonImported = false;
path.traverse(hasUnisonVisitor, { hasUnison: () => (unisonImported = true) });
Expand Down

0 comments on commit 8ad48bf

Please sign in to comment.