prettier-transform
v1.1.0
Published
Perform AST transforms before running prettier
Downloads
3
Readme
prettier-transform
Install
yarn add --dev prettier-transform
Usage with prettier
Add to your prettier configuration file (.prettierrc
):
{
"transform": {
"parser": "babylon",
"transforms": [
"./my-transform"
]
}
}
Then invoke prettier using the parser API:
prettier --parser ./node_modules/prettier-transform --write "**/*.js"
If you want to do different transforms for different files, you can make use of
prettier's overrides
configuration:
{
"overrides": [
{
"files": "*.jsx",
"options": {
"transform": {
"parser": "babylon",
"transforms": ["./transform-a", "./transform-b"]
}
}
}
]
}
Writing Transforms
Transforms must be standard JavaScript modules that export a function that takes an AST and returns an AST.
If you're working with a babylon-produced AST, you can do the following:
const traverse = require("babel-traverse").default;
module.exports = ast => {
traverse(ast, {
Identifier(path) {
if (path.node.name === "foo") {
path.node.name = "bar";
}
}
});
return ast;
};
API Usage
Basic usage:
const prettierTransform = require("prettier-transform");
const myTransform = require("./my-transform");
prettierTransform.format("foo()", [myTransform], { parser: "babylon" });
Pass options to prettier:
prettierTransform.format("foo()", [myTransform], {
parser: "babylon",
semi: false
});