# ast-parser

> ```sh $ npm i ast-parser -D ```

Latest version **0.2.0** (published 2022-04-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install ast-parser
pnpm add ast-parser
yarn add ast-parser
bun add ast-parser
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2022-04-14 |
| First published | 2019-01-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 37.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Daybrush |
| Maintainers | younkue |
| Keywords | ast, babel, tsdoc, jsdoc, typescript |

## Links

- npm: https://www.npmjs.com/package/ast-parser
- Homepage: https://github.com/daybrush/ast-parser
- Issues: https://github.com/daybrush/ast-parser/issues
- npm.io page: https://npm.io/package/ast-parser

## Dependencies (3)

- [@babel/types](https://npm.io/package/@babel/types.md) ^7.3.0
- [@babel/traverse](https://npm.io/package/@babel/traverse.md) ^7.2.3
- [@daybrush/utils](https://npm.io/package/@daybrush/utils.md) ^0.11.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.2.0 (latest) — 2022-04-14
- 0.1.1 — 2022-01-11
- 0.1.0 — 2021-09-07
- 0.0.6 — 2021-02-17
- 0.0.5 — 2020-08-11
- 0.0.4 — 2020-03-03
- 0.0.3 — 2019-03-14
- 0.0.2 — 2019-03-08
- 0.0.1 — 2019-02-13
- 0.0.0 — 2019-01-31

## README

# ast-parser

```sh
$ npm i ast-parser -D
```

```js
const {parse: babelParse} = require("@babel/parser");
const {parse: astParse, find, findInfo} = require("ast-parser");

function getNode(code) {
  return babelParse(code, {
    sourceType: "module",
    plugins: ["classProperties", "typescript"],
  });
}

expect(astParse(getNode(code)).nodeType).toBe("File");
expect(astParse(getNode(code).program).nodeType).toBe("Program");

const info = astParse(getNode(`
  const a = {a: 1, b: 2};
`)).program.body[0];
const info2 = info.declarations[0];

expect(info.nodeType).toBe("VariableDeclaration");
expect(info.kind).toBe("const");
expect(info.declarations.length).toBe(1);

expect(info2.id.nodeType).toBe("Identifier");
expect(info2.id.name).toBe("a");
expect(info2.id.string).toBe("a");
expect(info2.init.nodeType).toBe("ObjectExpression");
expect(info2.init.string).toBe("{a: 1, b: 2}");
expect(info.string).toBe("const a = {a: 1, b: 2}");

expect(
  astParse(getNode(`
    const a = {a: 1, b: 2};
  `)).program.body[0]
).).toBe("VariableDeclaration");
```

## find ObjectExpression, ObjectProperty
```js
const info = find("ObjectExpression", getNode(`
  const a = {a: 1, b: 2};
`));
const info2 = findInfo("ObjectProperty", info);

expect(info.string).toBe("{a: 1, b: 2}");
expect(info2.length).toBe(2);
expect(info2[0].string).toBe("a: 1");
expect(info2[0].key.string).toBe("a");
expect(info2[0].value.string).toBe("1");
expect(info2[1].string).toBe("b: 2");
expect(info2[1].key.string).toBe("b");
expect(info2[1].value.string).toBe("2");
```

## find ClassProperty
```js
const info = find("ClassDeclaration", `
  class A extends B implements C {
    a = "0";
    b: number = "1";
    private c: string = "2";
    public static d: () => void = () => ({});
    public static abstract e: () => void;
    constructor(a: string, private b?: number) {

    }
    public method1(): number {

    }
  }
`);
const info2 = findInfo("ClassProperty", info);
const info3 = findInfo("ClassMethod", info);

expect(info.string).toBe("class A extends B implements C");
expect(info2.length).toBe(5);
expect(info3.length).toBe(2);
expect(info2[0].string).toBe("a");
expect(info2[1].string).toBe("b: number");
expect(info2[2].string).toBe("private c: string");
expect(info2[3].string).toBe("public static d: () => void");
expect(info2[4].string).toBe("public static abstract e: () => void");
expect(info3[0].string).toBe("constructor(a: string, private b?: number)");
expect(info3[1].string).toBe("public method1(): number");
```

---
_Source: https://npm.io/package/ast-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
