facebook / jscodeshift

A JavaScript codemod toolkit.
https://jscodeshift.com
MIT License
9.32k stars 481 forks source link

Make `map` on Collections work with nodes #40

Open cpojer opened 9 years ago

cpojer commented 9 years ago

This should work:

  const findReactCreateClassExportDefault = path =>
    path
      .find(j.ExportDefaultDeclaration, {
        type: 'ExportDefaultDeclaration',
        declaration: {
          type: 'CallExpression',
          callee: REACT_CREATE_CLASS_MEMBER_EXPRESSION
        }
      })
      .map((p) => {
        return p.value.declaration;
      })

cc @iammerrick

caasi commented 5 years ago

There are two things blocking me from using map.

  1. It's hard to recreate a NodePath with a right parent.
  2. Using instanceof to check if a result is a NodePath fails when people mixing global installed jscodeshift with local installed jscodeshift or ast-types.

And because the map function works like a bind function for lists, I think it should accept a Collection as a result instead of some NodePath. If someone can find a way to recreate a Collection with a proper parent, it will make jscodeshift more expressive.

j(file.source)
  .findVariableDeclarators(someName)
  // with an improved map function
  .map(p => fromNodes(p.node.init.properties));