facebook / jscodeshift

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

Collection.find() produces different results when chained with the same parameters #477

Open matyasf opened 2 years ago

matyasf commented 2 years ago

I have the following code (it's part of a larger function, this is the simplified version):

  const aaaa = root.find(j.JSXElement, {
    openingElement: {
      name: {
        type: 'JSXIdentifier',
        name: "Button"
      }
    }
  })
  console.log(aaaa.length) // logs 4
  const bbbb = aaaa.find(j.JSXElement, {
    openingElement: {
      name: {
        type: 'JSXIdentifier',
        name: "Button"
      }
    }
  })
  console.log(bbbb.length) // logs 0

The input test file:

let a = (
  <p>
    <Button variant="link" href="#" />
    <Button variant="link-inverse" href="#" />
    <Button variant="link" />
    <Button variant="link-inverse" />
  </p>
)

Why does the second find call find zero results? It has the same parameters, I'd expect that it logs 4 again.