Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose custom transformers when emitting #409

Closed
dsherret opened this issue Aug 26, 2018 · 0 comments
Closed

Expose custom transformers when emitting #409

dsherret opened this issue Aug 26, 2018 · 0 comments

Comments

@dsherret
Copy link
Owner

dsherret commented Aug 26, 2018

Would be nice to do this:

project.emit({
    customTransformers: {
        // optional transformers to evaluate before built in .js transformations
        before: [context => sourceFile => visitSourceFile(sourceFile, context, numericLiteralToStringLiteral)],
        // optional transformers to evaluate after built in .js transformations
        after: [],
        // optional transformers to evaluate after built in .d.ts transformations
        afterDeclarations: []
    }
});

function visitSourceFile(sourceFile: ts.SourceFile, context: ts.TransformationContext, visitNode: (node: ts.Node) => ts.Node) {
    return visitNodeAndChildren(sourceFile) as ts.SourceFile;

    function visitNodeAndChildren(node: ts.Node): ts.Node {
        return ts.visitEachChild(visitNode(node), visitNodeAndChildren, context);
    }
}

function numericLiteralToStringLiteral(node: ts.Node) {
    if (ts.isNumericLiteral(node))
        return ts.createStringLiteral(node.text);
    return node;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant