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

Error when running pure-component on tsx file #257

Open
njradford opened this issue Mar 14, 2020 · 3 comments
Open

Error when running pure-component on tsx file #257

njradford opened this issue Mar 14, 2020 · 3 comments

Comments

@njradford
Copy link

When running the pure-component codemod on a typescript file

// TransformTest.tsx
import React from 'react';

class TransformTest extends React.Component {
	render() {
		return <div>I've too much class</div>;
	}
}

export default TransformTest;

I see the following errors

react-codemod pure-component TransformTest.tsx
? Which dialect of JavaScript do you use? TypeScript
? Use arrow functions? No
? Destructure props? No
Executing command: jscodeshift --verbose=2 --ignore-pattern=**/node_modules/** --parser tsx --extensions=tsx,ts,jsx,js --transform /Users/nick/.config/yarn/global/node_modules/react-codemod/transforms/pure-component.js TransformTest.tsx
Processing 1 files... 
Spawning 1 workers...
Sending 1 files to free worker...
 ERR TransformTest.tsx Transformation error (Cannot read property 'value' of undefined)
TypeError: Cannot read property 'value' of undefined
    at NodePath.pureClasses.replaceWith.p (/Users/nick/.config/yarn/global/node_modules/react-codemod/transforms/pure-component.js:296:37)
    at NodePath.<anonymous> (/Users/nick/.config/yarn/global/node_modules/react-codemod/node_modules/jscodeshift/src/collections/Node.js:144:47)
    at __paths.forEach (/Users/nick/.config/yarn/global/node_modules/react-codemod/node_modules/jscodeshift/src/Collection.js:77:36)
    at Array.forEach (<anonymous>)
    at Collection.forEach (/Users/nick/.config/yarn/global/node_modules/react-codemod/node_modules/jscodeshift/src/Collection.js:76:18)
    at Collection.replaceWith (/Users/nick/.config/yarn/global/node_modules/react-codemod/node_modules/jscodeshift/src/collections/Node.js:142:17)
    at Collection.replaceWith (/Users/nick/.config/yarn/global/node_modules/react-codemod/node_modules/jscodeshift/src/Collection.js:415:43)
    at module.exports (/Users/nick/.config/yarn/global/node_modules/react-codemod/transforms/pure-component.js:291:15)
All done. 
Results: 
1 errors
0 unmodified
0 skipped
0 ok

Node v10.18.0

@LukasDeco
Copy link

I am dealing with this error too. Would love to know more.

@LukasDeco
Copy link

This seems to be the block of code it is referring to:

  pureClasses.replaceWith(p => {
    const name = p.node.id.name;
    const renderMethod = p.value.body.body.filter(isRenderMethod)[0];
    const renderBody = renderMethod.value.body;
    const propsTypeAnnotation = findPropsTypeAnnotation(p.value.body.body);
    const statics = p.value.body.body.filter(isStaticProperty);
    const destructure = destructuringEnabled && canDestructure(j(renderMethod));

I think the offending couple of lines are:

    const renderMethod = p.value.body.body.filter(isRenderMethod)[0];
    const renderBody = renderMethod.value.body;

Is it possible that for some reason it can't find the render method?

@JelleZijlstra
Copy link

Not sure why this was closed; I still see the same issue. I looked into this for a bit and found that isRenderMethod needs to check for the ClassMethod type in addition to MethodDefinition. But that's not enough: it expects a .value attribute on the method, but ClassMethod doesn't have those. After I fix that too, I get an Error: Could not replace path.

@njradford njradford reopened this Oct 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants