Skip to content

Commit

Permalink
Added JSDoc to @codemod-utils/ast-template (#94)
Browse files Browse the repository at this point in the history
* refactor: Added JSDoc to @codemod-utils/ast-template

* chore: Added changeset

---------

Co-authored-by: ijlee2 <ijlee2@users.noreply.github.com>
  • Loading branch information
ijlee2 and ijlee2 authored Oct 29, 2023
1 parent 24ad5a9 commit 8b95c82
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-hairs-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@codemod-utils/ast-template": minor
---

Added JSDoc to @codemod-utils/ast-template
12 changes: 5 additions & 7 deletions packages/ast/template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ _Utilities for handling `*.hbs` files as abstract syntax tree_

## What is it?

`@codemod-utils/ast-template` wraps the methods from [`ember-template-recast`](https://github.com/ember-template-lint/ember-template-recast), a library that helps you parse and transform `*.hbs` files.
`@codemod-utils/ast-template` provides methods from [`ember-template-recast`](https://github.com/ember-template-lint/ember-template-recast) to help you parse and transform `*.hbs` files.

The wrappers help you read and write files of different types _in the same way_. This way, you can focus on learning the **builders** and **visit methods**, the building blocks for transforming code (library-dependent).

```js
```ts
import { AST } from '@codemod-utils/ast-template';

function transformCode(file) {
function transformCode(file: string): string {
const traverse = AST.traverse();

const ast = traverse(file, {
Expand Down Expand Up @@ -57,7 +55,7 @@ Copy-paste the visit methods from your file to AST explorer, then rename `AST.bu

<summary>Example</summary>

```js
```ts
/* Your file */
import { AST } from '@codemod-utils/ast-template';

Expand All @@ -84,7 +82,7 @@ function transformCode(file) {
}
```

```js
```ts
/* AST Explorer */
module.exports = function(env) {
const b = env.syntax.builders;
Expand Down
37 changes: 26 additions & 11 deletions packages/ast/template/src/ast/handlebars.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
type AST,
type AST as _AST,
builders,
type NodeVisitor,
print,
transform,
} from 'ember-template-recast';

function _traverse() {
return function (file: string, visitMethods: NodeVisitor = {}): AST.Template {
return function (
file: string,
visitMethods: NodeVisitor = {},
): _AST.Template {
const { ast } = transform({
plugin() {
return visitMethods;
Expand All @@ -19,16 +22,28 @@ function _traverse() {
};
}

type Tools = {
builders: typeof builders;
print: typeof print;
traverse: typeof _traverse;
};

const tools: Tools = {
/**
* Provides methods from `ember-template-recast` to help you parse
* and transform `*.hbs` files.
*
* @example
*
* ```ts
* import { AST } from '@codemod-utils/ast-template';
*
* function transformCode(file: string): string {
* const traverse = AST.traverse();
*
* const ast = traverse(file, {
* // Use AST.builders to transform the tree
* });
*
* return AST.print(ast);
* }
* ```
*/
export const AST = {
builders,
print,
traverse: _traverse,
};

export default tools;
2 changes: 1 addition & 1 deletion packages/ast/template/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { default as AST } from './ast/handlebars.js';
export * from './ast/handlebars.js';

0 comments on commit 8b95c82

Please sign in to comment.