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

Add support of syntaxes? #102

Closed
RIP21 opened this issue Mar 19, 2018 · 10 comments · Fixed by #103 or #105
Closed

Add support of syntaxes? #102

RIP21 opened this issue Mar 19, 2018 · 10 comments · Fixed by #103 or #105

Comments

@RIP21
Copy link

RIP21 commented Mar 19, 2018

I have a plugin which depends on many syntaxes, and if I use babel-cli or babel.transform through the code, everything is working perfectly.
When I use my plugin with codemod it's arguing, saying that I need to add react/jsx/spread etc plugins, but why should I when I just need babel to understand syntax and output code and when I have all required syntaxes already described in the plugin.

Thanks.

eventualbuddha added a commit that referenced this issue Mar 19, 2018
If you simply pass package names into babel's list of presets then it'll try to load them from the working directory. That doesn't work for us, so we need to resolve–but not load!–the presets relative to babel-codemod itself.

Fixes #102
eventualbuddha added a commit that referenced this issue Mar 19, 2018
If you simply pass package names into babel's list of presets then it'll try to load them from the working directory. That doesn't work for us, so we need to resolve–but not load!–the presets relative to babel-codemod itself.

Fixes #102
@babel-codemod-bot
Copy link

🎉 This issue has been resolved in version 1.6.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

@eventualbuddha
Copy link
Collaborator

Thanks for reporting this, @RIP21! I believe I identified the main problem, but if not please give me a little more information about your project:

  • your .babelrc file
  • a file you're trying to codemod
  • any custom plugins/codemods you're using
  • the command-line call to codemod

Thanks!

@RIP21
Copy link
Author

RIP21 commented Mar 19, 2018

@eventualbuddha I will report if something will appear. Thanks :)

@RIP21
Copy link
Author

RIP21 commented Mar 19, 2018

@eventualbuddha
So here is what I have.

I don't have .babelrc.
Command-line call:

codemod --plugin ./src/index.js ./file.js

This is what I've got:

SyntaxError: /Users/andriilos/Projects/Pet/babel-plugin-hyperscript-to-jsx/src/utils.js: Support for the experimental syntax 'objectRestSpread' isn't currently enabled (32:28):

  30 |     const id = firstId ? firstId.substr(1, firstId.length) : undefined;
  31 |     let result = id ? { tag, id } : { tag };
> 32 |     result = className ? { ...result, className } : result;
     |                            ^
  33 |     return result;
  34 |   }
  35 | };

Add @babel/plugin-proposal-object-rest-spread (https://git.io/vb4Ss) to the 'plugins' section of your Babel config to enable transformation.
    at Parser.raise (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:778:15)
    at Parser.expectPlugin (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2068:18)
    at Parser.parseObj (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:3459:14)
    at Parser.parseExprAtom (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:3091:21)
    at Parser.parseExprSubscripts (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2725:21)
    at Parser.parseMaybeUnary (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2704:21)
    at Parser.parseExprOps (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2616:21)
    at Parser.parseMaybeConditional (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2588:21)
    at Parser.parseMaybeAssign (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2546:21)
    at Parser.parseConditional (/usr/local/lib/node_modules/babel-codemod/node_modules/babylon/lib/index.js:2603:32)

This is the file I try to codemod:

import h from "react-hyperscript";

const StatelessComponent = props => h("h1");

const StatelessWithReturn = props => {
  return h(".class", { shouldRender: lol.length > 0 });
};

function named(props) {
  return h("h1");
}

class Comp extends React.Component {
  render() {
    return h("div.example", [
      h("h1#heading", { ...getProps, ...getKnobs(), stuff: "" }),
      h("h1#heading", getChildren(), [h("div")]),
      h("div", [h("div", "Some content")]),
      h("h1#heading", "This is hyperscript"),
      h("h2", "creating React.js markup"),
      h(
        AnotherComponent,
        { foo: "bar", bar: () => ({}), shouldRender: thing.length > 0 },
        [
          h("li", [h("a", { href: "http://whatever.com" }, "One list item")]),
          h("li", "Another list item")
        ]
      )
    ]);
  }
}

This is the plugin I use https://github.com/RIP21/babel-plugin-hyperscript-to-jsx

@eventualbuddha
Copy link
Collaborator

Okay, I think I figured it out. I need to enable all the babylon plugins in addition to using @babel/preset-env, which will make object rest spread work correctly. I'll have a fix likely tomorrow.

eventualbuddha added a commit that referenced this issue Mar 20, 2018
When transpiling required files we need to be pretty liberal about what syntax we allow, otherwise we can end up in a situation where the current node version can actually parse more natively than babylon is allowed to by `@babel/preset-env`. This should fix that.

Fixes #102
eventualbuddha added a commit that referenced this issue Mar 21, 2018
When transpiling required files we need to be pretty liberal about what syntax we allow, otherwise we can end up in a situation where the current node version can actually parse more natively than babylon is allowed to by `@babel/preset-env`. This should fix that.

Fixes #102
@babel-codemod-bot
Copy link

🎉 This issue has been resolved in version 1.7.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@RIP21
Copy link
Author

RIP21 commented Mar 21, 2018

@eventualbuddha it appears that it now runs, but fails with another error.

TypeError: Property expression of JSXExpressionContainer expected node to be of a type ["Expression"] but instead got null

It appears to be not a problem of babel-plugin itself. Cause regular babel.transform (latest babel v6) works nicely.

It fails somewhere at processing these lines:

      h('h1#heading', {...getProps, ...getKnobs(), stuff: ''}),

I assume that it can't get properly or in latest babel v7 AST was changed in some way, so it got broken, but what I get is undefined, undefined.

File I try to codemod is the same as at the top.

UPD on ^^ case: It's a babel 6 -> 7 AST difference. So it's not SpreadProperty but SpreadElement,
t.isSpreadProperty(node) || t.isSpreadElement(node) fixed with this line.
Error at the down the road is the same tho.

It throws a weird error like:

AssertionError [ERR_ASSERTION]: false == true
    at genericPrintNoParens (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:1165:20)
    at genericPrint (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:166:9)
    at printRootGenerically (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:107:15)
    at maybeReprint (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:99:16)
    at print (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:83:16)
    at exports.printComments (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/comments.js:324:22)
    at printWithComments (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:63:16)
    at /usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:1182:24
    at FastPath.map (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/fast-path.js:173:19)
    at genericPrintNoParens (/usr/local/lib/node_modules/babel-codemod/node_modules/recast/lib/printer.js:1170:18)
1 file(s), 0 modified, 1 errors

@eventualbuddha
Copy link
Collaborator

eventualbuddha commented Mar 22, 2018

There's an assertion that recast is making but isn't providing a good error message for that checks that there isn't a closing element when the JSXElement is self-closing. This validation isn't performed by babel's default printer. Therefore, I think this is a difference in how recast handles the AST vs. how babel handles it.

I traced the bug to this line:

selfClosing ? bJsxCloseElem(name) : null,

If you change it to this instead, it works for me:

!selfClosing ? bJsxCloseElem(name) : null,

and I get this output for the file you pasted above:

import React from "react";
import h from "react-hyperscript";

const StatelessComponent = props => <h1 />;

const StatelessWithReturn = props => {
  return <div className="class" shouldRender={lol.length > 0} />;
};

function named(props) {
  return <h1 />;
}

class Comp extends React.Component {
  render() {
    return <div className="example"><h1 id="heading" {...getProps} {...getKnobs()} stuff="" /><h1 id="heading" {...getChildren()}><div /></h1><div><div>Some content</div></div><h1 id="heading">This is hyperscript</h1><h2>creating React.js markup</h2><AnotherComponent foo="bar" bar={() => ({})} shouldRender={thing.length > 0}><li><a href="http://whatever.com">One list item</a></li><li>Another list item</li></AnotherComponent></div>;
  }
}

So I'm going to leave this closed and say this is working as expected, though the error message was pretty obtuse and could be improved in recast.

@RIP21
Copy link
Author

RIP21 commented Mar 22, 2018

@eventualbuddha hm. Not sure if it's really expected behavior :)
If babel outputs correctly, that means that babel plugin is a valid one.
If babel plugin is a valid one, he should be working with this babel-codemod. Cause input/output must be compliant with babel, not any other tools that you use under the hood.
I'm as a plugin developer should rely only on babel, not other tools that babel-codemod uses.

Maybe it's better to use prettier as an escape hatch under some flag? (also option to switch off recast will be a nice thing to have). Yes, he (prettier) can significantly reformat the code even if it was not touched by the babel-plugin itself but in such scenarios will be helpful.

@eventualbuddha
Copy link
Collaborator

That’s a fair point. I filed #107 to give people options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants