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 prettier transformer #246

Merged
merged 7 commits into from
Jul 20, 2017
Merged

Conversation

josephfrazier
Copy link
Contributor

This makes it possible to use astexplorer like https://prettier.github.io/prettier,
but with the ability to cross-reference the code with its AST.

This makes it possible to use astexplorer like https://prettier.github.io/prettier,
but with the ability to cross-reference the code with its AST.
Now that prettier is available as a transformer, we should make it possible to
use these parsers.
// typescript-eslint-parser is a dev dependency of prettier, so it's not
// installed by default but prettier does still require it. See
// https://github.com/prettier/prettier/issues/986
new webpack.IgnorePlugin(/typescript-eslint-parser/, /\/prettier/),
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want to upgrade Prettier, you need to provide new regexes to exclude irrelevant parsers (they're structured differently in Prettier now). Unless you want to allow all of the parsers, but then need to check that transform actually works with each of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, fixed in 6927696

Since this is a JavaScript/etc transformer, I eliminated the non-JS parsers.

@azz
Copy link

azz commented Jun 25, 2017

Unless you want to allow all of the parsers, but then need to check that transform actually works with each of them.

That would be great. With the next version of prettier there's an opportunity to modify the AST so AST Explorer would be the perfect place to try that out.

// codeExample.txt
export default ({ traverse }) => ({
  parser(code, { babylon, /* flow, typescript, postcss */ }) {
    const ast = babylon(code);
    traverse(ast, {
      Identifier(path) {
        path.node.name = path.node.name.split('').reverse().join('');
      }
    });
    return ast;
  }
});

Alternatively, like the postcss transformer:

// codeExample.txt
import traverse from "babel-traverse";

export default {
  parser(code, { babylon }) {
    const ast = babylon(code);
    traverse(ast, {
      Identifier(path) {
        path.node.name = path.node.name.split('').reverse().join('');
      }
    });
    return ast;
  }
});

@RReverser
Copy link
Collaborator

@fkling I wonder if we can / should limit certain transformers to a list of supported parsers to make sure they're not invoked with something that produces an incompatible AST? (and how that would look like in the UI)

Maybe let parsers and transformers define the standard which they produce aka "estree", "babel", "typescript", etc. to filter them out by such group?

@fkling
Copy link
Owner

fkling commented Jul 12, 2017

@RReverser

Maybe let parsers and transformers define the standard which they produce aka "estree", "babel", "typescript", etc.

Regardless of anything else, I think that would be a good idea.

I wonder if we can / should limit certain transformers to a list of supported parsers

IMO the current structure (select parser or select transform + parser) is wrong and confusing. What I started in #197 was to unify parsers and transforms:

Unified interface

One big difference is that transformers also have to provide a parse() method (instead of specifying the ID of a parser to use). This method is supposed to delegate to the parser the transform is using. That means, whether a tool is a transformer only depends on whether or not it implements a transform method.
Overall this should simplify the store logic and other places on the website.
Another positive side effect of this: The "parser settings" logic can now be reused for transformer settings.

The reasoning behind this is:

  • At the moment, parsers and transformers are completely independent. I.e. any settings you make to a parser doesn't affect the transform at all. This is confusing. The version the transform is using internally might also be different than the version we provide.
  • By delegating to the transformers own parser we can show an AST that is closer to what the transformer is actually using.
  • Not all transformers allow you to change parser settings anyway.

Of course not all transformers expose the parser. In that case we have to bundle the right version and "replicate" the parsing process (as long as it's reasonable to do so).

I.e. in the end users wouldn't select a parser or a parser + transform anymore, but just a "tool". A tool exposes a parse() or a parse() and a transform() method.

However, if a transform actually accepts an AST as input, then we should allow to select a compatible parser (which is where your other suggestion comes in).

@fkling
Copy link
Owner

fkling commented Jul 12, 2017

Is this good to go? Code looks fine to me.

@josephfrazier
Copy link
Contributor Author

Yup, I just resolved the merge conflict, and don't have any more changes to make.

@fkling fkling merged commit 9692b58 into fkling:master Jul 20, 2017
@fkling fkling added the deploy pending Marks issues/PRs that are merged into master but are not deployed to any webserver label Jul 20, 2017
@josephfrazier josephfrazier deleted the prettier-transformer branch July 20, 2017 01:49
@fkling fkling added deployed to production Marks issues/PRs that are deployed to https://astexplorer.net and removed deploy pending Marks issues/PRs that are merged into master but are not deployed to any webserver labels Jul 20, 2017
ionisaligox72 added a commit to ionisaligox72/astexplorer that referenced this pull request Aug 7, 2024
* Add `prettier` transformer

This makes it possible to use astexplorer like https://prettier.github.io/prettier,
but with the ability to cross-reference the code with its AST.

* Upgrade to prettier 1.4.4

* Include typescript-eslint-parser and flow-parser in build

Now that prettier is available as a transformer, we should make it possible to
use these parsers.

* Exclude irrelevant prettier parsers from bundle

See fkling/astexplorer#246 (comment)

* Upgrade prettier to v1.5.2
my2 added a commit to my2/astexplorer that referenced this pull request Aug 13, 2024
* Add `prettier` transformer

This makes it possible to use astexplorer like https://prettier.github.io/prettier,
but with the ability to cross-reference the code with its AST.

* Upgrade to prettier 1.4.4

* Include typescript-eslint-parser and flow-parser in build

Now that prettier is available as a transformer, we should make it possible to
use these parsers.

* Exclude irrelevant prettier parsers from bundle

See fkling/astexplorer#246 (comment)

* Upgrade prettier to v1.5.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deployed to production Marks issues/PRs that are deployed to https://astexplorer.net
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants