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

fix: fix so that we dont blow up when encountering an "export default… #4

Merged
merged 4 commits into from
Mar 10, 2021

Conversation

ammmze
Copy link
Contributor

@ammmze ammmze commented Feb 26, 2021

… class ..."

also adds tests to cover various scenarios

This is regarding the discussion we had over here.

But the gist of the issue is we blow up when a file contains a default export of a class:

export default class MyComponent extends React.Component {
    // ...
}

The error we would receive was:

Property declaration of ExportDefaultDeclaration expected node to be of a type ["FunctionDeclaration","TSDeclareFunction","ClassDeclaration","Expression"] but instead got "VariableDeclaration"

PS. I just saw your mention in #3, I'll take a deeper look at those tests as well.

package.json Outdated Show resolved Hide resolved
@ammmze
Copy link
Contributor Author

ammmze commented Feb 26, 2021

I tried to also add a test that would cover the classNameMatchers option, but in order to do so, I needed a javascript babel config instead of json, so I tried creating a .babelrc.js but then it failed to load the plugin because it tries to run in commonjs, so it gives me errors with the first line of src/index.js... import template from '@babel/template'. I tried changing it to .babelrc.mjs, but then it just stopped working. Maybe i'll have better luck via the other babel tester library.

Edit: So looks like we don't have the options setup to use that. So it appears the classNameMatchers is just unused code. I'll leave it as-is and won't worry about tests for now. I think we can include that in the future if deemed necessary.

Comment on lines -44 to +67
path.replaceWith(
t.VariableDeclaration('const', [
t.VariableDeclarator(
componentNameIdentifier,
iife.expression
),
])
)
const declarators = [
t.VariableDeclarator(
componentNameIdentifier,
iife.expression
),
]
if (
path.parentPath.isExportDefaultDeclaration()
) {
path.parentPath.insertBefore(
t.VariableDeclaration(
'const',
declarators
)
)
path.replaceWith(path.node.id)
} else {
path.replaceWith(
t.VariableDeclaration(
'const',
declarators
)
)
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not very familiar with everything here, but I know enough to pretty much understand what i'm doing here. But I based this off another project that was facing the same issue.

… class ..."

also adds tests to cover various scenarios
}
}

_defineProperty(_class, 'displayName', 'FancyName1')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...i need to look at this more ... our IIFE doesn't return the _class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just pushed some updates that I think address the class wrappers. I had noticed that when we wrapped classes, the class never got returned from our IIFE. I couldn't figure out how to have it return the _class variable it was defining, and it felt weird having it return the class name but everything else was using this _class variable, so I changed it so it no longer changes the type to a ClassExpression. However, in doing so, we get an infinite loop, which I broke by just checking if the parent type is a BlockExpression.

@mbrowne
Copy link
Owner

mbrowne commented Feb 27, 2021

Thanks for the PR! I probably won't have time to do a detailed review anytime soon, so my plan is to just run the tests and also try it in a real project when the PR is ready, and if everything works then I will just merge it. I can also publish it as a public npm package.

Regarding the Jest issue with imports when you use .babelrc.js, I wonder if that can be addressed by adjusting the Jest configuration. Jest has experimental support for ES modules: https://jestjs.io/docs/en/next/ecmascript-modules.

@ammmze
Copy link
Contributor Author

ammmze commented Feb 27, 2021

Thanks for the PR! I probably won't have time to do a detailed review anytime soon, so my plan is to just run the tests and also try it in a real project when the PR is ready, and if everything works then I will just merge it. I can also publish it as a public npm package.

Sounds like a plan! To keep things easier to maintain, I can also (in a separate PR) see about getting some github actions stuff setup to automate semver and publishing to npm.

Regarding the Jest issue with imports when you use .babelrc.js, I wonder if that can be addressed by adjusting the Jest configuration. Jest has experimental support for ES modules: https://jestjs.io/docs/en/next/ecmascript-modules.

Thanks! I had looked at that a little bit, but ultimately I ended up not needing the test for it since we don't currently wire up the options that would use the bit of code I was attempting to test, so its basically un-used code that was copied over isReactClass file.

ammmze and others added 2 commits February 27, 2021 14:51
…ompatibility

ideally we would be able to have it go through preset-env to get it to transform the arrow functions if necessary, but we dont appear to get that for free the way we are doing this. perhaps it can be investigated in the future
@mbrowne
Copy link
Owner

mbrowne commented Mar 7, 2021

Hi, sorry for the delay. Is this ready to merge? I ran the tests and the demo works, so it looks good to me. I also updated the readme, etc.

Future PRs for CI/CD are welcome.

Thank you

@mbrowne mbrowne mentioned this pull request Mar 9, 2021
@ammmze
Copy link
Contributor Author

ammmze commented Mar 9, 2021

Hi, sorry for the delay. Is this ready to merge? I ran the tests and the demo works, so it looks good to me. I also updated the readme, etc.

Future PRs for CI/CD are welcome.

Thank you

👍🏻 Yep! I think this is good to go.

@mbrowne mbrowne merged commit b238a8c into mbrowne:master Mar 10, 2021
@mbrowne
Copy link
Owner

mbrowne commented Mar 10, 2021

Published on npm as version 0.2.0! Thanks again.

https://www.npmjs.com/package/babel-plugin-pure-static-props

CC @Mattgic @mryechkin

@ammmze ammmze deleted the fix-default-export-class branch March 10, 2021 03:24
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

Successfully merging this pull request may close these issues.

None yet

2 participants