Skip to content

Instantly share code, notes, and snippets.

@stephencookdev
stephencookdev / simple-babel-plugin.js
Last active April 24, 2020 14:46
Example of a simple Babel plugin
module.exports = function () {
const SimpleVisitor = {
StringLiteral(path, state) {
if (path.node.value === "We'll never survive!") {
path.node.value = "Nonsense. You're only saying that because no one ever has.";
}
},
};
return { visitor: SimpleVisitor };
@stephencookdev
stephencookdev / grandma-visitor-initiator.js
Created April 24, 2020 15:15
A look at the initiator visitor of grandmas-own-jsx
module.exports = function ({ types: t }) {
const GrandmaVisitorInitiator = {
Program(path) {
const commentLineTokens = path.parent.comments.filter(
(token) => token.type === "CommentLine"
);
const commentBlockTokens = path.parent.comments.filter(
(token) => token.type === "CommentBlock"
);
@stephencookdev
stephencookdev / grandma-visitor.js
Last active April 24, 2020 16:03
A look at the main visitor of grandmas-own-jsx
module.exports = function ({ types: t }) {
const GrandmaVisitor = {
StringLiteral(path, state) {
if (path.node.value === "👵") {
const recipeRef = state.grandmasRecipes[path.node.loc.start.line];
const recipeMatches = recipeRef && recipeRef.start > path.node.start;
if (recipeMatches) {
const recipe = recipeRef.value;
const domStruc = cookRecipe(recipe, state.grandmasReference);
@stephencookdev
stephencookdev / gen-type-expression.js
Created April 24, 2020 16:23
A look at the genTypeExpression function of grandma-visitor-jsx
module.exports = function ({ types: t }) {
const genTypeExpression = (node) => {
return t.callExpression(
t.memberExpression(t.identifier("React"), t.identifier("createElement")),
[
/^[A-Z]/.test(node.type[0])
? t.identifier(node.type)
: t.stringLiteral(node.type),
t.objectExpression(node.args),
...node.children.map(genTypeExpression),