Content-Length: 256102 | pFad | http://redirect.github.com/babel/babel/pull/16598

1C Add experimental support for source map scopes by nicolo-ribaudo · Pull Request #16598 · babel/babel · GitHub
Skip to content
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

Add experimental support for source map scopes #16598

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nicolo-ribaudo
Copy link
Member

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

I'm trying to implement https://github.com/tc39/source-map/blob/main/proposals/scopes.md in Babel. This is super experimental, but so far we got a demo working with Babel + Chrome Devtools using this plugin:

const input = `
function inlineMe() {
  let num = 3;
  beforeI;
  console.log("Hello from inside!", num);
  afterI;
}

function fn() {
  beforeF;
  inlineMe();
  afterF;
}

fn(1);
`.trim();

const res = babel.transformSync(input, {
  configFile: false,
  sourceMaps: true,
  plugins: [
    await load("plugin-experimental-source-map-scopes"),
    /**
     * @param {core.PluginAPI} param0
     * @return {core.PluginObject}
     */
    function ({ types: t }) {
      return {
        visitor: {
          FunctionDeclaration(path) {
            //path.scope.rename("param", "p1");
          },
          // Do some inlining
          CallExpression(path) {
            if (path.get("callee").isIdentifier({ name: "inlineMe" })) {
              /** @type {core.NodePath<core.types.FunctionDeclaration>} */
              const fn = path.scope.getBinding("inlineMe").path;

              const argsNames = [];
              const argsDecls = [];
              path.get("arguments").forEach((arg, i) => {
                arg.assertExpression();
                const tmpId = path.scope.generateUidIdentifierBasedOnNode(
                  arg.node,
                  `arg${i}`
                );
                //tmpId.loc = arg.node.loc;
                argsNames.push(tmpId.name);
                argsDecls.push(t.variableDeclarator(tmpId, arg.node));
              });

              fn.get("params").forEach((param, i) => {
                param.assertIdentifier();
                fn.scope.rename(param.node.name, argsNames[i]);
              });

              const fnBody = fn.node.body.body;

              const scopeInfo = this.file.opts.generatorOpts.getScopeInfo?.(
                fnBody[0]
              );
              if (scopeInfo) {
                scopeInfo.callsite = {
                  column: path.node.loc.start.column,
                  line: path.node.loc.start.line,
                  source: "unknown",
                };
                scopeInfo.isScope = false;
              }

              fn.remove();

              path.replaceWithMultiple(
                [
                  argsDecls.length > 0 &&
                    t.variableDeclaration("let", argsDecls),
                  ...fnBody,
                ].filter(Boolean)
              );
            }
          },
        },
      };
    },
  ],
  ast: true,
});

@nicolo-ribaudo nicolo-ribaudo marked this pull request as draft June 28, 2024 12:37
@babel-bot
Copy link
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/57221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://redirect.github.com/babel/babel/pull/16598

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy