npm package: @pyts/tsparser
Description: TypeScript AST parser and code generator from JSON
Author: @jraylan
Latest version: 0.1.0

# @pyts/tsparser

TypeScript AST parser and code generator from JSON.

This library receives a JSON representation of TypeScript AST nodes (generated by `pytsast`) and converts them to actual TypeScript code.

## Installation

```bash
npm install @pyts/tsparser
```

## Usage

### From JSON String

```typescript
import { parseAndPrint } from '@pyts/tsparser';

const json = JSON.stringify({
  type: 'factory',
  name: 'createIdentifier',
  args: [{ type: 'literal', value: 'myVariable' }],
});

const code = parseAndPrint(json);
console.log(code); // myVariable
```

### From Serialized Object

```typescript
import { toTypeScript } from '@pyts/tsparser';

const serialized = {
  type: 'factory',
  name: 'createImportDeclaration',
  args: [
    // ... serialized args from pytsast
  ],
};

const code = toTypeScript(serialized);
console.log(code);
```

### Low-Level API

```typescript
import { deserialize, printNode } from '@pyts/tsparser';
import ts from 'typescript';

// Deserialize JSON to TypeScript AST
const node = deserialize(serializedJson) as ts.Node;

// Print the node as TypeScript code
const code = printNode(node, { trailingNewline: true });
```

## API

### High-Level Functions

- `parse(json: string): ts.Node` - Parse JSON string to AST node
- `parseAndPrint(json: string, options?): string` - Parse and print as code
- `toTypeScript(node: SerializedNode, options?): string` - Convert object to code
- `toTypeScriptFile(nodes: SerializedNode[], options?): string` - Multiple nodes to file

### Deserializer Functions

- `deserialize(node: SerializedNode): DeserializedValues
` - Deserialize any node
- `deserializeStatement(node): ts.Statement` - Deserialize as statement
- `deserializeStatements(nodes): ts.Statement[]` - Deserialize statement array
- `deserializeExpression(node): ts.Expression` - Deserialize as expression
- `deserializeDeclaration(node): ts.DeclarationStatement` - Deserialize as declaration

### Printer Functions

- `printNode(node: ts.Node, options?): string` - Print single node
- `printNodes(nodes: ts.Node[], options?): string` - Print multiple nodes
- `printFile(statements: ts.Statement[], options?): string` - Print as file

### Type Guards

- `isLiteral(node): node is SerializedLiteral`
- `isNumber(node): node is SerializedNumber`
- `isFactory(node): node is SerializedFactory`

## Serialization Format

The JSON format matches the output from `pytsast`:

```json
{
  "type": "factory",
  "name": "createIdentifier",
  "args": [
    { "type": "literal", "value": "myVar" }
  ]
}
```

### Node Types

- **literal**: `{ type: "literal", value: null | boolean | string }`
- **undefined**: `{ type: "undefined"}`
- **number**: `{ type: "number", value: number | string }`
- **factory**: `{ type: "factory", name: string, args: SerializedNode[] }`

## Integration with pytsast

```python
# Python side
>>> from pytsast import factory as ts
>>> import json
>>> 
>>> node = ts.createIdentifier("hello")
>>> json_output = json.dumps(node.serialize().model_dump(), indent=2)
>>> print(json_output)
{
  "type": "factory",
  "name": "createIdentifier",
  "args": [
    {
      "type": "literal",
      "value": "hello"
    }
  ]
}
```

```typescript
// TypeScript side
> import { parseAndPrint } from '@pyts/tsparser';
> 
> const jsonFromPython = '...'; // Output from Python
> const tsCode = parseAndPrint(jsonFromPython);
> console.log(tsCode);
hello
```