babel / sandboxes

Babel repl-like codesandbox: check out link =>
https://codesandbox.io/s/github/babel/sandboxes
39 stars 12 forks source link

[plugins] Ability to author a plugin within the code sandbox. #15

Closed ianjennings closed 4 years ago

ianjennings commented 4 years ago

Writing a Plugin

Matcher?

const matcher = astMatcher('__any.method(__str_foo, [__arr_opts])')

https://github.com/dumberjs/ast-matcher

const isRequire = matches({
    "type": "VariableDeclaration",
    "declarations": (decs) => decs.length === 1 && isRequireDeclarator(decs[0]),
    "kind": "var"
});

http://nene.github.io/2016/04/02/matches-ast

{
  FunctionExpression(path: NodePath<t.FunctionExpression>): void {
    const argumentNameMatcher = m.capture(m.anyString())
    const matcher = m.functionExpression(
      m.anything(),
      [m.identifier(argumentNameMatcher)],
      m.blockStatement([
        m.returnStatement(m.fromCapture(m.identifier(argumentNameMatcher))),
      ])
    )

    if (matcher.match(path.node)) {
      path.replaceWith(t.identifier('IDENTITY'))
    }
  },
};

https://github.com/codemod-js/codemod/blob/master/packages/matchers/README.md

https://blog.scottlogic.com/2016/06/22/xslt-inspired-ast-transforms.html

ianjennings commented 4 years ago

Kamino closed and cloned this issue to MLH-Fellowship/babel-sandbox