Skip to content
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

Migrate class-based to a function based AST plugin for Ember 4.x #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 49 additions & 54 deletions lib/bind-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,77 +25,72 @@
```
*/

module.exports = class BindTransform {
constructor() {
this.syntax = null;
}
function transformNode(node, builders) {
return addTarget(node, builders);
}

get builders() {
return this.syntax.builders;
}
function addTarget(node, builders) {
if (node.path.original === "bind" && node.params.length > 0) {
let hasTarget = node.hash.pairs.some(p => p.key === "target");

transform(ast) {
let transform = node => this.transformNode(node);
this.syntax.traverse(ast, {
SubExpression: transform,
MustacheStatement: transform
});
if (!hasTarget) {
let target = getDefaultTarget(node.params[0], builders);

return ast;
node.hash.pairs.push(builders.pair("target", target));
}
}
}

function getDefaultTarget(fn, builders) {

transformNode(node) {
this.addTarget(node);
if (fn.type !== "PathExpression") {
return builders.null();
}

addTarget(node) {
if (node.path.original === "bind" && node.params.length > 0) {
let hasTarget = node.hash.pairs.some(p => p.key === "target");
switch (fn.parts.length) {
case 0:
return builders.null();
case 1:
return builders.path("this");
default: {
let parts = fn.parts.slice(0, -1); // Remove function part

if (!hasTarget) {
let target = this.getDefaultTarget(node.params[0]);
let lastPart = parts[parts.length - 1];

node.hash.pairs.push(this.builders.pair("target", target));
// Remove actions part
if (lastPart === "actions") {
parts.pop();
}
}
}

getDefaultTarget(fn) {
let { builders } = this;
if (parts.length === 0) {
return builders.path("this");
}

if (fn.type !== "PathExpression") {
return builders.null();
return pathFor(parts.join("."), fn.data, builders);
}
}
}

switch (fn.parts.length) {
case 0:
return builders.null();
case 1:
return builders.path("this");
default: {
let parts = fn.parts.slice(0, -1); // Remove function part
function pathFor(completePath, isData = false /* named argument */, builders) {
let path = builders.path(completePath);

let lastPart = parts[parts.length - 1];
path.data = isData;

// Remove actions part
if (lastPart === "actions") {
parts.pop();
}
return path;
}

if (parts.length === 0) {
return builders.path("this");
}
function bindHelperSyntaxPlugin(env) {
let builders = env.syntax.builders;

return this.pathFor(parts.join("."), fn.data);
}
}
}
let transform = node => transformNode(node, builders);

pathFor(completePath, isData = false /* named argument */) {
let path = this.builders.path(completePath);

path.data = isData;
return {
name: 'ember-bind-helper',
visitor: {
SubExpression: transform,
MustacheStatement: transform
}
};
}

return path;
}
};
module.exports = bindHelperSyntaxPlugin;
Loading