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

Transform calls to `babel-template` at compile time #7171

Open
nicolo-ribaudo opened this Issue Jan 7, 2018 · 1 comment

Comments

Projects
None yet
4 participants
@nicolo-ribaudo
Member

nicolo-ribaudo commented Jan 7, 2018

Templates take away from the developer the responisbility of manually cloning nodes nodes, so they should be encouraged.
They are slower than using the builders provided by babel-types, so we could make a plugin that transforms them.

e.g.

// Input
const buildEnumAssignment = template(`
  ID[ID[NAME] = VALUE] = NAME;
`);

// Output
const buildEnumAssignment = ({ ID, NAME, VALUE }) =>
  t.expressionStatement(
    t.assignmentExpression(
      "=",
      t.memberExpression(
        ID,
        t.assignmentExpression(
          "=",
          t.memberExpression(t.cloneNode(ID), NAME, true)
          VALUE
        ),
        true
      ),
      t.cloneNode(NAME)
    )
  );

I don't know if this is actually possible 🤔

@loganfsmyth

This comment has been minimized.

Show comment
Hide comment
@loganfsmyth

loganfsmyth Jan 7, 2018

Member

I think I'd want to see some benchmarks to see how much of an improvement this would actually be. In Babel 6 it was definitely slower because we re-traversed each template every time a template function was called, but in Babel 7 we only traverse once up front to decide what to replace, and the only time spent after that is cloning the AST and doing the replacement, which shouldn't be too much slower than using t.XXX directly, I'd hope.

Member

loganfsmyth commented Jan 7, 2018

I think I'd want to see some benchmarks to see how much of an improvement this would actually be. In Babel 6 it was definitely slower because we re-traversed each template every time a template function was called, but in Babel 7 we only traverse once up front to decide what to replace, and the only time spent after that is cloning the AST and doing the replacement, which shouldn't be too much slower than using t.XXX directly, I'd hope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment