https://github.com/azz/prettier-transform
Perform AST transforms before running prettier
https://github.com/azz/prettier-transform
ast prettier transform
Last synced: about 1 year ago
JSON representation
Perform AST transforms before running prettier
- Host: GitHub
- URL: https://github.com/azz/prettier-transform
- Owner: azz
- License: mit
- Created: 2017-09-29T07:35:53.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-29T12:05:31.000Z (over 8 years ago)
- Last Synced: 2025-03-18T12:16:48.030Z (over 1 year ago)
- Topics: ast, prettier, transform
- Language: JavaScript
- Size: 46.9 KB
- Stars: 23
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# prettier-transform
[](https://travis-ci.org/azz/prettier-transform)
[](https://github.com/prettier/prettier)
[](https://npmjs.org/prettier-transform)
[](https://github.com/semantic-release/semantic-release)
[](LICENSE)
## Install
```bash
yarn add --dev prettier-transform
```
## Usage with prettier
Add to your prettier configuration file (`.prettierrc`):
```json
{
"transform": {
"parser": "babylon",
"transforms": [
"./my-transform"
]
}
}
```
Then invoke prettier using the parser API:
```bash
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:
```json
{
"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:
```js
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:
```js
const prettierTransform = require("prettier-transform");
const myTransform = require("./my-transform");
prettierTransform.format("foo()", [myTransform], { parser: "babylon" });
```
Pass options to prettier:
```js
prettierTransform.format("foo()", [myTransform], {
parser: "babylon",
semi: false
});
```