facebook / jscodeshift

A JavaScript codemod toolkit.

Home Page:https://jscodeshift.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how can I add @babel/plugin-proposal-private-methods to jscodeshift ?

sibelius opened this issue · comments

node_modules/write-file-atomic/node_modules/signal-exit/dist/cjs/index.js: Class private methods are not enabled. Please add @babel/plugin-proposal-private-methods to your configuration.
210 | this.#emitter.count -= 1;

Two recommendations:

(DISCLAIMER: Don't take this as a verbatim, literal answer, and I don't suspect it to run without considerable tweaking. I copy-pasted from source code for the babylon parser options in jscodeshift and GPT'ed it into JSON)

Make use of the --parser-config command line option and pass it the path a JSON file with babel parser configuration.

e.g. a file named custom_parser_config.json

{
  "sourceType": "module",
  "allowImportExportEverywhere": true,
  "allowReturnOutsideFunction": true,
  "startLine": 1,
  "tokens": true,
  "plugins": [
    ["flow", {"all": true}],
    "flowComments",
    "jsx",
    "asyncGenerators",
    "bigInt",
    "classProperties",
    "classPrivateProperties",
    "classPrivateMethods",
    ["decorators", {"decoratorsBeforeExport": false}],
    "doExpressions",
    "dynamicImport",
    "exportDefaultFrom",
    "exportNamespaceFrom",
    "functionBind",
    "functionSent",
    "importMeta",
    "logicalAssignment",
    "nullishCoalescingOperator",
    "numericSeparator",
    "objectRestSpread",
    "optionalCatchBinding",
    "optionalChaining",
     "plugin-proposal-private-methods",
    ["pipelineOperator", {"proposal": "minimal"}],
    "throwExpressions"
  ]
}

Then run a command like:

jscodeshift --parser-config custom_parser_config.json -t my_jscodshift_transform.js my_file_to_be_transformed.js

Incidentially, there's two takeaways I have from this issue:

  1. The documentation around custom babel configuration and custom parsers needs to be fleshed out with more real world, stem-to-stern examples.

  2. IMAO, the latest preset-env needs to be the default for jscodeshift. Simple utility is probably more important than minimalism for probably 99% of the people using this library. If we had the ES2022 preset-env baked into the library, @sibelius wouldn't even need to be making this into an issue on github because the functionality would already be there.

  3. I'm fine with the second item being an evcodeshift thing and seeing how well this works in the real world, as Facebook tends to be conservative about changes to jscodeshift.

this custom parser didn't worked for me

Did you also install the node module for the plugin?

the latest preset-env needs to be the default for jscodeshift

+1
This seems like a reasonable change to me.

where can I change this ?