HearTao/ts-creator

Outside value Support

Opened this issue · 1 comments

Syntax node(list) factory

type Factory<T extends Node> = Record<T["kind"], (node: T) => T>  
type ListFactory<T extends Node> = Record<T["kind"], (node: T) =>T[]> 

Signal node factory

const a = 1;
{
   [ts.SyntaxKind.Identifier]: (id: ts.Identifier) => {
        // match id is 'a'
       // ...
        return ts.createIdentifier(id.text.toUpperCase())
    }
}

to

ts.createVariableStatement(
    undefined,
    ts.createVariableDeclarationList(
        [
        ts.createVariableDeclaration(
            ts.createIdentifier('A'),
            undefined,
            ts.createNumericLiteral('1')
        )
        ],
        ts.NodeFlags.Const
    )
)

Multi node factory

interface Foo {
    a: never
}
{
    [ts.SyntaxKind.PropertySignature]: (p: ts.PropertySignature) => {
        // match p is 'a: never'
        // ...
        return [
            ts.createPropertySignature(
                undefined,
                ts.createIdentifier('a'),
                undefined,
                ts.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword),
                undefined
            ),
            ts.createPropertySignature(
                undefined,
                ts.createIdentifier('b'),
                undefined,
                ts.createKeywordTypeNode(ts.SyntaxKind.StringKeyword),
                undefined
            )
        ]
    }
}

to

interface Foo {
    a: number
    b: string
}

consider tsquery