@pyts/tsparser
v0.1.0
Published
TypeScript AST parser and code generator from JSON
Maintainers
Readme
@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
npm install @pyts/tsparserUsage
From JSON String
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); // myVariableFrom Serialized Object
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
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 nodeparseAndPrint(json: string, options?): string- Parse and print as codetoTypeScript(node: SerializedNode, options?): string- Convert object to codetoTypeScriptFile(nodes: SerializedNode[], options?): string- Multiple nodes to file
Deserializer Functions
deserialize(node: SerializedNode): DeserializedValues- Deserialize any nodedeserializeStatement(node): ts.Statement- Deserialize as statementdeserializeStatements(nodes): ts.Statement[]- Deserialize statement arraydeserializeExpression(node): ts.Expression- Deserialize as expressiondeserializeDeclaration(node): ts.DeclarationStatement- Deserialize as declaration
Printer Functions
printNode(node: ts.Node, options?): string- Print single nodeprintNodes(nodes: ts.Node[], options?): string- Print multiple nodesprintFile(statements: ts.Statement[], options?): string- Print as file
Type Guards
isLiteral(node): node is SerializedLiteralisNumber(node): node is SerializedNumberisFactory(node): node is SerializedFactory
Serialization Format
The JSON format matches the output from pytsast:
{
"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 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 side
> import { parseAndPrint } from '@pyts/tsparser';
>
> const jsonFromPython = '...'; // Output from Python
> const tsCode = parseAndPrint(jsonFromPython);
> console.log(tsCode);
hello