Giter VIP home page Giter VIP logo

Comments (1)

auvred avatar auvred commented on August 12, 2024

It's actually not that hard

const esquery = require("esquery");

function stringify(selector) {
  switch (selector.type) {
    case "wildcard":
      return "*";
    case "identifier":
      return selector.value;
    case "field":
      return "." + selector.name.split(".");
    case "matches":
      return ":matches(" + selector.selectors.map(stringify).join(", ") + ")";
    case "compound":
      return selector.selectors.map(stringify).join("");
    case "not":
      return ":not(" + selector.selectors.map(stringify).join(", ") + ")";
    case "has":
      return ":has(" + selector.selectors.map(stringify).join(", ") + ")";
    case "child":
      return stringify(selector.left) + " > " + stringify(selector.right);
    case "descendant":
      return stringify(selector.left) + " " + stringify(selector.right);
    case "attribute": {
      const parts = ["[", selector.name];
      if (selector.operator) {
        parts.push(selector.operator);
        if (selector.value.type === "regexp") {
          parts.push(selector.value.value.toString());
        } else if (selector.value.type === "literal") {
          parts.push(`${selector.value.value}`);
        } else if (selector.value.type === "type") {
          parts.push("type(" + selector.value.value + ")");
        }
      }
      return [...parts, "]"].join("");
    }
    case "sibling":
      return stringify(selector.left) + " ~ " + stringify(selector.right);
    case "adjacent":
      return stringify(selector.left) + " + " + stringify(selector.right);
    case "nth-child":
      return ":nth-child(" + selector.index.value + ")";
    case "nth-last-child":
      return ":nth-last-child(" + selector.index.value + ")";
    case "class":
      return ":" + selector.name;
  }
}

console.log(stringify(esquery.parse("aaa.bbb[ccc=111]:statement > eee:matches(* ~ ttt + ddd :not(ee, yy[a])), *:first-child")));
// :matches(aaa.bbb[ccc=111]:statement > eee:matches(* ~ ttt + ddd :not(ee, yy[a])), *:nth-child(1))

But it will replace top level matches with :matches and first-child/last-child with :nth-child(...). Also it will remove all extra spaces. (limitations of AST-based codegen)

from esquery.

Related Issues (20)

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.