Coder Social home page Coder Social logo

Comments (4)

schiller-manuel avatar schiller-manuel commented on June 15, 2024

Currently

this

  const value = React.useMemo(function somethingHere() {
    return 'memoed-value';
  }, []);

compiles to

const value = React.useMemo([]);

The issue lies in eliminateUnreferencedIdentifiers.
If this is commented out in compilers.ts, the function is not removed and the error does not occur.

from router.

tannerlinsley avatar tannerlinsley commented on June 15, 2024

I released a new version, did that help at all? I was noticing an issue with array destructuring as well.

from router.

schiller-manuel avatar schiller-manuel commented on June 15, 2024

I released a new version, did that help at all? I was noticing an issue with array destructuring as well.

no same problem exists in v1.26.8

https://stackblitz.com/edit/github-is6hai-wa51rg?file=package.json

from router.

SeanCassiere avatar SeanCassiere commented on June 15, 2024

Did a bit of debugging and figured it out.

If this line, if the getIdentifier function is modified to include this, then this gets resolved.

function getIdentifier(
  path: NodePath<
    | BabelTypes.FunctionDeclaration
    | BabelTypes.FunctionExpression
    | BabelTypes.ArrowFunctionExpression
  >,
): NodePath<BabelTypes.Identifier> | null {
  // ..

  if (
    path.node.type === 'ArrowFunctionExpression' ||
    path.node.type === 'FunctionExpression'
  ) {
    return null
  }

 // ...
}

Alternatively, in the isIdentifierReferenced function, you could add a check before this line to check if a FunctionExpression is a child of a CallExpression.

function isIdentifierReferenced(
  ident: NodePath<BabelTypes.Identifier>,
): boolean {
  const binding = ident.scope.getBinding(ident.node.name)

  if (
    binding?.path.node.type === 'FunctionExpression' &&
    binding.path.parentPath?.node.type === 'CallExpression'
  ) {
    return true
  }
 // ...
}

@tannerlinsley I'm hesitant to add a PR for this since testing pretty much breaks as some snapshot files rely on absolute pathing from your machine for the tests to pass.

This was the test I used to validate that this worked.

// tests/test-files/functionAsParameter.tsx
import * as React from 'react'

const value = React.useMemo(function performAction() {
  return 'true'
}, [])

console.log(value)

const value2 = React.useMemo(() => {
  return 'true'
}, [])

console.log(value2)

Edit: It looks like tests aren't failing in CI since the script in the package.json is named differently.

from router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.