mainmatter / ast-workshop Goto Github PK
View Code? Open in Web Editor NEW"Abstract Syntax Forestry" workshop for EmberConf 2020
"Abstract Syntax Forestry" workshop for EmberConf 2020
Hey @Turbo87, thanks for putting together the workshop in written form. Loving it! Do you have the solution for 05b-strip-test-selectors
?
I have the following code in ember-cli-build.js
, but I still see data-test-***
attributes in the DOM when I npm start
, which I restarted.
class StripTestSelectorsTransform {
transform(root) {
this.syntax.traverse(root, {
// TODO write your implementation here
AttrNode(node) {
if (node.name.startsWith('data-test-')) {
return null;
}
}
});
return root;
}
}
Your course is amazing! I noticed one thing about setting the attr to be children, but I'm curious if it could go the other way? If so, how would you do it? I've been thinking about trying to use builder to do it but haven't been able to set it within text
node.attributes.push(builder.attr("@text", node.children));
Example of how I've done it but it doesn't feel like it's using the builder docs https://astexplorer.net/#/gist/2330f08af1d8a9755f9d6c75c7ab9d9b/a21152396895fe606f2c09d9a6799f072eb8a3c1
const HEADING_LIST = ["h1", "h2", "h3", "h4", "h5", "h6"];
const ATTR_MAP = {
class: "className"
};
function handleMustacheStatementParams(param) {
if (param.type === "StringLiteral") {
return `"${param.original}"`;
} else {
// todo: handle block statements
// todo: handle SubExpression type
return param.original;
}
}
function handleMustacheStatementHashPairs(pair) {
if (pair.type === "HashPair") {
// eg without content bit: {{pluralize @count @nounLabel without-count=true}}
return `${pair.key}=${pair.value.value}`;
}
// not sure of other hash pairs
}
function getOriginalChildValue(child, builder) {
// todo: handle element node being a child. Will likely need to eject from the codemod there
if (child.type === "TextNode") {
return child.chars.trim();
} else if (child.type === "MustacheStatement") {
return builder;
let paramsString = "";
let hashString = "";
if (child.params.length > 0) {
// going to have to process potentially more params
// see handleMustacheStatementParams
// may not necessarily be a string here
paramsString = child.params
.map((param) => {
return ` ${handleMustacheStatementParams(param)}`;
})
.join("");
}
if (child.hash.pairs.length > 0) {
hashString = child.hash.pairs
.map((pair) => {
return ` ${handleMustacheStatementHashPairs(pair)}`;
})
.join("");
}
// handling just a single path with or without a param string
return `{{${child.path.original}${paramsString}${hashString}}}`;
}
}
function getChildren(node, builder) {
return node.children.reduce(
(accumulator, child) => accumulator + getOriginalChildValue(child, builder),
""
);
}
module.exports = (env) => {
let builder = env.syntax.builders;
return {
ElementNode(node) {
if (node.type === "ElementNode" && HEADING_LIST.includes(node.tag)) {
// get the node tag name for setting hierarchy with as
// better than just setting everything as h1 by default
const nodeTag = node.tag;
node.tag = "Ds::Title";
// iterate through each attribute and set to @ to set within the react component
node.attributes.forEach((attr) => {
attr.name = `@${ATTR_MAP[attr.name] || attr.name}`;
});
// replace the text prop with the children
// have tried using builder.mustache and builder.hash here to handle specific types
node.attributes.push(builder.attr("@text", builder.text(getChildren(node, builder))));
// take in attribute with @as
node.attributes.push(builder.attr("@as", builder.text(nodeTag)));
// remove the children
node.children = [];
}
}
};
};
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.