-
Notifications
You must be signed in to change notification settings - Fork 743
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
Conversation
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.
website/webpack.config.js
Outdated
// 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/), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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 // 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;
}
}); |
@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? |
Regardless of anything else, I think that would be a good idea.
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:
The reasoning behind this is:
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 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). |
Is this good to go? Code looks fine to me. |
Yup, I just resolved the merge conflict, and don't have any more changes to make. |
* 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
* 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
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.