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 plugins name #8769

Merged
merged 3 commits into from Nov 18, 2018
Merged

Add plugins name #8769

merged 3 commits into from Nov 18, 2018

Conversation

nicolo-ribaudo
Copy link
Member

I recall that they where needed for some repl feature (probably time travel).

Do you prefer them to be named transform-classes or @babel/plugin-transform-classes?

I generated them using this script
"use strict";

const recast = require("recast");
const babel = require("@babel/core");
const fs = require("fs");
const path = require("path");

const dirs = fs
  .readdirSync("./packages/")
  .filter(/./.test.bind(/babel-plugin-(?:proposal|transform|syntax)/));

const notUpdated = [];

for (const dir of dirs) {
  try {
    const name = dir.replace("babel-plugin-", "");
    const filename = path.resolve(`./packages/${dir}/src/index.js`);
    let content = fs.readFileSync(filename, "utf-8");
    content = babelRecast(
      content,
      { filename },
      {
        configFile: false,
        plugins: [[addName, { name }]],
      }
    );
    fs.writeFileSync(filename, content);
  } catch (e) {
    notUpdated.push([dir, e]);
  }
}

console.log(notUpdated);

function addName({ types: t }, { name }) {
  const ok = new WeakSet();

  function hasProp(node, name) {
    return node.properties.some(prop => t.isIdentifier(prop.key, { name }));
  }

  function program(path) {
    return path.find(p => p.isProgram());
  }

  return {
    visitor: {
      ObjectExpression(path) {
        if (
          !hasProp(path.node, "visitor") &&
          !hasProp(path.node, "manipulateOptions")
        )
          return;

        try {
          if (hasProp(path.node, "name") || ok.has(program(path).node)) {
            return;
          }

          path.node.properties.unshift(
            t.objectProperty(t.identifier("name"), t.stringLiteral(name))
          );
        } finally {
          ok.add(program(path).node, true);
        }
      },
      Program: {
        exit(path) {
          if (!ok.has(path.node)) throw "NOT FOUND";
        },
      },
    },
  };
}

function babelRecast(code, parserOpts, transformOpts) {
  const ast = recast.parse(code, {
    parser: {
      parse: source => babel.parse(source, parserOpts),
    },
  });

  const opts = Object.assign(
    {
      ast: true,
      code: false,
    },
    transformOpts,
    {
      plugins: [
        // For some reason, recast doesn't work with transformFromAst.
        // Use this hack instead.
        () => ({
          visitor: {
            Program(path) {
              path.replaceWith(ast.program);
            },
          },
        }),
      ].concat(transformOpts.plugins || []),
    }
  );

  const output = babel.transformSync("", opts);

  return recast.print(output.ast).code + "\n";
}
babel on  master [⇣$!+] is 📦 ⚠ via ⬢ v10.10.0 took 3s 
➜ node add-names.js && yarn fix
@babel/preset-env: `DEBUG` option

Using targets:
{
  "node": "10.10"
}

Using modules transform: false

Using plugins:
  proposal-async-generator-functions { "node":"10.10" }
  syntax-object-rest-spread { "node":"10.10" }
  proposal-unicode-property-regex { "node":"10.10" }
  proposal-json-strings { "node":"10.10" }
  proposal-optional-catch-binding { "node":"10.10" }

Using polyfills: No polyfills were added, since the `useBuiltIns` option was not set.
[ [ 'babel-plugin-transform-regenerator', 'NOT FOUND' ] ]
yarn run v1.9.4
$ make fix
./node_modules/.bin/prettier "{packages,codemod}/*/test/fixtures/**/options.json" --write --loglevel warn
./node_modules/.bin/eslint scripts packages codemods '*.js' --format=codeframe --fix
Done in 35.58s.

@nicolo-ribaudo nicolo-ribaudo added the PR: Polish 💅 A type of pull request used for our changelog categories label Sep 25, 2018
@babel-bot
Copy link
Collaborator

babel-bot commented Sep 25, 2018

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

@xtuc
Copy link
Member

xtuc commented Sep 28, 2018

Very nice, maybe we could keep the script arround? Could you please commit it in the scripts dir?

@nicolo-ribaudo
Copy link
Member Author

I don't think that we will ever need it anymore.

@danez
Copy link
Member

danez commented Nov 6, 2018

We should maybe add a test check that folder name matches the plugin name, otherwise it might be easily forgotten, at least I would probably 😄?
On the other hand we do not rename plugins that often and usually probably search through the code for the name anyway.

@nicolo-ribaudo
Copy link
Member Author

Good idea 👍

I think that I will implement it as a linting rule, since the tests would have to be manually enabled for any new package (so it is as easy to forget it as it is to add the plugin name)

@nicolo-ribaudo
Copy link
Member Author

ESLint plugin added!

@nicolo-ribaudo nicolo-ribaudo merged commit 8c7d4b5 into babel:master Nov 18, 2018
@nicolo-ribaudo nicolo-ribaudo deleted the plugin-names branch November 18, 2018 22:03
@nicolo-ribaudo
Copy link
Member Author

Now Time Travel in the repl shows the correct plugins name 😄

NMinhNguyen pushed a commit to NMinhNguyen/babel-plugin-transform-destructuring that referenced this pull request Aug 9, 2019
* Add plugins name

* Add missing names found by the plugin

* Add eslint plugin
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 4, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Polish 💅 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants