Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/azz/prettier-transform

Perform AST transforms before running prettier
https://github.com/azz/prettier-transform

ast prettier transform

Last synced: 2 months ago
JSON representation

Perform AST transforms before running prettier

Awesome Lists containing this project

README

        

# prettier-transform
[![Travis](https://img.shields.io/travis/azz/prettier-transform.svg?style=flat-square)](https://travis-ci.org/azz/prettier-transform)
[![Prettier](https://img.shields.io/badge/styled_with-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![npm](https://img.shields.io/npm/v/prettier-transform.svg?style=flat-square)](https://npmjs.org/prettier-transform)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)](https://github.com/semantic-release/semantic-release)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](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
});
```