GithubHelp home page GithubHelp logo

ast-workshop's People

Contributors

bobrimperator avatar turbo87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ast-workshop's Issues

Solution to 05b-strip-test-selectors

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;
    }
  }

Is it possible to set an attribute to children?

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 = [];
      }
    }
  };
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.